• Resolved Agonius

    (@agonius)


    I’m trying to make a simple plugin that let’s me create blogposts from a custom post_type (tutorials), with it’s own custom taxonomies. I got this working.

    There are 2 problems though.

    1: I also created a widget which works almost the same as the category widget provided by WordPress. It displays the categories from my custom taxonomy (tutorials). It also shows the post count per category. Just like the default category widget for regular posts. But, when I click on one of the categories, I get a ‘Not Found’ page, instead of an overview of my tutorials within that category.

    The url of this page is ‘example.com/tutorials/category’. I already tried to make a custom template called ‘taxonomy-tutorials.php’, which is a copy of the ‘archive.php’, but this displays all posts of my custom post type on the url ‘example.com/tutorials/’. What I want is that only the posts from a certain category will be displayed, on the url ‘example.com/tutorials/category-name/’.

    2: The tutorials (custom post_type) should have the url ‘example.com/tutorials/category-name/blogpost-title’. But I have no idea how to set this correctly. Right now the url is ‘example.com/tutorials/blogpost-title’.

    The permalink structure is set to ‘/%category%/%postname%’. Regular blogposts for example are ‘example.com/category-name/blogpost-title’. And my custom blogposts should be ‘example.com/tutorials/category-name/blogpost-title’.

    Here is the PHP code that registers the custom post_type, the custom taxonomy, and the widget.

    /* Function to register the custom post type */
    function create_tutorials_posttype() {
    	register_post_type( 'tutorials', array(
    			'labels' => array(
    				'name' => __( 'Tutorials' ),
    				'singular_name' => __( 'Tutorial' )
    			),
    			'taxonomies' => array( 'tutorials' ),
    			'public' => true,
    			'has_archive' => true,
    			'rewrite' => array( 'slug' => 'tutorials' )
    		)
    	);
    }
    
    /* Function to register the custom taxonomy */
    function create_tutorials_taxonomy() {
    	register_taxonomy( 'tutorials', 'tutorials', array(
    		'labels' => array (
    			'name' => __( 'Tutorials' ),
    			'Singular_name' => __( 'Tutorial' )
    		),
    		'hierarchical' => true,
    		'public' => true,
    		'show_ui' => true,
    		'show_admin_column' => true,
    		'query_var' => true,
    		'rewrite' => array( 'slug' => 'tutorials' )
    	) );
    }
    
    public function widget( $args, $instance ) {
    	// outputs the content of the widget
    	$title = apply_filters( 'widget_title', $instance['title'] );
    
    	echo $args['before_widget'];
    	if ( ! empty( $title ) ) {
    		echo $args['before_title'] . $title . $args['after_title'];
    	}
    
    	$terms = get_terms( 'tutorials', 'orderby=name&hide_empty=1' );
    	echo '<ul class="taxonomylist">';
    	foreach ($terms as $term) {
    		$link = get_term_link( $term, 'tutorials' );
    		echo '<li><a href="'.$link.'">'.$term->name.'</a><span class="badge"> ('.$term->count.')</span></li>';
    	}
    	echo '</ul>';
    	echo $args['after_widget'];
    }

    I hope someone knows how to do this. Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    One problem you have is you are using the same slug for both your taxonomy and custom post type. Either remove the rewrite from the CPT or assign a different slug.

    I’m not sure this will solve your problem, but it’s a start. You could try explicitly defining a rewrite rule for tutorials, though this should not be necessary. A permalink like /tutorials/tutorial-term should be easily attainable. Tagging on a post title slug beyond that seems to always be fraught with problems. Something about too much information. The title slug alone is adequate to get a post, the taxonomy data is unneeded and seems to confuse the query for some reason beyond my understanding.

    A link like this: example.com/meaningless/gobbledy/goop/known-title-slug works fine. But this fails: example.com/tax-slug/known-tax-term/known-title-slug even though the post has the same term assigned to it, go figure.

    Thread Starter Agonius

    (@agonius)

    Thanks for the answer. I managed to get it working. I think the problem was the same slug for both the custom taxonomy and the custom post type.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problems with displaying custom post type and custom taxonomy’ is closed to new replies.