首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换首页中的wordpress循环

问替换首页中的wordpress循环
EN

Stack Overflow用户
提问于 2020-07-14 18:31:06
回答 1查看 55关注 0票数 0

我正在尝试发布一个使用主题twenty seventeen的博客,但想要一个不同的首页方法。而不是显示最近发布的帖子,我想列出所有类别与该类别中最新帖子的摘录。

为此,我需要替换以下代码:

代码语言:javascript
复制
<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编辑代码的解决方案,因为主题的更新将需要我不断地重复这项任务。

有谁能给出一个如何做到这一点的提示?

EN

回答 1

Stack Overflow用户

发布于 2020-07-14 20:44:13

使用下面的代码并用您的模板结构替换内容。

代码语言:javascript
复制
$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即可。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62893166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档