【问题标题】:Wordpress posts array contains only a reference to the home pageWordpress 帖子数组仅包含对主页的引用
【发布时间】:2018-12-21 00:30:24
【问题描述】:

您好,刚刚开始使用 Wordpress,并尝试了解 wordpress 如何使用循环。我正在使用以下教程:

https://www.youtube.com/watch?v=FVqzKAUsM68&list=PLmFwGfciLDFjWspYdKJ4hGaFqzR2ujeJZ

我已经进入教程大约 1 小时 23 分钟,在那里我创建了一个基本上是空的主题:index.php、style.css 和 screenshot.png。

我刚刚创建了 3 个公开发布的帖子。

我现在想列出三个帖子及其标题和内容,为此我在索引中编写了以下代码:

<?php 
if(have_posts()){
    while(have_posts()){
        the_post();
        //echo wp_count_posts();
    ?>
    <h1><?php echo the_title(); ?></h1>
    <h3><?php echo the_content();?></h3>
    <hr>
    <?php

    }
}
?>

我希望看到我拥有的 3 个帖子的标题以及相关内容。

我看到的是首页的标题和首页的内容。

我的理解是我在帖子循环中,因此应该看到帖子数据。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    参考:https://wordpress.stackexchange.com/questions/220590/loop-code-is-displaying-pages-but-not-actual-posts

    获取帖子的代码(来自参考,由用户 frogg3862 编写):

    /* You can replace your original loop with the code below. This will query 10 posts
    and return their titles.
    See http://codex.wordpress.org/Class_Reference/WP_Query for customizing it. */
    
    $args = array(
     'post_type'         => 'post',
     'posts_per_page'    => 10
    );
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
       echo '<ul>';
       while ( $the_query->have_posts() ) {
           $the_query->the_post();
           echo '<li>' . get_the_title() . '</li>';
       }
       echo '</ul>';
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    

    【讨论】:

      【解决方案2】:

      欢迎来到 WordPress 世界! 这是因为您在设置/阅读选项卡中更改了首页设置。 请改回您的最新帖子,然后您将看到您想要的内容。

      为了您的信息,您需要在开始编辑模板文件之前研究template hierarchy。 希望这对您有所帮助。 顺便说一句,您需要使用 the_title();和 the_content();而不是 echo the_title() 和 echo the_content(); (它的默认动作是回声)

      【讨论】:

      • 非常感谢它的即时结果:-)
      猜你喜欢
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2017-08-21
      • 1970-01-01
      • 2014-05-06
      • 2014-07-13
      相关资源
      最近更新 更多