• Resolved Dutchg

    (@dutchg)


    Here’s my blog. You will see, on that home page, a blog entry from a couple weeks ago, and a podcast episode, just uploaded yesterday. Those are the two categories of things I will be doing at the site: blog entries and podcast episodes. Editorially speaking, they are only tangentially related–the blog entries are not about the podcasts. I don’t want them on the same page.

    I want visitors to the site to see, by default, the blog entries only. But “Podcasts” will be a nav choice, and if it’s selected, they’ll see the podcast page, with only the podcasts listed, and the latest episode at the top. Ideally, I’d even like to be able to put a custom podcast-related header on that podcast page.

    I’ve been looking all over, trying to figure this out. I’m a relative newbie. I can handle plopping in a bit of code here and there, but I’ve not seen a description of how to do this that makes sense to me.

    I’m in theme Twenty Twelve.

    Any help appreciated.
    Dutch

Viewing 15 replies - 1 through 15 (of 24 total)
  • First you will need to exclude the podcast posts from the site’s main posts page template. In your child theme’s functions.php file, try adding:

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-9' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    where 9 is the id of the Podcast category.

    Thread Starter Dutchg

    (@dutchg)

    Thanks, Esmi. I’ll give this a shot in just a bit. Then on to phase two.

    Thread Starter Dutchg

    (@dutchg)

    Esmi, I came up against this problem: copying function.php into my child theme breaks the site. I know there’s a workaround, and am researching that now. Once I figure out how to get function.php into my child theme, I’ll resume here. thanks.

    copying function.php into my child theme breaks the site.

    Do not copy the parent’s functions.php file into the child. Just create an empty file in a plain text editor, add <?php to the top of it, save it as functions.php and upload it to your child theme.

    Thread Starter Dutchg

    (@dutchg)

    OK. I think I’m over that hump. I’ve got that exclusion code running now in the child functions.php. (It’s the only thing in that file.) Seems to be working.

    So–I suppose we’re ready for the next step. Is it creating a page for the Podcast?

    Yep. You’ll need to create a custom page template in your child theme. I’d suggest starting with:

    <?php
    /*
    Template Name: Podcasts
    */
    get_header(); ?>
    
    <div id="primary" class="site-content">
    <div id="content" role="main">
    
    <?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'content', 'page' ); ?>
    <?php comments_template( '', true ); ?>
    <?php endwhile; // end of the loop. ?>
    
    </div><!-- #content -->
    </div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer();

    Call this new file podcasts.php. Then edit functions.php again and try adding:

    function only_category( $query ) {
        if ( $query->is_page_template( 'podcasts.php' ) && $query->is_main_query() ) {
            $query->set( 'cat', '9' );
        }
    }
    add_action( 'pre_get_posts', 'only_category' );

    [EDITED]

    Thread Starter Dutchg

    (@dutchg)

    Excellent, thank you. Let me see if I can implement…

    Thread Starter Dutchg

    (@dutchg)

    I’m having trouble with that, Esmi.

    I created that custom page template, uploaded it into my child theme directory. I then went into my child functions.php, adding the code you sent, so it looked like this:

    <?php 
    
    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-9' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
    
    function exclude_category( $query ) {
        if ( $query->is_page_template( 'podcasts.php' ) && $query->is_main_query() ) {
            $query->set( 'cat', '9' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
    
    ?>

    When I updated to save that change, the site went away and I got this error:

    Warning: Cannot modify header information – headers already sent by (output started at /usr/home/mdgarvey/public_html/oldanswers/wp-content/themes/twentytwelve-child/functions.php:10) in /usr/home/mdgarvey/public_html/oldanswers/wp-includes/pluggable.php on line 1121

    You seem to have introduced a leading space or non-visible character into your functions.php file. Try reviewing Solving “headers already sent” warnings.

    Thread Starter Dutchg

    (@dutchg)

    I went through the code, re-saved and re-uploaded functions.php and podcasts.php. Then, this time, after I added the new snippet to functions.php and updated, I got shut down and this error:

    Fatal error: Cannot redeclare exclude_category() (previously declared in /usr/home/mdgarvey/public_html/oldanswers/wp-content/themes/twentytwelve-child/functions.php:3) in /usr/home/mdgarvey/public_html/oldanswers/wp-content/themes/twentytwelve-child/functions.php on line 14

    Sorry! My bad! Change the second instance of function exclude_category to function only_category

    Thread Starter Dutchg

    (@dutchg)

    OK. Just did that. So far so good, I think. I noticed, that last line of your snippet:

    add_action( ‘pre_get_posts’, ‘exclude_category’ );

    Is “exclude_category” right there? Or should that, too, change to “only_category”?

    Good catch! That should be changed too. ??

    Thread Starter Dutchg

    (@dutchg)

    well.. I changed it to only_category (last line of that snippet), updated, and got this error:

    Fatal error: Call to undefined method WP_Query::is_page_template() in /usr/home/mdgarvey/public_html/oldanswers/wp-content/themes/twentytwelve-child/functions.php on line 11

    Darn! I hoped that is_page_template() would work with pre_get_posts(). ??

    Try:

    function only_category( $query ) {
        if ( is_page_template( 'podcasts.php' ) && $query->is_main_query() ) {
            $query->set( 'cat', '9' );
        }
    }
    add_action( 'pre_get_posts', 'only_category' );
Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘separate page for podcast episodes’ is closed to new replies.