Group Admins

  • Profile picture of Steve

WordPress Troubleshooting and Support

Public Group active 3 months, 2 weeks ago ago

WordPress support from our community

pagination in custom post types

Tagged: , ,

This topic contains 3 replies, has 2 voices, and was last updated by  Iftekhar Bhuiyan 4 years, 10 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2968

    lester
    Participant

    hi WP community!
    i’m learning custom post types (w/out plugins) and having trouble being able to click through a series of custom posts / pagination / older posts / newer posts, etc.
    i’m using the _s theme, doing tutorials from a sitepoint book.
    thought it might be my loop structure, since i read that pagination can be an issue with some queries, but both $query_posts and $WP_Query failed.
    ideas, thoughts, comments and help are appreciated.
    thanks so much!

    #2971

    Iftekhar Bhuiyan
    Participant

    Would have been better if I could see your code for Custom Post Type.
    Simply hover to this page and check various parameters for CPTs. https://codex.wordpress.org/Function_Reference/register_post_type

    Now, Copy and paste the following function (numeric_pagination) to your theme’s functions.php file and update it.
    <!———– function —————->

    function numeric_pagination() {
    	if( is_singular() )
    		return;
    		global $wp_query;
    	if( $wp_query->max_num_pages <= 1 )
    		return;
    		$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    		$max   = intval( $wp_query->max_num_pages );
    		if ( $paged >= 1 )
    			$links[] = $paged;
    		if ( $paged >= 3 ) {
    			$links[] = $paged - 1;
    			$links[] = $paged - 2; }
    		if ( ( $paged + 2 ) <= $max ) {
    			$links[] = $paged + 2;
    			$links[] = $paged + 1;	}
    		echo '<div class="navigation"><ul>' . "\n";
    		if ( get_previous_posts_link() )
    			printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
    		if ( ! in_array( 1, $links ) ) {
    			$class = 1 == $paged ? ' class="active"' : '';
    			printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
    		if ( ! in_array( 2, $links ) )
    			echo '<li>…</li>';	}
    			sort( $links );
    			foreach ( (array) $links as $link ) { $class = $paged == $link ? ' class="active"' : '';
    			printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );	}
    		if ( ! in_array( $max, $links ) ) {
    		if ( ! in_array( $max - 1, $links ) )
    			echo '<li>…</li>' . "\n";
    			$class = $paged == $max ? ' class="active"' : '';
    			printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max ); }
    		if ( get_next_posts_link() )
    			printf( '<li>%s</li>' . "\n", get_next_posts_link() );
    		echo '</ul></div>' . "\n";
    	 }
    

    <!——– function ends —————>
    Now go to the “archive-custompostytpe.php” (archive page for your custom post type)*. If you don’t have it (should have/create one if you don’t have) go to the archive-xxxxx.php page and paste the following function after <?php endwhile; ?> of your loop.
    <?php if( function_exists(‘numeric_pagination’)) { numeric_pagination(); } ?>

    You are pretty much done. However, now you need to style(with CSS) the elements(div, ul, li) that exists within the “numeric_pagination()” function. Here is the sample code for that.

    CSS Code for the pagination:
    .navigation { padding:10px 0px 10px 0px; text-align:center; overflow:hidden; }
    .navigation ul { margin:0px; padding:0px; }
    .navigation li { display:inline-block; border:0px; }
    .navigation li a { padding:6px 9px; color:#222222; font-size:12px; text-decoration:none; border:solid 1px #efefef; }
    .navigation li a, .navigation li a:hover, .navigation li.active a, .navigation li.disabled { display:inline-block; cursor:pointer; }
    .navigation li a:hover, .navigation li.active a { padding:6px 9px; background-color:#ee1111; border:solid 1px #ee1111; color:#ffffff; }

    Change it with your desired color / styles. Hope that helps. Thank you.
    —————– 0 ——————–
    * it requires you to set the following parameter to “true” or change it with your “cpt name” while registering your Custom Post type.
    ‘has_archive’=> urcptname,”

    #2976

    lester
    Participant

    hello Bhuiyan,
    thank you so much for your reply.
    my apologies for not responding sooner, as i didn’t recall seeing a notification for your reply post. (notify me: checked)
    i will use and study the above code, for sure. had to step away from the topic for a while to reset myself, although it was really fun making CPTs which was quite easy (if one doesn’t mind typing a bit).
    a straight copy and paste of the ‘books’ cpt from the codex worked just fine, too.
    at any rate, i can’t thank you enough 🙂
    the wp community here is always so helpful . . . . thank you!
    ps: i didn’t see anything concerning pagination at the CPT link above. nothing on pagination or get_next, get_previous_post_link() in ‘related’ at bottom . . . .

    #3029

    Iftekhar Bhuiyan
    Participant

    I am using the same function on my site and it works just fine. Simply follow the instructions that I have mentioned and let me know. Thanks.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.