【问题标题】:Content and title won't show on certain page in Wordpress内容和标题不会显示在 Wordpress 的某些页面上
【发布时间】:2016-03-22 08:24:00
【问题描述】:

我有一个“front-page.php”,它是一个静态的一侧页面。如果我使用 Wordpress 循环查看我在 front-page.php 上的最新帖子,它们都会显示出来。 现在我想创建一个新闻页面,所以我创建了一个文件“page-news.php”。从首页删除循环代码并将其粘贴到页面新闻中。虽然,什么都没有发生。

循环代码:

<?php get_header();?>

<?php
if (have_posts()):
while (have_posts()): the_post();?>

<?php the_title();?>
<?php the_content();?>

<?php
endwhile;
else: echo '<p>no posts were found</p>';
endif;

 ?>

<?php get_footer();?>

我错过了什么?

【问题讨论】:

  • 我不知道你为什么要为每一行打开和关闭php标签,你不能在顶部打开一次,在底部关闭一次吗?
  • @miqdadamirali 也许还有一些其他(HTML)代码与问题无关。 ;)
  • 您是否创建了一个名为 news 的页面?你是 100% 这个模板正在运行吗?如果您只是在代码中添加

    test

    或其他内容,就会显示出来。
  • 我的页眉/页脚可见。另外,是的,我通过 Wordpress 仪表板创建了一个名为“news”的页面。
  • 在您的 get_header 行之后添加我放在

    Test

    的代码 - 并让我知道是否显示 - 需要确保您的模板实际被加载。

标签: php html wordpress loops


【解决方案1】:

您需要添加 wp_Query 主页面被认为是一个博客页面,因此它具有查询默认值。

$args = array (
/*'cat'                    => $catNum,*/
'post_type'              => 'post',
'pagination'             => false,
'posts_per_page'         => '-1',
'ignore_sticky_posts'    => false,
'order'                  => 'DESC',
'orderby'                => 'date',
);

// The Query
$query = new WP_Query( $args );

你应该在之前添加这段代码

if (have_posts()):
while (have_posts()): the_post();?>

关于have_posts()的这部分将是

// The Loop
if ( $query->have_posts() ) { ?>
    <?php while ( $query->have_posts() ) {
        $query->the_post();

不要忘记在末尾添加wp_reset_postdata();,这样您就可以在一页中使用多个查询。

【讨论】:

  • 我按照你的指示做了,但我收到一条错误消息:“unexpected 'endwhile'。这就是代码的样子:jsfiddle.net/n63rh5t3
  • 我尝试创建一个自定义页面模板,并将其添加到 wordpress 仪表板的“新闻页面”中。但是,它仍然只显示页面的名称。
  • 关于你的页面分享代码。使用这个应用程序分享代码climbi.com/create它会更好看
猜你喜欢
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
  • 2011-11-17
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 2012-07-31
相关资源
最近更新 更多