• Hi guys,

    i have a strange problem in my wordpress. I have a category template wich holds a query to custom post with a the category. The strange thing is when the pagination loads it displays how many pages we have. But when i click on the second page of the pagination it gives me a 404 page. If I make the query only to the custom post or to the category it works fine. After i have refresh the query with the test for the custom post and the category it works fine. Here is the code of the category template:

    <div id="conteiner-first-vision">
      <section id="inner-pad">
        <?php
     	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array(
    	'posts_per_page' => 6,
    	'post_type' =>  'awards-pub',
    	'cat'=>6,
    	'paged' => $paged,
    	'order'=>'DESC'
    	);
    	$wp_query = new WP_Query( $args );
    	//wp_query->query('showposts=5'.'&paged='.$paged);
    	$i=1;
    	while ( $wp_query->have_posts() )
    	{
    		$wp_query->the_post();
    		?>
        <article class="article-first-vision <?= ($i==2)?'mar':'' ?>" >
          <?php if ( has_post_thumbnail()) { ?>
          <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
          <?php the_post_thumbnail('menu-pub'); ?>
          </a>
          <?php
    	}; ?>
          <h2>
            <?php the_title(); ?>
          </h2>
          <div class="article-description">
            <?= truncateHtml(get_the_content()); ?>
          </div>
          <a href="<?php the_permalink(); ?>" style="bottom:0; right:0;" class="pa"><img src="<?php bloginfo('template_url') ?>/img/link-articles.png"></a> </article>
        <?php
      if($i==3){$i=0;}
      $i++;
    	}
    	?>
      </section>
      <div class="clear"></div>
      <div class="pagination-conteiner">
        <?php
      	get_template_part('pagination');
    	?>
      </div>
      <div class="clear"></div>
      <?php
    	wp_reset_query();
      ?>

    And this is the pagination:

    global $wp_query;
        $big = 999999999;
        echo paginate_links(array(
            'base' => str_replace($big, '%#%', get_pagenum_link($big)),
            'format' => '?paged=%#%',
            'current' => max(1, get_query_var('paged')),
            'total' => $wp_query->max_num_pages,
    		'prev_next'    => True,
    		'prev_text'    => __('«'),
    		'next_text'    => __('»'),
        ));

    and this is the custom post:

    register_post_type('awards-pub', // Register Custom Post Type
            array(
            'labels' => array(
                'name' => __('Awards', 'awardspub'), // Rename these to suit
                'singular_name' => __('Awards Post', 'awardspub'),
                'add_new' => __('Add Awards', 'awardspub'),
                'add_new_item' => __('Add Awards  Post', 'awardspub'),
                'edit' => __('Edit', 'awardspub'),
                'edit_item' => __('Edit Awards Post', 'awardspub'),
                'new_item' => __('New Awards Post', 'awardspub'),
                'view' => __('View Awards Post', 'awardspub'),
                'view_item' => __('View Awards Post', 'awardspub'),
                'search_items' => __('Search Awards Post', 'awardspub'),
                'not_found' => __('No Awards Posts found', 'awardspub'),
                'not_found_in_trash' => __('No Awards Posts found in Trash', 'awardspub')
            ),
            'public' => true,
            'hierarchical' => true, // Allows your posts to behave like Hierarchy Pages
            'has_archive' => true,
            'supports' => array(
                'title',
                'editor',
                'excerpt',
    			'comments',
                'thumbnail'
            ), // Go to Dashboard Custom HTML5 Blank post for supports
            'can_export' => true, // Allows export in Tools > Export
            'taxonomies' => array(
                'post_tag',
                'category'
            ) // Add Category and Post Tags support
        ));
    
    And to add the category to the custom post:

    register_taxonomy_for_object_type(‘category’, ‘awards-pub’);
    register_taxonomy_for_object_type(‘post_tag’, ‘awards-pub’);

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter George Georgiev

    (@george-georgiev)

    I have found that it start to work if i go to the posts and make more then the post_per_page post with that category.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it without the query in the category template, but with a query in your functions.php:
    https://codex.wordpress.org/Pagination#Removing_query_posts_from_the_main_loop

    function my_post_queries( $query ) {
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    
    		// use post type awards-pub on category 6 pages
    		if ( is_category( 6 ) ) {
    			$query->set( 'posts_per_page', 6 );
    			$query->set( 'post_type', array( 'awards-pub' ) );
    		}
    
    	}
    }
    add_action( 'pre_get_posts', 'my_post_queries' );
    Thread Starter George Georgiev

    (@george-georgiev)

    The point is that when it has only the category it works fine but when we include the custom posts it has the problem. keesiemeijer i will try this. Thanks!

    Thread Starter George Georgiev

    (@george-georgiev)

    No this generate the same thing… What can be the problem? I did not understand when i create post in the posts and attach to them the category it works really fine…

    Thread Starter George Georgiev

    (@george-georgiev)

    The same template i have duplicated in the page template and it works perfekt.

    Thread Starter George Georgiev

    (@george-georgiev)

    I also want to point that the template is category-slug.

    Thread Starter George Georgiev

    (@george-georgiev)

    Hi this is the solution to my problem

    add_action( ‘parse_query’,’changept’ );
    function changept() {
    if( is_category() && !is_admin() )
    set_query_var( ‘post_type’, array( ‘menu-pub’, ‘new-pub’, ‘read-pub’, ‘awards-pub’, ‘live-pub’,’post’ ));
    return;
    }

    And put it in the functions file.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom post and category template pagination problem’ is closed to new replies.