Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

php - How to make a simple module in OpenCart? Example getting latest posts from Wordpress and showing it in OpenCart?

I am new to this forum and as well as to OpenCart.

I need help on creating a module in OpenCart. In my case it will get the latest 5 posts from each category of my WordPress installation and display it in my home page of my OpenCart store.

I have already installed OpenCart and WordPress in same database on the same host.

Can someone advice me on this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This can be very easy depending on your skills. I expect a downvote on your question but I will briefly run through the steps since this is not the way SO works. The first thing is to edit our THEMES files. Since OpenCart is MVC, we edit our Theme and then our PHP... or PHP and then the THEME files.. this is vice versa..

Guide

1 - Open /catalog/view/theme/default/template/common/home.tpl

After this line:

<h1 style="display: none;"><?php echo $heading_title; ?></h1>

Add this:

<?php MyWordPressFunction() ?>

or this:

<div>
    <h2>Latest posts from our blog</h2>
    <?php MyWordPressFunction() ?>
</div>

2 - Open our PHP code which is now the code for the home.tpl page, this is /catalog/controller/common/home.php

At the bottom of the code after the main class and the ending ?> PHP tag add this:

// WORDPRESS LATEST POSTS
//#customPHP
// The tag above is so that when you upgrade OpenCart
// Before doing so you need to make sure of all the core
// core changes you made - a unique global comment tag
// is easy to find.

function MyWordPressFunction(){

    // DB
        // GET THE POSTS
        // LIMIT BY 5
        // ORDER BY LATEST POSTS
    $sql=mysql_query("SELECT * FROM `wordpress_db`.`wp_posts` ORDER BY `wp_posts`.`post_date` DESC LIMIT 5");
    while($row = mysql_fetch_array($sql)){

        // VARS (easy to play with in the echo)

        $id=$row["ID"];
        $author=$row["post_author"];
        $date=$row["post_date"];
        $post=$row["post_content"];
        $title=$row["post_title"];

        echo '

            <div id="postID_'.$id.'>

                <h3>'.$title.'</h3>

                <p>'.$post.'</p>

                <p>Posted by '.$author.' on '.$date.'</p>

            </div>

        ';

    }

    // END DB

}

That should give you an idea of some basic PHP function calls. It is a direction to get you started. You can further expand to link categories, author links, etc..

By the way, all these variables can be used as you can see in the WP_Posts table:

/*

All these can be used

ID
post_author
post_date
post_date_gmt
post_content 
post_title
post_excerpt
post_status
comment_status
ping_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mime_type
comment_count

*/

Tips

Generally look through the entire OpenCart filter on SO - there are many articles on writing mods, modifying how it works and creating custom pages - these will really help in your long time tweaking. The above code is not got styling or further tweaks, this is a guide.

Further reading and better module type posts

How to add new module to opencart administration?

How to add new module to opencart administration?

How to create a custom admin page in opencart?

How to create a custom admin page in opencart?

How do I get an external page coupon/voucher form to work in OpenCart?

How do I get an external page coupon/voucher form to work in OpenCart?

Opencart - How I can execute a custom code on product page? Without mods on controller product

Opencart - How I can execute a custom code on product page? Without mods on controller product

How can I display the SubTotal on OpenCart on any page?

How can I display the SubTotal on OpenCart on any page?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...