0
votes

Building my first custom theme in Wordpress and I made a custom query to only display one category on a custom category page

Using: $cat_posts = new WP_Query("category_name=deep-thoughts-page");

I no longer have pagination. My query works perfect to create a custom category page for my posts labeled with the category 'deep-thoughts-page', it also works perfectly everywhere else.

I understand this query, I like this query, and it works, I just need my pagination back.

Note: I am using wp-admin panel to limit number of posts per page (which works fine with my query), but I still lose pagination.

Please help:)

3
Define category id instead of category name .kuldip Makadiya
Some example code and output would be useful to help figure out the root of the problem - instead of just a quick fix. Since your using a custom query you may need to reset the post metadata afterwards: codex.wordpress.org/Function_Reference/wp_reset_postdataWilliam Patton

3 Answers

0
votes

I think you had define wrong in wp_query instead of category name you should apply category id on that. then it will work for you and also apply pagination to category page .

please see below code it will use for you. $query = new WP_Query( 'cat=1,2,3,4,5&posts_per_category=5' );

0
votes

Would this work for you ?

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat_posts->query( 'showposts=10&category_name=deep-thoughts-page&paged='.$paged );

In your page implementation if you need to rebuild the pagination you can do this:

<?php
while ($cat_posts->have_posts()) : $cat_posts->the_post();

    // display the posts...

endwhile;

// Now rebuild the pagination
?>

<?php if ($cat_posts->max_num_pages > 1) : ?>
    <div class="pager">

        <?php if ($paged > 1) : ?>
            <a href="<?php echo get_category_link($category_id).'page/1'; ?>">Previous</a>
        <?php endif; ?>

        <?php for ($i=1; $i<=$cat_posts->max_num_pages; $i++) : ?>
            <a href="<?php echo get_category_link($category_id).'page/'.$i;?>" <?php echo ($paged==$i)? 'class="active"':'';?>><?php echo $i;?></a>
        <?php endfor; ?>

        <?php if ($paged != $cat_posts->max_num_pages) : ?>
            <a href="<?php echo get_category_link($category_id).'page/'.$i; ?>">Next</a>
        <?php endif; ?>
    </div>
<?php endif; ?>
0
votes

Hey using below code you can easily create a pagination.

if ( !function_exists( 'matword_pagination' ) ) :
/**
* Add number paggination
**/
function matword_pagination() {

    $prev_arrow = is_rtl() ? '&rarr;' : '&larr;';
    $next_arrow = is_rtl() ? '&larr;' : '&rarr;';

    global $wp_query;
    $total = $wp_query->max_num_pages;
    $big = 999999999; // need an unlikely integer
    if( $total > 1 )  {
         if( !$current_page = get_query_var('paged') )
             $current_page = 1;
         if( get_option('permalink_structure') ) {
             $format = 'page/%#%/';
         } else {
             $format = "&paged=%#%";
         }
        echo paginate_links(array(
            'base'          => htmlspecialchars_decode(  str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ) ),
            'format'        => $format,
            'current'       => max( 1, get_query_var('paged') ),
            'total'         => $total,
            'mid_size'      => 3,
            'type'          => 'list',
            'prev_text'     => $prev_arrow,
            'next_text'     => $next_arrow,
         ) );
    }
}
endif;

just add this code into functions.php file and call "matword_pagination()" function where you want to display pagination.

for more ddetails read this article.