[Closed] custom post list doesn't appear in a category-slug.php file.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by bruce 8 years ago.

Assigned support staff: bruce.

Author
Posts
#13816

I created a custom post type like house
(

Custom post type name plural (required): houses

Custom post type name singular (required): house

Slug (required): house

Description

Visibility
Make this type public :check

Select Taxonomies
category:check

Rewrite : true
Use the normal WordPress URL logic : check

Allow permalinks to be prepended with front base: true

Feeds: true

Pages: true

has_archive: true

show_in_menu: true

show_ui: true

publicly_queryable: ture

exclude_from_search: false

hierarchical: false

can_export:true

show_in_nav_menus:true

query_var:true

Permalink epmask
EP_PERMALINK

) and generated view templates.

What I want is to display house category in the "category-house.php" file. (My theme is twentyeleven.)

I added a category like house and duplicated category.php and renamed it as category-house.php.

And when I published custom posts, I assigned them to the category house.

But when I address hidden link, it doesn't show any post but shows "Apologies, but no results were found ......"

Please help me to display custom post list in a category-slug.php file.

#13820

bruce
Supporter

Languages: English (English )

Timezone: Australia/Perth (GMT+08:00)

Hi David,

WordPress doesn't do this by default and you'll need to add a filter hook to your theme functions.php file.

Something like this:

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

	if ( is_post_type_archive() && $query->is_main_query() ) {
		$query->set( 'post_type', array( 'post', 'house' ) );
        }

	return $query;
}

Best regards,
Bruce.

#13832

I added the code above to the functions.php but nothing happened.

So I added the code below in the category-house.php file.


<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?> 
.
.
.
<?php
  $type = 'house';
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $args=array(
    'post_type' => $type,
    'post_status' => 'publish',
    'paged' => $paged,
    'is_category' => 1,
    'category_name' => $term->slug,
    'posts_per_page' => 8,
  );
  $temp = $wp_query;
  $wp_query = null;
  $wp_query = new WP_Query();
  $wp_query->query($args);
?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
.
.
.
<?php endwhile; ?>
.
.
<?php if(function_exists('wp_pagenavi')) {

	wp_pagenavi(array( 'query' => $wp_query )); 

} ?>

It shows the post and pagenavi but when I click the second page(/page/2/), it goes to 404 error page.

Do you have any idea?

#13833

bruce
Supporter

Languages: English (English )

Timezone: Australia/Perth (GMT+08:00)

Hi David,

If you do it that way then you have to handle pagination yourself.

Try this in your functions.php

add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_archive() ) {
/* Taxonomy archives. */
if ( is_tax() || is_category() || is_tag() ) {
$query->set( 'post_type', array( 'post', 'house' ) );
}
}

return $query;
}

Best regards,
Bruce

The topic ‘[Closed] custom post list doesn't appear in a category-slug.php file.’ is closed to new replies.