【发布时间】:2020-01-15 00:18:13
【问题描述】:
我想在 index.php 的循环中在第一个帖子之后显示一个类别列表(这是我的 WP 主题用来显示帖子的模板)。
我在网上四处搜索,发现了一些代码(见下文),它应该可以按照我的意愿执行 - 将类别标题列表作为循环中帖子列表之间的链接注入。
但是,它没有按预期工作。它只显示一个类别标题,而不是全部。有趣的是,它显示了第一个帖子类别的标题(自定义代码之前的帖子),但没有其他内容。
我的Loop代码,包括我插入的自定义代码如下:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
// START CUSTOM CODE
<div>
<?php
if( $wp_query->current_post == 0 ) {
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= '<a href="'.get_category_link( $category ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
}
echo trim($output, $separator);
}
}
?>
</div>
// END CUSTOM CODE
<?php endwhile; ?>
希望有人能提供帮助。
谢谢,
湄公河
【问题讨论】: