Custom archives function

I’m working on building a custom archives page for my church’s website. On my archives page I want to have last week’s sermon shown at the top in a special box, followed by the future sermons and then finally the Sermon Archives.

I’d like the future sermons and Sermon archives to be separated by a title… i.e.

Read More

Future Sermons

the future sermons…

Sermon Archives

the sermon archives…

My problem is when I click the button to go to older Sermon posts with the pagination function, it displays the current and future sermons on every archives page I’m on!

How do I get the current sermon and future sermons to only display on page 1, and not when I click to view older posts?

Here are my functions:

// Create the loop for the sermons page
function sermon_posts() {
    if(is_page('sermons')) {
        current_sermon_archives(); ?>
        <h2 class="sm-header">Future Sermons</h2>
        <?php future_sermon_archives(); ?>
        <h2 class="sm-header">Sermon Archives</h2>
        <?php sermon_archives(); 
    } 
}

function current_sermon_archives() {
    $thisSunday = date('d', strtotime('last Sunday'));
    $thisSundayMonth = date('n', strtotime('last Sunday'));
        $lastWeekSermon = new WP_Query(array( 'posts_per_page' => 1,
                                        'post_type' => 'sermon_post',
                                        'monthnum' => $thisSundayMonth,
                                        'day' => $thisSunday) );

if ( $lastWeekSermon->have_posts() ) : while ( $lastWeekSermon->have_posts() ) : $lastWeekSermon->the_post();  ?>

<div id="sm-upcoming-archives">
    <h2 class="sm-header">Last Week's Sermon</h2>
        <div class="scripture-post">
            <h2><p><?php the_title(); ?></p><span class="sm-date"> - <?php the_time(get_option('date_format')); ?></span></h2>

                    <?php echo get_the_term_list( get_the_ID(), 'speaker' ) ?>
<?php the_content(); ?>
<div class="sermonexcerpt">
                        <?php $thereisdescription = get_post_meta($post->ID, "_description", true);
                        if($thereisdescription){ ?>
                            <?php echo get_post_meta($post->ID, "_description", true); ?>
                        <?php }else{ ?>
                        <?php } ?></div>

    </div>
</div>
    <?php endwhile; else: ?>
        <?php wp_reset_postdata(); ?>
    <?php endif; 
}

function future_sermon_archives() {
    $current_year = date(' Y', strtotime('next Sunday'));
    $current_month = date('M ', strtotime('next Sunday'));
    $current_day = date('d', strtotime('next Sunday'));
    $futureSermon = new WP_Query(array( 'showposts' => '3',
                                            'post_type' => 'sermon_post',
                                            'monthnum' => $current_month,
                                            'day' => $current_day) );

        if ( $futureSermon->have_posts() ) : while ( $futureSermon->have_posts() ) : $futureSermon->the_post();  ?>

<div id="sm-upcoming-archives">
    <h2 class="sm-header">Last Week's Sermon</h2>
        <div class="scripture-post">
            <h2><p><?php the_title(); ?></p><span class="sm-date"> - <?php the_time(get_option('date_format')); ?></span></h2>

                    <?php echo get_the_term_list( get_the_ID(), 'speaker' ) ?>
<?php the_content(); ?>
<div class="sermonexcerpt">
                        <?php $thereisdescription = get_post_meta($post->ID, "_description", true);
                        if($thereisdescription){ ?>
                            <?php echo get_post_meta($post->ID, "_description", true); ?>
                        <?php }else{ ?>
                        <?php } ?></div>

    </div>
</div>
    <?php endwhile; else: ?>
        <?php wp_reset_postdata(); ?>
    <?php endif; 
}

function sermon_archives() {
    query_posts(array('post_type'=>'sermon_post', 'paged' => get_query_var( 'paged' ) ) ); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="scripture-post">
            <h2><p><?php the_title(); ?></p><span class="sm-date"> - <?php the_time(get_option('date_format')); ?></span></h2>

                    <?php echo get_the_term_list( get_the_ID(), 'speaker' ) ?>
<?php the_content(); ?>
<div class="sermonexcerpt">
                        <?php $thereisdescription = get_post_meta($post->ID, "_description", true);
                        if($thereisdescription){ ?>
                            <?php echo get_post_meta($post->ID, "_description", true); ?>
                        <?php }else{ ?>
                        <?php } ?></div>

</div>

        <?php endwhile; else: ?>
    <?php endif; ?>
<div class="navigation">
                <?php if (function_exists("pagination")) {                       pagination($additional_loop->max_num_pages);
                        } ?></div>
<?php }

I know it looks like a lot of code, but really they are all pretty similar functions…

How do I get the current sermon and future sermons to only display on page 1, and not when I click to view older posts?

Related posts

Leave a Reply

1 comment

  1. You want is_paged() (see Codex). This returns true if you are on page 2,3, etc and false if you are on the first page. So…

    if(!is_paged()){
         //Display current / future sermons
    }
    
    //Display archived sermons.