经过多次谷歌搜索后,我完成了这段代码(在 PHP 上不太好)
<div>
<?php
$args = array(
'post_type' => 'post'
);
$categories = get_categories( $args );
$catlinks = get_category_link( $categories);
foreach ( $categories as $category ) {
echo '<a href=" '.$catlink->link . '"> <h2>' . $category->name .'</h2></a>';
$args['category'] = $category->term_id;
} ?>
</div>
此代码显示了一个 Wordpress 帖子类别循环,我试图获取每个类别链接,但我仍然没有获得正确的链接。
任何提前的帮助都会很棒。
谢谢罗德里戈
最佳答案
你已经很接近了。
您需要运行 get_category_link()
针对 foreach
循环中 $category
的 ID。
看起来像这样:
<?php
foreach ( $categories as $category ) {
echo '<a href="' . get_category_link( $category->term_id ) . '"> <h2>' . $category->name . '</h2></a>';
}
?>
因此,总而言之,您的整个代码应为:
<div>
<?php
$args = array(
'post_type' => 'post'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<a href="' . get_category_link( $category->term_id ) . '"> <h2>' . $category->name . '</h2></a>';
}
?>
</div>
关于php - Wordpress 帖子类别名称链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42590467/