Search code examples
phpwordpresspaginationgenesis

Custom post type pagination issues


Having trouble implementing the pagination for custom post type ('vehicle') in Genesis child theme, I'm using Genesis : 2.0.0 in wordpress 4.1.1

I've tried placing the following code in function.php add_filter( 'genesis_prev_link_text', 'gt_review_prev_link_text' );

function gt_review_prev_link_text() {
        $prevlink = '« Previous Reviews';
        return $prevlink;
}
add_filter( 'genesis_next_link_text', 'gt_review_next_link_text' );
function gt_review_next_link_text() {
        $nextlink = 'Next Reviews »';
        return $nextlink;
} 

i've tried the following too;

<?php genesis_posts_nav();?>

The code below works, but only as 'newer' and 'older' hyperlinks, i'm trying to achieve a numeric pagination.

<?php previous_posts_link( '« Newer' ); ?>
<?php next_posts_link( 'Older »', $vehicle->max_num_pages ); ?>

Here is my custom post type archive template.

<?php
/*
Template Name: Archive Vehicle(CAR)
*/
//* Force full width content
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//* Remove Post Info
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );

//* Remove Post Meta
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_open', 5 );
remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_close', 15 );
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
 /** Code for custom loop */

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'am_custom_loop' );
function am_custom_loop(){
$paged = 1;
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
if ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
$paged = intval( $paged );
$args = array('post_type' => 'vehicle', 'posts_per_page' => '3', 'paged'=> $paged, );
   $vehicle = new WP_query($args);
$wp_query = $vehicle;
                if ($vehicle -> have_posts() ){
?>
<div class="first arc">
<h2> View our latest used car collection from KH cars Birmingham</h2>
<p>Need a car?  Look no further, browse through our vast selection of used cars on site based in Birmingham.  </p>
</div>
<?php
                while ($vehicle -> have_posts() ){
                $vehicle -> the_post();

?>

<div class="first archivecar">
    <div class="one-fourth">  
<?php 
$images = get_field('image');
    if( $images ): 
        $image = $images[0];
?>                
            <a href="<?php the_permalink($id); ?>">
                <img src="<?php echo $images[0]['sizes']['large']; ?>" alt="<?php echo $image['alt']; ?>" />
            </a> 
<?php endif; ?>
         </div>
       <div class="two-fourths">
<br>
            <h3><a  style="text-decoration:none;" href="<?php the_permalink($id) ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<ul class="carinfo">
    <li><p> Year: <?php $terms = get_the_terms( $post->ID , 'year', 'year' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?></p></li>
    <li><p> Trans: <?php $terms = get_the_terms( $post->ID , 'trans', 'trans' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?></p></li>
   <li><p> Fuel: 
  <?php   // Get terms for post
 $terms = get_the_terms( $post->ID , 'fuel', 'fuel' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?></li>
 <?php if( get_field('Mileage') ): ?>
    <li><p> Mileage: <?php the_field('Mileage');?></p></li>
 <?php endif; ?>
    <li><p>Color: <?php $terms = get_the_terms( $post->ID , 'colours', 'colours' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?></p></li>
 <?php if( get_field('owners') ): ?>
    <li><p>Owners: <?php $terms = get_the_terms( $post->ID , 'owners', 'owners' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} }?></p></li>
 <?php endif; ?>
</ul>
</div>
<div class="one-fourth">

        <?php if ( get_field('price')):?>
    <p class="price"><?php the_field('price');?></p>
    <?php endif; ?>
<a class="btn" style="text-align:center" href="<?php the_permalink() ?>">view Details</a>
</div>
</div>
<?php
}
?>
<?php genesis_posts_nav();?>
<?php wp_reset_query();?>
<?php
}
}
genesis();

Any Help is much appreciated. Thanks in advance


Solution

  • First i Removed $wp_query = $vehicle; and then used the code below from wordpress codex and voila it works.

    <?php
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $vehicle->max_num_pages
    ) );
    ?>