在首页上显示帖子


Display posts on front page

我想在我的wordpress网站上的front-page.php模板上显示帖子。如何在不将其作为静态页面的情况下实现这一点。。?

    <div id="primary" class="site-content">
        <div id="content" role="main">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php if( is_home() ) : ?>
                    <?php get_template_part( 'content' ); ?>
                <?php else : ?>
                    <?php get_template_part( 'content', 'page' ); ?>
                <?php endif; ?>
                <?php comments_template( '', true ); ?>
            <?php endwhile; // end of the loop. ?>
        </div><!-- #content -->
    </div><!-- #primary -->

易于使用:(这将添加一个5篇文章的列表(阅读内联标记(,标题和简短描述(如果你为你的文章设置了readmore标签(

<ul>
<?php $the_query = new WP_Query( 'showposts=5' ); ?> <!-- change the number "5" to the number of posts you want to display -->
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<li><?php the_excerpt(__('(weiterlesen)')); ?></li> <!-- change "weiterlesen" to the value you want to display as "read more..." -->
<?php endwhile;?>
</ul>

或者,如果设置了这个标签,它将显示完整的内容并显示自述标签。

<ul>
<?php $the_query = new WP_Query( 'showposts=5' ); ?><!-- change number of posts to be displayed here -->
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<li><?php the_content(__('(weiterlesen)')); ?></li><!-- change the "read more" value by changing "weiter lesen..."
<?php endwhile;?>
</ul>

或者作为第三种选择——如果你没有设置readmore标签,下面将为每篇文章添加一个标题,摘录限制为200个字符。

<ul>
<?php $the_query = new WP_Query( 'showposts=5' ); ?><!-- change the number of posts here again -->
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<li><?php echo substr(strip_tags($post->post_content), 0, 200);?></li><!-- change the 200 to the number of characters you want to display -->
<?php endwhile;?>
</ul>

希望这个能解决你的问题。

一切都好Fabian

为任何页面中的get posts添加WP_Query

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query(array( 'post_type' => 'post', 'paged' => $paged,'posts_per_page' => get_option('posts_per_page')));

在下面添加此代码,while循环可以获得所有帖子。用户CCD_ 2&用于重置该查询的wp_reset_query();