我有两个代码。我在page.php上使用的第一个,它显示我在管理面板中创建的任何页面的内容。第二个代码用于在主页上显示我的帖子,只是不确定该代码应该去哪里。
它可以工作,如果我把它放在page.php中,但然后相同的内容(帖子)显示在我创建的任何页面上。我尝试将第二个代码放在home.php和index.php中,同时使用page.php中的第一个代码但不起作用。
if (have_posts()): while (have_posts()): the_post();
wp_title(''); echo '<br />';
the_content(); echo '<br />';
endwhile; endif;
<--- Second Code -->
$args = array( 'posts_per_page' => 10 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
setup_postdata( $post ); ?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail( array(100,100) ); ?></div>
<div id="pcontent">
<a href="<?php the_permalink(); ?>" class="ptitle"><?php the_title(); ?></a>
<?php the_excerpt(); ?><br />
Post Category: <?php the_category( ', ' ); ?>
</div>
</div>
<?php endforeach;
wp_reset_postdata(); ?>
我想你应该看看以下内容:https://developer.wordpress.org/themes/basics/template-hierarchy/#home-page-display
您会注意到,如果存在,home.php是WP将用于显示您的帖子页面的文件 - 当然,如果在设置>阅读>帖子页面中设置了与您的正常首页分开的页面。
然后在home.php中,你可以添加你喜欢的任何东西,即 -
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name'
);
wp_list_categories( $args );
?>
为了澄清,如果你的主题中不存在home.php,那么WP会查找index.php。
page.php,single.php有相同的查询帖子,因为它只会获取内容。您可以通过以下方式区分。
主页显示在fallowing中添加相同的代码(查询帖子)
1)home.php 2)index.php
单一帖子
单个帖子模板文件用于呈现单个帖子。 WordPress使用以下路径:
单页
用于呈现静态页面的模板文件(页面后置型)。请注意,与其他后期类型不同,页面对于WordPress是特殊的,并使用以下补丁:
详细说明:click here