简体   繁体   English

WordPress的获取类别链接

[英]Wordpress get category link

I want to ask on how to get the link of each category that have related post on it. 我想问一下如何获取具有相关帖子的每个类别的链接。 I only got some code that will only display parent category. 我只有一些仅显示父类别的代码。 Any help would be much appreciated. 任何帮助将非常感激。 Thanks! 谢谢!

<?php

$categories = get_categories();

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

?>

Sounds like you may want get_category_link -- something like 听起来像您可能想要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>';
}

should print out the links to the categories for you. 应该为您打印出类别的链接。

According to Function Reference/get category link 根据Function Reference/get category link

<?php get_category_link( $category_id ); ?> 

Example: 例:

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM