我想为主题的侧边栏创建一个小部件,我最近正在工作…但是我找不到一个方法来获得类别的链接。
这是我的小部件代码:
<section class="sidebar-categories">
<div class="inner">
<h3><label>categories</label></h3>
<ul>
<?php
$args = array(
'taxonomy' => 'category',
'parent' => 0, // get top level categories
'orderby' => 'name',
'order' => 'ASC',
'number' => 2,
'hierarchical' => 1,
'pad_counts' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ){
echo '<a href=""><li>'. $category->name . '<span>'. $category->count .'</span></li></a>';
}
?>
</ul>
</div><!-- /inner -->
</section><!-- /sidebar-categories -->
一切都好……加价正是我想要的。但是我不知道在<a href="">
中放入什么来获得类别的链接…
使用
echo get_category_link( $category->term_id );
获取给定类别项的链接。
该函数的文档在这里:https://codex.wordpress.org/Function_Reference/get_category_link
试试这个,
foreach ( $categories as $category ){
$category_link = get_category_link( $category->cat_ID );
echo '<a href="'.esc_url( $category_link ).'"><li>'. $category->name . '<span>'. $category->count .'</span></li></a>';
}
换行
echo '<a href=""><li>'. $category->name . '<span>'. $category->count .'</span></li></a>';
. .到
$category_id = get_cat_ID( $category->name );
echo '<a href="' . get_category_link( $category_id ) .'"><li>'. $category->name . '<span>'. $category->count .'</span></li></a>';