• Resolved akschoeck

    (@akschoeck)


    Website link is:
    https://xn--ernst-und-gabriele-mller-stiftung-bqd.de/

    Working on this website based off of the twenty-twelve theme. On the home page, towards the bottom, I’m trying to get recent blog posts to show up, truncated to some amount of characters. I have the following code in my functions.php file already:

    function my_excerpt_length($length) {
    	return 25;
    }
    add_filter('excerpt_length', 'my_excerpt_length');

    and the following code is on my index.php file:

    <?php if ( have_posts() ) :
       while ( have_posts() ) :
          the_excerpt(); ?>
          <div class="blogPost">
             <div class="blogMeta">
                <div class="blogDate"><?php the_time('d') ?><br /></div>
                <div class="blogMonth"><?php the_time('M') ?></div>
             </div>
             <div class="blogContent">
                <?php get_template_part( 'content', get_post_format() ); ?>
             </div>
          </div>
       <?php endwhile; ?>
    <?php endif;

    When I use the_post() on the third line there instead of the_excerpt(), it works and displays the full post. When I change it to the_excerpt(), it takes forever to load, and clearly is not behaving as I was hoping it would. This is what the homepage is currently set to, so you can see what it’s doing exactly.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    You’re putting the code in the wrong place.

    Change it back to the_post(). That function call sets up the global variables for the other functions to work. You need to call the_post() for anything to display.

    Now, what is actually displaying your post content here is not in this file. This line of code:
    <?php get_template_part( 'content', get_post_format() ); ?>

    That tells WP to go and fetch a file named content.php from the theme and run it. It also looks for the content-format.php files, where “format” is the post format you’re using. So if you made an “aside” post, then it would first look for content-aside.php and include that, and fall back to content.php if that file does not exist.

    Anyway, inside those files, you will find a reference to a function call the_content(). If you change that to the_excerpt(), then it will work like you expect. However, doing that will mean that even the single-post page view will only show the excerpt.

    So, you can do something like this instead:

    if ( is_single() ) {
       the_content();
    } else {
       the_excerpt();
    }

    And that will show the full content on the single-post pages and use the excerpt everywhere else.

    Thread Starter akschoeck

    (@akschoeck)

    Aha, I understand what you’re saying.

    Took a little bit of a different approach and just replaced

    <div class="blogContent">
       <?php get_template_part( 'content', get_post_format() ); ?>
    </div>

    with

    <div class="blogContent">
       <?php the_title( '<h3>', '</h3>' ); ?>
       <?php the_excerpt(); ?>
    </div>

    and that worked as well. Thank you for the explanation!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can't get the_excerpt() to work’ is closed to new replies.