【问题标题】:How to make Wordpress show only one post on the front page with comments, and comment form?如何让 Wordpress 在首页上只显示一篇带有评论和评论表格的帖子?
【发布时间】:2011-03-12 17:33:42
【问题描述】:

我执行以下操作以在我的主页中显示单个帖子:

home.php:

<?php
get_header();
query_posts('posts_per_page=1'); //returns only the front page
?>

<div id="content">
    <?php
    /* Run the loop to output the posts.
     * If you want to overload this in a child theme then include a file
     * called loop-index.php and that will be used instead.
     */
    get_template_part( 'loop', 'index' );
    ?>
</div>
<div id="sidebar">
    <?php get_sidebar(); ?>
</div>
<div id="footer">
<?php get_footer(); ?>

但我仍然必须单击Comments 才能查看 cmets 和表单以发表评论。如何立即自动显示?

【问题讨论】:

    标签: wordpress post comments


    【解决方案1】:
        <?php get_header(); ?>
        <div class="blog">
            <section>
                <div class="container">
                    <div class="row">
                        <div class="blog-sidebar">
                            <?php get_sidebar(); ?>
                        </div>
                        <div class="span8">
                        <?php $articles = new WP_Query(array(
                                            'post_type' => 'post',
                                            'post_status' => 'publish',
                                            'posts_per_page' => 1
                                        ));?>
                            <?php  while ($articles->have_posts()): $articles->the_post(); ?>
                                <article id="post-<?php the_ID(); ?>" role="article">
                                    <header>
                                        <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'wpbs-featured' ); ?></a> 
                                    <section class="post_content clearfix" >
                                         <?php the_content(); ?>
                                    </section> 
                                </article>
                            <?php endwhile; ?> 
                          <?php comments_template('',true); ?>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    
        <?php get_footer(); ?>
    

    【讨论】:

      【解决方案2】:

      使用 &lt;?php comments_template(); ?&gt; 加载 cmets 模板 - 这将显示当前帖子的所有 cmets。

      【讨论】:

      • 抱歉,我将该代码放在#content div 之前,但没有任何反应!
      • 如果您想在加载循环之前显示 cmets,请调用 the_post(),加载您的 cmets,然后使用 rewind_posts() 重置循环。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 2011-10-21
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多