我正在尝试发布一个使用主题twenty seventeen的博客,但想要一个不同的首页方法。而不是显示最近发布的帖子,我想列出所有类别与该类别中最新帖子的摘录。
为此,我需要替换以下代码:
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/post/content', get_post_format() );
endwhile;
the_posts_pagination( ... );
else :
get_template_part( 'template-parts/post/content', 'none' );
endif;
?>
</main><!-- #main -->在index.php文件中,使用一个遍历每个类别的文件,显示这个类别中的最新帖子(类别的名称也应该显示在帖子内容的上方)。
我只有很少的php使用经验。我正在寻找一个不需要在wordpress的Edit theme选项中为index.php编辑代码的解决方案,因为主题的更新将需要我不断地重复这项任务。
有谁能给出一个如何做到这一点的提示?
发布于 2020-07-14 20:44:13
使用下面的代码并用您的模板结构替换内容。
$cats = get_the_category();
foreach($cats as $cat) {
$args = array('numberposts' => 1, 'category' => $cat->term_id);
$post = get_posts($args);
echo 'Post under Category: '.$cat->name;
echo get_the_excerpt($post);
echo '<br>';
}我不想在foreach中运行while循环,这将是一个非常糟糕的做法。这是基本代码,只需根据需要替换html即可。
https://stackoverflow.com/questions/62893166
复制相似问题