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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13208488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:58:39  来源:igfitidea点击:

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

phpwordpressopencart

提问by Developer

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

我是这个论坛的新手,也是 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.

我需要在 OpenCart 中创建模块的帮助。就我而言,它将从我的 WordPress 安装的每个类别中获取最新的 5 个帖子,并将其显示在我的 OpenCart 商店的主页中。

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

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

Can someone advice me on this?

有人可以就此给我建议吗?

回答by TheBlackBenzKid

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..

根据您的技能,这可能非常容易。我希望对您的问题投反对票,但我将简要介绍这些步骤,因为这不是 SO 的工作方式。第一件事是编辑我们的主题文件。由于 OpenCart 是 MVC,我们先编辑我们的主题,然后是 PHP... 或 PHP,然后是主题文件.. 反之亦然..

Guide

指导

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

1 - 打开 /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.tplpage, this is /catalog/controller/common/home.php

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

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

在主类和结束?>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

}

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..

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

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

顺便说一下,所有这些变量都可以使用,就像您在 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

*/

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.

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

Further reading and better module type posts

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

How to add new module to opencart administration?

如何将新模块添加到 opencart 管理?

How to add new module to opencart administration?

如何将新模块添加到 opencart 管理?

How to create a custom admin page in opencart?

如何在 opencart 中创建自定义管理页面?

How to create a custom admin page in opencart?

如何在 opencart 中创建自定义管理页面?

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

如何在 OpenCart 中获取外部页面优惠券/凭证表格?

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

如何在 OpenCart 中获取外部页面优惠券/凭证表格?

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

Opencart - 如何在产品页面上执行自定义代码?控制器产品上没有模组

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

Opencart - 如何在产品页面上执行自定义代码?控制器产品上没有模组

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

如何在任何页面的 OpenCart 上显示小计?

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

如何在任何页面的 OpenCart 上显示小计?

回答by Gavin Simpson

Thank you TheBlankBenzKid for a very helpful answer, but I think there is one small thing worth adding here. If you do wish to display your wordpress blogs on your opencart shop, make sure you give your wordpress databases the correct user permissions to the opencart database user, done im my case via cpanel.

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