0

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' => __('&laquo previous'),
        'next_text' => __('next &raquo;'),
        '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' => __('&laquo previous'),
        'next_text' => __('next &raquo;'),
        '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 );
}

1 Answer 1

2

Between my phone and work some errors creeped in, sorry about that. here is my revised answer. Hope this helps in solving your problem.

PROBLEM 1

You should not name your template 'archive-{$post_type}.php' if it is a page template. This totally goes against the template hierarchy for pages, and that is why your static frontpage doesn't work. From the codex

A static page: WordPress uses the Static Page template hierarchy: Custom Page Template, page-{id}.php, page-{slug}.php, page.php, index.php

SOLUTION TO PROBLEM 1

You should consider names like front-page.php which is the appropriate name for a frontpage template or something like page-podcasts.php which falls within the hierarchy for page templates. archive-{$post_type}.php is exclusively used for archive pages of custom post types. Consider having a look at Theme Developments as well

You can remove the pre_get_posts function as this will not be necessary anymore if you change your template name.

PROBLEM 2

When using WP_Query to create a custom query, and you need pagination, you will need to make use of the pagination parameters within WP_Query. This parameter differ for static front pages. Where paged is usually used, front pages make use of page

page (int) - number of page for a static front page. Show the posts that would normally show up just on page X of a Static Front Page.

SOLUTION TO PROBLEM 2

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

should be changed to

$paged = (get_query_var('page')) ? get_query_var('page') : 1;

This line in your array that you pass to WP_Query is correct. No need to change this

'paged' => $paged

PROBLEM 3

Your pagination function should also be adjusted accordingly to your custom query. Two things jump up here for me, your max_num_pages argument is not set for your custom query, paged should be page for frontpage, and you are using the variable $wp_query. Your variable set for your custom query is $postslist, so you should adjust your pagination function according to your variable.

SOLUTION TO PROBLEM 3

I came across this ready-made function from @ewroman that combines a custom query and the normal main query into one pagination function, so you will not need to create two functions just for different paginated queries. I've tested this with your custom post page and it works great. You can just modify this function even further

if( ! function_exists( 'my_paginate_links' ) ) {
    function my_paginate_links() {
        global $wp_query, $postslist;

        $big = 999999999; // This needs to be an unlikely integer
        if ( is_front_page()) {
            $myqueryis = $postslist;
            $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
            } else {
            $myqueryis = $wp_query;
            $paged = get_query_var('paged');
            }
        // For more options and info view the docs for paginate_links()
        // http://codex.wordpress.org/Function_Reference/paginate_links
        $paginate_links = paginate_links( array(
            'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
            'current' => max( 1, $paged ),
            'total' => $myqueryis->max_num_pages,
            'mid_size' => 5,
            'prev_next' => True,
            'prev_text' => __('&laquo previous'),
            'next_text' => __('next &raquo;'),
            'type' => 'list'
        ) );

        // Display the pagination if more than one page is found
        if ( $paginate_links ) {
            echo '<div class="pagination-centered">';
            echo $paginate_links;
            echo '</div><!--// end .pagination -->';
        }
    }
}

From what I could see from your pagination function also that I did not mention, your current parameter seemed to also be the big culprit here

ALTERNATIVE SOLUTION TO PROBLEM 3

You can maybe also have a look at next_posts_link and previous_posts_link. Just remember here as well, for custom queries, the $max_pages parameter in next_posts_link should be set for pagination to work correctly.

next_posts_link( 'Older Entries', $postslist->max_num_pages );
3
  • What happened to our dialogue here? All of our posts have disappeared.
    – Desi
    Jul 14, 2014 at 10:55
  • Please check my updated answer. This should solve your problem now :-) Jul 14, 2014 at 16:08
  • My pleasure, glad we got this solved. Enjoy :-) Jul 15, 2014 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.