如何在首页和博客页面中浏览帖子?

时间:2016-10-25 17:40:50

标签: php wordpress

我想根据帖子类型在主页上显示一些帖子,但我无法访问帖子和帖子循环只返回“主页”帖子,那么实现这一点的正确方法是什么?

我的阅读设置

  • 首页显示:静态页面
  • 首页:主页
  • 帖子页面:博客

1 个答案:

答案 0 :(得分:1)

保持您的阅读设置。

您可以创建一个名为front-page.php的主题文件,并使用它来控制您的主页。

front-page.php内,您可以使用get_posts检索帖子然后循环播放

示例:

<?php get_header(); ?>

<?php
$args = array(
    'numberposts' => 10, // number of posts to return
    'post_type' => 'your-post-type' // change this to the post type you want to retrieve
);


$posts = get_posts( $args );

if ( $posts ) : 
    foreach ( $posts as $post ) : setup_postdata( $post );  ?>

        <article <?php post_class(); ?>>
            <h1><?php the_title(); ?></h1>
            <?php the_content(); ?>
        </article>


    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>

<?php endif; ?>

<?php get_footer(); ?>
相关问题