WordPress的获取类别链接(Wordpress get category link)

2019-07-19 18:11发布

我想问如何让每一个有它相关的职位类别的链接。 我只得到了一些代码,将只显示父类别。 任何帮助将非常感激。 谢谢!

<?php

$categories = get_categories();

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

?>

Answer 1:

听起来像你可能想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>';
}

应打印出链接到的类别为您服务。



Answer 2:

根据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>


文章来源: Wordpress get category link
标签: php wordpress