【问题标题】:Cant' Grab WordPress Built-in Posts Through Loop无法通过循环抓取 WordPress 内置帖子
【发布时间】:2013-11-03 09:51:58
【问题描述】:

使用 WordPress 3.7.1 我试图在我创建的页面上显示所有常规帖子,以免说 TestPage。以下是我为此采取的步骤:

1- 生成一个名为:Test Page 的自定义页面模板并通过以下代码加载
2- 根据Test Page 模板生成一个名为 TestPage 的页面

更新页面后,我没有在页面上收到任何帖子,而我已经生成了一些帖子!

<?php
/*
Template Name: Test Page
*/
?>
<?php get_header(); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h1><?php the_title() ;?></h1>  
    <?php the_content(); ?>
<?php endwhile; else: ?>
    <p>Sorry, this page does not exist</p>
<?php endif; ?>

<?php get_footer(); ?>

他上面的代码实际上是加载页面的标题和 TestPage 的内容,而不是通过帖子!你能告诉我为什么会这样吗?

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    您必须了解帖子和页面之间的区别。

    页面与模板相关联。通过代码,您可以使用此页面/模板列出帖子或其他页面。

    以上代码只会显示页面数据

    您不希望使用相同的模板创建许多页面,以便使用相同的模板显示不同的数据。相反,您创建帖子并给它们一个类别。

    然后您可以获取这些帖子并以与测试页面相同的方式显示它们。

    您可以使用get_postswp_get_recent_posts
    或者,如果您真的想列出页面,请使用wp_list_pages

    除此之外,您还应该查看http://codex.wordpress.org/Pageshttp://codex.wordpress.org/Posts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多