【问题标题】:Why is my Wordpress Post Loop not working?为什么我的 Wordpress Post Loop 不起作用?
【发布时间】:2018-01-05 07:18:56
【问题描述】:

好的,我正在尝试运行 wordpress 帖子循环以使用 the_title() 和 the_content() 打印帖子标题和内容。但它没有打印出页面上的帖子?

有什么想法吗?

index.php

        <div class="blog-container">
        <!--  The POST LOOP -->
        <?php
        if(have_posts()) :
            while (have_posts()) : the_post(); ?>

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

            <?php endwhile; ?>

            <?php else : 
                echo '<p>No content found</p>';

            endif; 

        ?>

    </div>

编辑好的,我只打印内容而不是帖子标题,这是一张图片:http://imgur.com/a/DiBqG。

编辑 2 内部 wordpress 管理帖子应如下所示:http://imgur.com/a/1DQAp

【问题讨论】:

  • 如果在开始循环之前添加 global $wp_query; var_dump($wp_query); die; 会发生什么。查询对象中是否列出了任何帖子?
  • 我觉得不错。也许您在一个没有帖子的页面上?您可以启用查询日志。
  • 你可能想也可能不想使用&lt;pre&gt;标签来更好地格式化输出。
  • 好的,我只打印内容而不是帖子标题,这是一张图片:imgur.com/a/DiBqG
  • 请分享您的网站链接

标签: php html wordpress css loops


【解决方案1】:

您没有定义查询并以错误的方式使用 Wordpress 内置函数。这是更正。

<?php
// The Query
$the_query = new WP_Query(  );
// 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();
} else {
// no posts found
}

【讨论】:

    【解决方案2】:

    添加以下代码以获取帖子数据

     <?php
        $post_args=array(
            'type'                     => 'post',
            'post_status'              => 'publish',
            'posts_per_page'           => -1,
            'orderby'                  => 'date',
            'order'                    => 'ASC',
    
        );
    
        $post_my_query = null;
        $post_my_query = new WP_Query($post_args);
    
        if( $post_my_query->have_posts() ) 
        {
            while ($post_my_query->have_posts()) : $post_my_query->the_post(); 
            ?>
            <h2><?php echo get_the_title( $post_my_query->ID );?></h2>
            <?php
                 echo get_the_content( $post_my_query->ID );
              endwhile;
        }
        wp_reset_query($post_my_query);
    ?>
    

    【讨论】:

    • 粘贴到 index.php 中,只获取内容,我已经在工作了
    • 那是什么问题?请描述更多。我认为这是重置数据的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多