简体   繁体   English

如何使用php获取WordPress网站上发布的所有帖子

[英]how to get all the posts that are posted on WordPress website using php

I am making my own piece of app in PHP and I want to display all the posts that I have posted on my WordPress website (both are on the same server) . 我正在用PHP开发自己的应用程序,我想显示我在WordPress网站上发布的所有帖子(两者都在同一服务器上) I know that WordPress stores data in a database but I am having trouble in finding all the parts of the posts. 我知道WordPress将数据存储在数据库中,但是在查找帖子的所有部分时遇到了麻烦。

So my question is how can one access each and every post and article that are published on my WordPress website. 所以我的问题是,如何访问WordPress网站上发布的每篇文章和文章。

If you're using WordPress just use the native WordPress functions, namely the WP_Query class. 如果您使用的是WordPress,则只需使用本机WordPress功能,即WP_Query类。

$the_query = new WP_Query( ['posts_per_page' => -1]);

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        // All Post Data Available Now
        // Do what you want with it
    }
    wp_reset_postdata();
}

Inside the while loop you have access to all the relevant post information using standard WP functions like get_the_content() , get_the_title() , and even get_post_custom() if you have custom fields. while循环内,您可以使用标准WP函数(如get_the_content()get_the_title()甚至get_post_custom()访问所有相关的帖子信息get_post_custom()如果您具有自定义字段)。

In order to access all WordPress functions you need to add this to your php file. 为了访问所有WordPress功能,您需要将其添加到您的php文件中。

<?PHP

    // require wp-load.php to use built-in WordPress functions
    require_once("../wp-load.php");

?>

Now all you need is, use your mind and WordPress skills to get the posts using basic WP functions. 现在,您需要做的就是,使用您的头脑和WordPress技能,使用基本的WP函数获取帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM