WordPress.org

Ready to get started?Download WordPress

Forums

Pagination with custom post types and WP_Query(); (5 posts)

  1. endless-loving
    Member
    Posted 10 months ago #

    Hi there,

    I am trying to get my custom post types paged but with the result that there is no navigation on the bottom of the list:

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
    	// display posts
    	$args = array(
    		'post_type' => 'review-module',
    		'post_status' => 'publish',
    		'posts_per_page' => $quotes_quantity,
    		'paged' => $paged,
    		'reviews' => $quotes_taxonomy,
    		'orderby' => $quotes_order, // rand or date
    		'order' => 'ASC'
    	);
    	$queryObject = new WP_Query($args);
    
    	if ($queryObject->have_posts()) :
    		while ($queryObject->have_posts()) :
    			$queryObject->the_post();

    This could be due to my usage of this script for the pagination, which actually worked in earlier versions of WP.

    // advanced Pagination ################################
    function emm_paginate($args = null) {
    	$defaults = array(
    		'page' => null, 'pages' => null,
    		'range' => 3, 'gap' => 3, 'anchor' => 1,
    		'before' => '<hr /><div class="emm-paginate">', 'after' => '</div>',
    		'title' => __('Pages:'),
    		'nextpage' => __('&raquo;'), 'previouspage' => __('&laquo'),
    		'echo' => 1
    	);
    
    	$r = wp_parse_args($args, $defaults);
    	extract($r, EXTR_SKIP);
    
    	if (!$page && !$pages) {
    		global $wp_query;
    
    		$page = get_query_var('paged');
    		$page = !empty($page) ? intval($page) : 1;
    
    		$posts_per_page = intval(get_query_var('posts_per_page'));
    		$pages = intval(ceil($wp_query->found_posts / $posts_per_page));
    	}
    
    	$output = "";
    	if ($pages > 1) {
    		$output .= "$before<span class='emm-title'>$title</span>";
    		$ellipsis = "<span class='emm-gap'>...</span>";
    
    		if ($page > 1 && !empty($previouspage)) {
    			$output .= "<a href='" . get_pagenum_link($page - 1) . "' class='emm-prev'>$previouspage</a>";
    		}
    
    		$min_links = $range * 2 + 1;
    		$block_min = min($page - $range, $pages - $min_links);
    		$block_high = max($page + $range, $min_links);
    		$left_gap = (($block_min - $anchor - $gap) > 0) ? true : false;
    		$right_gap = (($block_high + $anchor + $gap) < $pages) ? true : false;
    
    		if ($left_gap && !$right_gap) {
    			$output .= sprintf('%s%s%s',
    				emm_paginate_loop(1, $anchor),
    				$ellipsis,
    				emm_paginate_loop($block_min, $pages, $page)
    			);
    		}
    		else if ($left_gap && $right_gap) {
    			$output .= sprintf('%s%s%s%s%s',
    				emm_paginate_loop(1, $anchor),
    				$ellipsis,
    				emm_paginate_loop($block_min, $block_high, $page),
    				$ellipsis,
    				emm_paginate_loop(($pages - $anchor + 1), $pages)
    			);
    		}
    		else if ($right_gap && !$left_gap) {
    			$output .= sprintf('%s%s%s',
    				emm_paginate_loop(1, $block_high, $page),
    				$ellipsis,
    				emm_paginate_loop(($pages - $anchor + 1), $pages)
    			);
    		}
    		else {
    			$output .= emm_paginate_loop(1, $pages, $page);
    		}
    
    		if ($page < $pages && !empty($nextpage)) {
    			$output .= "<a href='" . get_pagenum_link($page + 1) . "' class='emm-next'>$nextpage</a>";
    		}
    
    		$output .= $after;
    	}
    	if ($echo) {
    		echo $output;
    	}
    	return $output;
    }
    
    function emm_paginate_loop($start, $max, $page = 0) {
    	$output = "";
    	for ($i = $start; $i <= $max; $i++) {
    		$output .= ($page === intval($i))
    			? "<span class='emm-page emm-current'>$i</span>"
    			: "<a href='" . get_pagenum_link($i) . "' class='emm-page'>$i</a>";
    	}
    	return $output;
    }

    So the output of my navigation code on the bottom of my custom post type list looks like this:

    // get pagination function from the functions.php
    if (function_exists("emm_paginate")) emm_paginate();

    Any idea what I am doing wrong?
    Any help is much appreciated.

  2. keesiemeijer
    moderator
    Posted 10 months ago #

    Try adding the pages and page argument when using the function (with your custom query object $queryObject):

    <?php
    // set the "paged" parameter (use 'page' if the query is on a static front page)
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array(
        'page' => $paged,
        'pages' => $queryObject->max_num_pages,
    );
    ?>
  3. endless-loving
    Member
    Posted 10 months ago #

    Thanks keesiemeijer,
    unfortunately that did not help to get this pagination going.
    Do you have an idea if this function 'emm_paginate' is still a good choice in regards of the current WP version?
    It looks quite smart to me:
    http://www.ericmmartin.com/pagination-function-for-wordpress/
    But I can't get it rollin. :/

    The plugin version of this script is here:
    http://wordpress.org/plugins/wp-paginate/
    It seems that it is not up to date anymore.

  4. keesiemeijer
    moderator
    Posted 10 months ago #

    Can you post the full code of the template file with the query - see the Forum Rules for posting code and using the pastebin.

  5. endless-loving
    Member
    Posted 10 months ago #

    Thank you.
    I decided to simplify my code for testing purposes.
    I am including the code in my page.php (http://pastebin.com/WhiMmHcg).

    In my page php the include looks like this:

    ...
    // 'Quotes' page
    case $quotes_page :
    // page with quotes from the 'Quotes/Reviews' section of the website
    $quotes_include = 'content'; // for later use in the quotes snippet
    include 'wp-content/themes/love/extras/quotes-snippet.php';
    break;
    ...

    This is what I got - but it does not show any pagination:

    // set the "paged" parameter (use 'page' if the query is on a static front page)
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array(
    		'page' => $paged,
    		'pages' => $queryObject->max_num_pages,
    		'post_type' => 'review-module',
    		'post_status' => 'publish',
    		'posts_per_page' => $quotes_quantity,
    		'reviews' => $quotes_taxonomy,
    		'orderby' => $quotes_order, // rand or date
    		'order' => 'ASC'
    	);
    	$queryObject = new WP_Query($args);
    
    	if ($queryObject->have_posts()) :
    		while ($queryObject->have_posts()) :
    			$queryObject->the_post();
    
    the_title();
    
    endwhile;
       // get pagination function from the functions.php
    if (function_exists("emm_paginate")) emm_paginate();
    endif;

    I have also tried this code:

    global $paged;
    $curpage = $paged ? $paged : 1;
    $args = array(
        'post_type' => 'testimonials',
        'orderby' => 'post_date',
        'posts_per_page' => 5,
        'paged' => $paged
    );
    $query = new WP_Query($args);
    if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
    ?>
    <div id="post-<?php the_ID(); ?>" class="quote">
    <?php
    echo get_the_post_thumbnail($id, array($image_width,$image_height));
    the_content();
    ?>
    </div>
    <?php
    endwhile;
        echo '
        <div id="wp_pagination">
            <a class="first page button" href="'.get_pagenum_link(1).'">«</a>
            <a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
            for($i=1;$i<=$query->max_num_pages;$i++)
                echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
            echo '
            <a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
            <a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
        </div>
        ';
        wp_reset_postdata();
    endif;
    ?>

    That one shows at least a navigation but each link just keeps me on the same page.

Reply

You must log in to post.

About this Topic