doujiao2443
2015-10-09 13:44
浏览 35
已采纳

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

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

ANY HELP WOULD BE APPRECIATED...

图片转代码服务由CSDN问答提供 功能建议

我想为我最近正在处理的主题的侧边栏创建一个小部件...但我可以 找不到获取类别链接的方法。

这是我的小部件的代码:

 &lt; section class =“  sidebar-categories“&gt; 
&lt; div class =”inner“&gt; 
&lt; h3&gt;&lt; label&gt; categories&lt; / label&gt;&lt; / h3&gt; 
&lt; ul&gt; 
&lt;?php  
 $ args = array(
'taxonomy'=&gt;'category',
'parent'=&gt; 0,//获取顶级类别
'orderby'=&gt;'name',
'  order'=&gt;'ASC',
'number'=&gt; 2,
'hierarchical'=&gt; 1,
'pad_counts'=&gt; 0 
); 
 
 $ categories = get_categories  ($ args); 
 
 foreach($ categories as $ category){
 
 echo'&lt; a href =“”&gt;&lt; li&gt;'。  $ category-&gt;名称。  '&LT;跨度&GT;'。  $ category-&gt; count。'&lt; / span&gt;&lt; / li&gt;&lt; / a&gt;'; 
 
} 
?&gt; 
&lt; / ul&gt; 
&lt; / div&gt;&lt;  ;! -  / inner  - &gt; 
&lt; / section&gt;&lt;! -  / sidebar-categories  - &gt; 
   
 
 

一切都很好。 ..Markup是我想要的......但是我不知道在&lt; a href =“”&gt; 中放入什么来获取类别的链接......

任何帮助都会得到赞赏......

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douguyi3903 2015-10-09 13:50
    已采纳

    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

    点赞 打赏 评论
  • doujianwei8217 2015-10-09 13:51

    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>';
    }
    
    点赞 打赏 评论
  • dongmanzui8486 2015-10-09 13:52

    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>';
    
    点赞 打赏 评论

相关推荐 更多相似问题