WordPress.org

Ready to get started?Download WordPress

Forums

Custom post and category template pagination problem (8 posts)

  1. George Georgiev
    Member
    Posted 8 months ago #

    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');

  2. George Georgiev
    Member
    Posted 8 months ago #

    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.

  3. keesiemeijer
    moderator
    Posted 8 months ago #

    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' );
  4. George Georgiev
    Member
    Posted 8 months ago #

    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!

  5. George Georgiev
    Member
    Posted 8 months ago #

    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...

  6. George Georgiev
    Member
    Posted 8 months ago #

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

  7. George Georgiev
    Member
    Posted 8 months ago #

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

  8. George Georgiev
    Member
    Posted 8 months ago #

    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.

Reply

You must log in to post.

About this Topic