WordPress.org

Forums

Twenty Eleven is not showing an auto-generated excerpt (7 posts)

  1. jannmirch
    Member
    Posted 3 years ago #

    I have been banging my head against the wall on this one!

    I am creating a custom template for the home page. Everything seems to be working fine EXCEPT I can't get the excerpts to show!!

    The idea is to have three columns that each pull the most recent post from a specific category. The title and "read more" are showing up and linking through to the appropriate posts, but the excerpt itself is not showing up. It will only do so if I create an explicit excerpt which I really don't want.

    Here's the code I'm using:

    <?php
    $posts = get_posts('numberposts=1&category=6');
    foreach($posts as $post) :
    ?>
    <h2><a>"><?php the_title(); ?></a></h2>
    <p><?php the_excerpt();?></p>
    <p><a>">Read more...</a></p>
    <?php endforeach; ?>

    I'm using the Twenty Eleven theme and I've already created a child theme.

    Here's the testing page: http://jannmirch.com/ypie/

    You'll see the left column has the manually added excerpt.

  2. jannmirch
    Member
    Posted 3 years ago #

    Here's the code properly referenced, sorry:

    <?php
    $posts = get_posts('numberposts=1&category=6');
    foreach($posts as $post) :
    ?>
    <h2>"><?php the_title(); ?></h2>
    <p><?php the_excerpt();?></p>
    <p>">Read more...</p>
    <?php endforeach; ?>
  3. jannmirch
    Member
    Posted 3 years ago #

    Oy!! Some got stripped. Really, truly, here it is:

    <?php
    $posts = get_posts('numberposts=1&category=6');
    foreach($posts as $post) :
    ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><?php the_excerpt();?></p>
    <p><a href="<?php the_permalink(); ?>">Read more...</a></p>
    <?php endforeach; ?>
  4. alchymyth
    Forum Moderator
    Posted 3 years ago #

    try to add setup_postdata($post);

    <?php
    $posts = get_posts('numberposts=1&category=6');
    foreach($posts as $post) :
    setup_postdata($post);
    ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><?php the_excerpt();?></p>
    <p><a href="<?php the_permalink(); ?>">Read more...</a></p>
    <?php endforeach; ?>

    http://codex.wordpress.org/Template_Tags/get_posts#Examples

  5. jannmirch
    Member
    Posted 3 years ago #

    Like a charm!! Thank you!

    And I'm assuming based on my reading I can go into my functions.php to set the length of the excerpt, correct?

  6. alchymyth
    Forum Moderator
    Posted 3 years ago #

    I can go into my functions.php to set the length of the excerpt

    a bit more complicated, as the parent theme already has some code to set a custom length for the excerpt; and this would be executed after the functions.php of the child theme.

    however, there is an instruction in functions.php of twenty eleven:

    * We can remove the parent theme's hook only after it is attached, which means we need to
     * wait until setting up the child theme:
     *
     * <code>
     * add_action( 'after_setup_theme', 'my_child_theme_setup' );
     * function my_child_theme_setup() {
     *     // We are providing our own filter for excerpt_length (or using the unfiltered value)
     *     remove_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );
     *     ...
     * }
     * </code>
     *

    or read http://ottopress.com/tag/twentyten/ (should work analogous for Twenty Eleven) for how to do this in a child theme.

  7. jannmirch
    Member
    Posted 3 years ago #

    Sorry for radio silence! Vacation then Hurricane Irene had me loopy for a bit.

    Anyway, I just edited the functions.php file:

    /**
     * Sets the post excerpt length to 40 words.
     *
     * To override this length in a child theme, remove the filter and add your own
     * function tied to the excerpt_length filter hook.
     */
    function twentyeleven_excerpt_length( $length ) {
    	return 30;
    }
    add_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );

    And it seems to be working.

    Thanks for all your help.

Topic Closed

This topic has been closed to new replies.

About this Topic