首页 文章

如何在OpenCart中创建一个简单的模块?从Wordpress获取最新帖子并在OpenCart中显示的示例?

提问于
浏览
21

我是这个论坛的新手,也是OpenCart的新手 .

我需要有关在OpenCart中创建模块的帮助 . 在我的情况下,它将从我的WordPress安装的每个类别中获取最新的5个帖子,并将其显示在我的OpenCart商店的主页中 .

我已在同一主机上的同一数据库中安装了OpenCart和WordPress .

有人可以就此提出建议吗?

2 回答

  • 32

    根据您的技能,这可能非常容易 . 我希望你的问题可以进行下调,但我会简要介绍一下这些步骤,因为这不是SO的工作方式 . 首先是编辑我们的THEMES文件 . 由于OpenCart是MVC,我们编辑我们的主题然后我们的PHP ...或PHP,然后是THEME文件......反之亦然..

    指南

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

    在此之后:

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

    添加这个:

    <?php MyWordPressFunction() ?>
    

    或这个:

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

    2 - 打开我们的PHP代码,现在是 home.tpl 页面的代码,这是 /catalog/controller/common/home.php

    在主类和结束 ?> PHP标记之后的代码底部添加:

    // 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
    
    }
    

    这应该让你了解一些基本的PHP函数调用 . 这是一个让你入门的方向 . 您可以进一步扩展到链接类别,作者链接等 .

    顺便说一下,所有这些变量都可以在WP_Posts表中看到:

    /*
    
    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
    
    */
    

    小贴士

    通常在SO上查看整个OpenCart过滤器 - 有很多关于编写mod的文章,修改它的工作方式和创建自定义页面 - 这些将有助于你长时间的调整 . 上面的代码没有样式或进一步调整,这是一个指南 .

    进一步阅读和更好的模块类型帖子

    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?

  • 1

    感谢TheBlankBenzKid提供了一个非常有用的答案,但我认为这里有一件值得添加的小事 . 如果您希望在opencart商店中显示您的wordpress博客,请确保为您的wordpress数据库提供对opencart数据库用户的正确用户权限,通过cpanel完成我的情况 .

相关问题