Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

Agonius on "Problems with displaying custom post type and custom taxonomy"

$
0
0

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 all articles
Browse latest Browse all 8245

Trending Articles