如何获取自定义帖子类别链接

时间:2016-02-06 作者:Max

I\'m trying to echo custom posts sub category link as a view more option.For the last week, i am trying to do that, but i just can\'t find the right way. I\'ve tried, category links, terms, with args, id..whatever i could think of or find here. The only way currently to show is with the id but here i will have to add a number, which is not the option i want. So let me give you more info.

First i created custom posts (note that i removed some options here because they are not needed, so to make it shorter)

 
$args = array(       
        \'taxonomies\' => array( \'category\', \'post_tag\', \'fixture\' ),
        \'public\' => true,
        \'show_in_menu\' => true,
        \'publicly_queryable\' => true,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'rewrite\' => true,
        \'query_var\' => true,
        \'capability_type\' => \'post\'
    );
register_taxonomy(
    \'fixs_cat\',
    \'match\',
    array(
      \'label\' => __( \'Fixture Categories\', \'ko-text\' ),
      \'rewrite\' => array( \'slug\' => \'fixture\' ),
      \'hierarchical\' => true,
      \'query_var\' => true
    )
  );
register_post_type( \'match\', $args );
}
 

Then i created widget which will display them. It\'s a tabbed widget so i wont add all tabs options because all are the same, only number changes from 1 to 10.

 
public function widget( $args, $instance )
/* Widget variables. */
        $fix_name1 = $instance[\'fix_name1\'];
        $fix_cat1 = $instance[\'fix_cat1\'];
        $fix_num1 = $instance[\'fix_num1\'];

        $fix_name2 = $instance[\'fix_name2\'];
        $fix_cat2 = $instance[\'fix_cat2\'];
        $fix_num2 = $instance[\'fix_num2\'];

 

public function form( $instance ) {
TODO: Define default values for your variables
$defaults = array( 
\'sort\' => \'latest\', 
\'$fix_name1\' => 10, 
\'$fix_name2\' => 10, 
        );
        $instance = wp_parse_args(
            (array) $instance
        );

Then in widget setting I\'ve made option so the user can select title, category and how many post does he want to show from selected custom post category. Can\'t post it here, without breaking it so here http://pastebin.com/hjpdrdnA

And the final thing is actual loop which displays those posts in widget

 
if($fix_name1) { ?>
 \'match\', 
\'posts_per_page\' => $fix_num1, 
\'tax_query\' => array(
 array( \'taxonomy\' => \'fixs_cat\',
 \'field\' => \'slug\',
  \'terms\' => $fix_cat1 ))
  )); while($recent->have_posts()) : $recent->the_post();?>     
//Stuff to display
 
 } ?>

For an example this code with term link lists all categories, but i want only the one which is selected under tab1 and is here $fix_name1

http://codex.wordpress.org/Function_Reference/get_term_link - here for terms I added a general category like get_terms(\'fixs_cat\')

//Something short like this echo $fix_cat1; displays needed category but the main custom post category is missing in the link it should be fixture/category selected and this shows only /category selected.

//this does the same from the same code example

 
$fixs = get_terms(\'fixs_cat\'); 
foreach($fixs as $fix) { ?>
if ($fix->slug == $fix_cat1) 

Any ideas how to solve this?

1 个回复
最合适的回答,由SO网友:Max 整理而成

这是我能做的最好的了。它给出了所需的内容,但正如我所说的,它在url中显示类别,而不是应该是“fixture”。不过,效果很好。term\\u id;//category$category\\u link=获取\\u category\\u链接($catID);?>“title=”Category Name“>查看更多信息

相关推荐

Get_Terms()在自定义帖子类型上提供了错误的自定义分类Childs计数

我有一个自定义的分类法,我们称之为;指示;。我们将其与自定义帖子类型“配合使用”;产品;。我们对术语Parent->;小孩父级从未与产品建立连接。我使用它来创建一个自定义的select字段,并且需要父字段作为optgroup标题。是否使用此结构父级子级1(根据后端的2篇帖子)子级2(根据后端的1篇帖子)但是如果我查询子主题并检查计数,它总是显示为NULL。在Wordpress仪表板中,它使用2和1正确定位。为什么不在前端呢。 $args = array( \'hide_e