I created a similar question about an hour ago but since then I feel that I've changed things enough so that if someone viewed it for the first time, it'd just be a confusing mess. So I am opening up a new question that is a bit more clear and relevant to my issue.
Basically, I have a custom post type called podcasts
and I have created an archive page for this post type by creating a new file called archive-podcasts.php
. I have made this page into a custom template and have set it as the front page via Settings > Reading
in the dashboard.
However, even after doing this, the archive page wasn't showing as the front page, so I did a little research and found this gem to drop in my functions.php
which finally allowed the archive of posts to show up on the front page.
add_action("pre_get_posts", "custom_front_page");
function custom_front_page($wp_query){
//Ensure this filter isn't applied to the admin area
if(is_admin()) {
return;
}
if($wp_query->get('page_id') == get_option('page_on_front')):
$wp_query->set('post_type', 'podcasts');
$wp_query->set('page_id', ''); //Empty
//Set properties that describe the page to reflect that
//we aren't really displaying a static page
$wp_query->is_page = 0;
$wp_query->is_singular = 0;
$wp_query->is_post_type_archive = 1;
$wp_query->is_archive = 1;
endif;
}
Awesome, but not quite finished yet. Below is what my archive-podcasts.php
page looks like:
<?php
/*
Template Name: Podcasts
*/
?>
<?php get_header();?>
<section id="content">
<section id="latest-blogs">
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array( 'posts_per_page' =>5, 'post_type'=> 'podcasts', 'paged' => $paged );
$postslist = new WP_Query( $args );
if ( $postslist->have_posts() ) :
while ( $postslist->have_posts() ) : $postslist->the_post(); ?>
<div class="article-wrapper">
<article id="post-<?php the_ID(); ?>">
<time datetime="<?php the_time('c'); ?>"><?php the_time('F j, Y'); ?></time>
<?php
$custom = get_post_custom($post->ID);
$buzzsprout_code = $custom["buzzsprout_code"][0];
echo do_shortcode($buzzsprout_code);
?>
<p class="read-emails"><a href="<?php the_permalink() ?>" rel="bookmark" title="View emails and comment on this episode.">View emails and comment on this episode</a></p>
</article>
</div>
<?php endwhile; ?>
<div id="pagination">
<?php my_paginate_links(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</section>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You'll notice that I'm calling the function my_paginate_links()
. However, calling this function is screwy because it doesn't paginate the archive of posts correctly. When I click on a page number, it goes to the correct page, but the current
CSS class doesn't match up.
Here is what my_paginate_links()
looks like:
function my_paginate_links() {
global $wp_rewrite, $wp_query;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
'base' => @add_query_arg('page','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'prev_text' => __('« previous'),
'next_text' => __('next »'),
'end_size' => 1,
'mid_size' => 2,
'show_all' => true,
'type' => 'list'
);
if ( $wp_rewrite->using_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
if ( !empty( $wp_query->query_vars['s'] ) )
$pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );
}
Can someone help me figure out why the pagination isn't working? Also, is there a better or more efficient way of going about all of this?
Also, I should note that the pagination works fine on my main query for my regular posts. They just don't work for this archive of custom post type posts. Is there a way to get the pagination to work for both instances?
Edit: Updated the pagination function
function my_paginate_links() {
global $wp_rewrite, $postslist;
$postslist->query_vars['page'] > 1 ? $current = $postslist->query_vars['page'] : $current = 1;
$pagination = array(
'base' => @add_query_arg('page','%#%'),
'format' => '',
'total' => $postslist->max_num_pages,
'current' => $current,
'prev_text' => __('« previous'),
'next_text' => __('next »'),
'end_size' => 1,
'mid_size' => 2,
'show_all' => true,
'type' => 'list'
);
if ( $wp_rewrite->using_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
if ( !empty( $postslist->query_vars['s'] ) )
$pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );
}