简体   繁体   English

获取类别小部件的Wordpress侧栏中的类别链接

[英]get category link in Category Widget Wordpress sidebar

I wanted create a widget for the sidebar of the theme that i'm working on recently...But I can't find a way to get links of categories. 我想为我最近正在研究的主题的侧边栏创建一个小部件...但是我找不到找到类别链接的方法。

This is the code of my widget: 这是我的小部件的代码:

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

Everything is fine...Markup is exacly what I want...But I don't know what to put in <a href=""> to get the links of categories... 一切都很好...标记完全是我想要的...但是我不知道要放入<a href="">来获取类别的链接...

ANY HELP WOULD BE APPRECIATED... 任何帮助,将不胜感激...

Use 采用

  echo get_category_link( $category->term_id );

To get the link of the given category term. 获取给定类别术语的链接。

Documentation for the function is here: https://codex.wordpress.org/Function_Reference/get_category_link 该功能的文档在这里: https : //codex.wordpress.org/Function_Reference/get_category_link

Try this, 尝试这个,

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>';
}

Change your line 换行

echo '<a href=""><li>'. $category->name . '<span>'. $category->count .'</span></li></a>';

..into ..进入

$category_id = get_cat_ID( $category->name );
echo '<a href="' . get_category_link( $category_id ) .'"><li>'. $category->name . '<span>'. $category->count .'</span></li></a>';

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

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