【问题标题】:show wordpress post outside non wordpress php page在非 wordpress php 页面之外显示 wordpress 帖子
【发布时间】:2017-12-08 03:07:09
【问题描述】:

我需要在非 wordpress php 页面中显示 wordpress 博客文章。我试过下面的代码。

<?php
// Include WordPress
define('WP_USE_THEMES', false);
//exact path for wp-load.php.
// This file is kept in the root of wordpress install
require('http://test.com/wordpress/blog/wp-load.php');
//Query wordpress for latest 4 posts
query_posts('showposts=5');
?>
<?php while (have_posts ()): the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>

<?php endwhile; ?>

但它显示了以下错误

Fatal error: Call to undefined function query_posts()

如何解决这个问题?

【问题讨论】:

  • 您可以使用 rss 选项,您可以编写一个新的隐藏代码并使用该文件读取数据...以 JSON 格式

标签: php wordpress


【解决方案1】:

请查看代码中的以下行

require('http://test.com/wordpress/blog/wp-load.php');

在 require 函数中,您应该使用相对路径或物理路径。您不应包含网址。

【讨论】:

    【解决方案2】:

    由于您需要 wordpress 数据库和框架,这似乎根本不可能工作。尝试从您的 wordpress 中获取 XML、RSS 或 JSON 数据,您可以使用自制脚本获取这些数据。

    【讨论】:

      【解决方案3】:

      对于外部集成,通过 RSS 路由进行处理可能更可靠。完成这项工作的最简单也可能是最懒惰的方法是使用 simplexml_load_file(和 HTTP 流。)

      $t = simplexml_load_file( "http://blogs.voanews.com/breaking-news/feed/" );
      
      foreach( $t->channel->item as $item ) {
          printf(
              "<div>%s <a href='%s'>%s</a></div><hr/>",
              $item->description,
              $item->link,
              $item->title
          );
      }
      

      这会输出您希望看到的提要。请注意,这不使用任何类型的缓存,因此每个页面请求都会命中原始提要。

      <div>Some Chinese officials are furious at Apple's iPhone for apparently 
      helping users have too much of a good time. Chinese media say the complaints
      surround the iPhone's voice-activated personal assistant, known as
      &#8220;Siri,&#8221; which has been helping some users find prostitutes and
      brothels. The Mandarin language version can apparently present users with as
      many as [...] <a href='...(snip)...)'>iPhone Under Fire in China over
      Prostitution</a></div>
      

      【讨论】:

        【解决方案4】:

        您可以使用 rss 选项,您可以编写一个新的隐藏代码并使用该文件以 JSON 格式读取数据...

        也许要做的第一件事是搜索为您执行此操作的扩展/插件/模块;

        我认为你不是第一个想要这样做的人:p

        【讨论】:

          【解决方案5】:

          为了在 wordpress 设置之外显示最近的帖子,首先包含 wp-load.php 文件。

          require( './blog/wp-load.php' );
              // Load the recent top 10 posts
              $args = array( 'posts_per_page' => 10, 'post_status'=>"any", 'post_type'=>"post", 'orderby'=>"date","order"=> "DESC", "suppress_filters"=>true);
              $postslist = get_posts( $args );
          

          现在您可以循环访问 $postlist 变量了。

          foreach ($postslist as $post) : setup_postdata($post);?> 
                <ul class="media-list">
                  <li class="media">
                    <div class="media-left"> <?php the_post_thumbnail( array(80,80));?> </div>
                    <div class="media-body">
                      <a target="_blank" href="<?php the_permalink();?>"><h5 class="media-heading"><?php the_title(); ?></h5></a>
                      <p><?php echo $post->post_content; ?></p>
                    </div>
                  </li>
                </ul>
          
          <?php endforeach;
          

          【讨论】:

            【解决方案6】:

            包含 wp-load.php 后,您必须像这样实例化查询:

            $wp_query = new \WP_Query();
            $wp_query->query('showposts=5');
            

            之后你的循环应该是这样的:

            <?php while ($wp_query->have_posts()) :
                $wp_query->the_post();
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <?php the_excerpt(); ?>
            
            <?php endwhile; ?>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-03-16
              • 1970-01-01
              • 1970-01-01
              • 2016-04-10
              • 2017-07-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多