php - Custom post type with pagenavigation not working -
i have been struggling night make work, seems can't find problem. i'm using bootstrap make new theme, , along it, have small piece of code custom page navigation, , seems work fine basic wordpess post list.
but have added new custom post type called gallery, , can't find solution on how list posts in gallery custom post type , make pagination work. far, have made new file called archive-galleries.php, , lists posts gallery custom post type, won't go second page. error 404 not found
.
here code of archive-galleries.php page
<?php get_header(); ?> <?php get_sidebar('top'); ?> <div class="row"> <div class="span8"> <?php the_post(); ?> <h3 class="text-error"><?php _e('latest image galleries', 'vt-translate'); ?></h3> <?php $count = 0; $paged = intval(get_query_var('paged')); $paged = ($paged) ? $paged : 1; $query_arguments = array( 'post_type' => 'galleries', 'posts_per_page' => 4, 'paged' => $paged, ); // query posts query_posts( $query_arguments ); while ( have_posts() ) : the_post(); $open = !($count%4) ? '<div class="row">' : ''; $close = !($count%4) && $count ? '</div>' : ''; echo $close.$open; ?> <div class="span2"> <a href="<?php the_permalink() ?>"> <img class="thumb" data-src="holder.js/160x250" alt="<?php the_title(); ?>" src="<?php bloginfo('template_url'); ?>/inc/timthumb.php?src=<?php echo get_post_meta($post->id, 'thumb', true); ?>&h=250&w=160&zc=1" /> </a> <div class="clearfix"></div> <h6> <a href="<?php the_permalink() ?>"><?php trim_title(); ?></a> </h6> </div> <?php $count++; endwhile; echo $count ? '</div>' : ''; ?> </div> <div class="span4"> <?php get_sidebar('pageright'); ?> </div> </div> <div class="pagination pagination-centered"> <?php page_navi(); ?> </div><!-- #nav-below --> <?php get_sidebar('belownavigation'); ?> <?php get_footer(); ?>
and here custom code used pagination, located in functions.php file:
//wordpress page navigation numbers function page_navi($before = '', $after = '') { global $wpdb, $wp_query; $request = $wp_query->request; $posts_per_page = intval(get_query_var('posts_per_page')); $paged = intval(get_query_var('paged')); $numposts = $wp_query->found_posts; $max_page = $wp_query->max_num_pages; if ( $numposts <= $posts_per_page ) { return; } if(empty($paged) || $paged == 0) { $paged = 1; } $pages_to_show = 7; $pages_to_show_minus_1 = $pages_to_show-1; $half_page_start = floor($pages_to_show_minus_1/2); $half_page_end = ceil($pages_to_show_minus_1/2); $start_page = $paged - $half_page_start; if($start_page <= 0) { $start_page = 1; } $end_page = $paged + $half_page_end; if(($end_page - $start_page) != $pages_to_show_minus_1) { $end_page = $start_page + $pages_to_show_minus_1; } if($end_page > $max_page) { $start_page = $max_page - $pages_to_show_minus_1; $end_page = $max_page; } if($start_page <= 0) { $start_page = 1; } echo $before.'<ul>'.""; if ($paged > 1) { echo '<li class="prev"><a href="'.get_pagenum_link().'" title="first">'.__('«','vt-translate').'</a></li>'; } $prevposts = get_previous_posts_link('« previous'); if($prevposts) { echo '<li>' . $prevposts . '</li>'; } else { echo '<li class="active"><a href="#">« previous</a></li>'; } for($i = $start_page; $i <= $end_page; $i++) { if($i == $paged) { echo '<li class="active"><a href="#">'.$i.'</a></li>'; } else { echo '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>'; } } echo '<li class="">'; next_posts_link('next »'); echo '</li>'; if ($end_page < $max_page) { echo '<li class="next"><a href="'.get_pagenum_link($max_page).'" title="last">'.__('»','vt-translate').'</a></li>'; } echo '</ul>'.$after.""; }
i have found solution, or it's kinda workaround, reason custom post type archive doesn't work same way default archive.
so workaround main problem permalinks, had use different custom post type name , custom page template.
in functions.php file have code register custom post type called galleries. since wanted use galleries list posts name page , had add rewrite rule custom post type function register custom post type looks this.
function my_custom_post_product() { $labels = array( 'menu_name' => 'galleries', 'all_items' => __( 'all posts' ), ); $args = array( 'labels' => $labels, 'description' => 'add image galleries custom posts', 'public' => true, 'exclude_from_search' => true, 'show_in_nav_menus' => true, 'show_in_menu' => true, 'capability_type' => 'post', 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ), 'has_archive' => true, 'rewrite' => array('slug' => 'gallery'), '_builtin' => false ); register_post_type( 'galleries', $args ); } add_action( 'init', 'my_custom_post_product' );
so instead of galleries custom post use gallery slug.
now list posts in gallery have used code archive-galleries.php page , created new page template called page-galleries.php , added code archive-galleries.php.
now created new page, selected newly created galleries template , saved page. added new page menu , works.
there big debate on internet how make pagination work custom post type , reason not works regular archive works, if it's intended work native archive-custom_post_type_slug.php working workaround have crafted self , main problem many on internet overlooked (like did also) can't name page same post type slug, , archive won't work permalinks default set.
Comments
Post a Comment