1

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?

2
  • I still don't know the answer but considering that this does the trick i wonder is this approach a correct one <?php $link = get_bloginfo( 'url' ) . '/fixture/' . $fix_cat1 . '/'; ?> <a href="<?php echo $link ?>">View More</a>
    – Max
    Feb 10, 2016 at 3:56
  • Going by the slug i get it work. Only thing is that url goes like sitename/category/$fix_cat1 and now i want instead /category/ it says custom post default category so it would be /fixture/$fix_cat1 This currently works..any idea how i could swap these two things <?php $slug = $fix_cat1; $cat = get_category_by_slug($slug); $catID = $cat->term_id; // category $category_link = get_category_link( $catID ); ?> <a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">View More</a>
    – Max
    Feb 10, 2016 at 6:11

1 Answer 1

0

This is the best i could do. It gives what's wanted, but as i said it displays category in url instead what should be "fixture". Still, works fine. term_id; // category $category_link = get_category_link( $catID ); ?> " title="Category Name">View More

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.