php Wordpress 获取类别链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14816552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:01:19  来源:igfitidea点击:

Wordpress get category link

phpwordpress

提问by Boy Pasmo

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

<?php

$categories = get_categories();

$categories = get_categories();

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

?>

?>

回答by anotherthink

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.

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

回答by Irfan DANISH

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>