4

我想问一下如何获取每个有相关帖子的类别的链接。我只有一些只显示父类别的代码。任何帮助将非常感激。谢谢!

<?php

$categories = get_categories();

foreach ($categories as $cat){
   if($cat->parent < 1){
      echo $cat->cat_name ;
   }
}

?>

4

2 回答 2

19

听起来你可能想要get_category_link - 类似于

$categories = get_categories();
foreach ($categories as $cat) {
   $category_link = get_category_link($cat->cat_ID);
   echo '<a href="'.esc_url( $category_link ).'" title="'.esc_attr($cat->name).'">'.$cat->name.'</a>';
}

应该为您打印出类别的链接。

于 2013-02-11T16:51:33.453 回答
1

根据Function Reference/get category link

<?php get_category_link( $category_id ); ?> 

例子:

<?php
// Get the ID of a given category
$category_id = get_cat_ID( 'Category Name' );

// Get the URL of this category
$category_link = get_category_link( $category_id );
?>

<!-- Print a link to this category -->
<a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>
于 2013-02-11T17:12:38.817 回答