WordPress.org

Ready to get started?Download WordPress

Forums

the_excerpt() not working properly on homepage (12 posts)

  1. trickyninja
    Member
    Posted 2 years ago #

    Hello,
    I have been searching the codex/support forums and trying to resolve this for a week to no avail. I'm hoping a WP guru can get me on track and will be as detailed as possible. I ran a WP site a few years back but many things have changed and I'm just getting back into it.

    I'm hosting a WP site on a server at my home:

    • Server: LAMP configuration (Ubuntu 10.04 LTS).
    • WP version: 3.4
    • Theme: eDegree
    • Plugins: Featured Content Gallery, Contact Form 7, NextGEN Gallery (all updated to latest versions)

    My goal was to have summary posts on the main page displaying my 5 most recent posts, all set under the "Settings" > "Reading" section. This did not work as far as displaying a summary/excerpt so I browsed around on the Codex/forums for a week trying to resolve this myself before posting.

    While browsing, I found a number of past posts related to this, including the Codex entry for the_excerpt() function. The biggest difference I have found in my theme vs. what I read in posts, is that it seems to use the_post() instead of the_content(). Since many of the posts I found were outdated (a year or more), I assumed that the_post() was an updated version. I tried replacing the_post() with the_excerpt() on index.php and home.php, but it led to a few problems. I noticed that if I replaced it only on index.php nothing happened, but when I replaced it on home.php it seems to break something. When I reload the main page, it will post an excerpt above the post, and then endlessly re-display the same post again and again until I hit stop in my browser, and also makes the sidebar disappear.

    Sample code I'm referring to is a conditional (tried updating the_post() -> the_excerpt():

    <?php if (have_posts()) : ?>
    		<?php
            $i = 0;
            while (have_posts()) {
                the_post();
                include(dirname(__FILE__).'/post.php');
                if ($html = get_option($shortname.'_custom_html_'.$i)) {
                    echo "<div class='customhtml'>$html</div>";
                }
            $i++;
            }
    		?>
    	<?php endif; ?>

    I'm wondering if it is possibly an oversight in the template code somewhere that is causing this issue, possibly a missing HTML or PHP closing tag or something. I have tried using the <!--more--> tag in my posts which seems to work as far as displaying the summary/excerpt, however, when I validate my WP site using the W3C validator, it seems to omit closing </p> tags for some reason and cause validation errors. I have tried deactivating all plugins after making the updates to the_excerpt() but it doesn't seem to make a difference.

    Thank you for taking the time to read and/or provide any guidance, it is greatly appreciated. I can provide more info if needed as well.

  2. deepbevel
    Member
    Posted 2 years ago #

    I'm no guru but I'm here.
    Sounds like a complex theme.
    About all I can say is I don't see an "endwhile". But chances are if you've been pursuing this for a week you already know that and for whatever reason, you don't need it in this code. goodluck

    ..aha, the bracket. never mind.

  3. jonradio
    Member
    Posted 2 years ago #

    I don't like changing Template files, so I create a Page, and put a Shortcode on the Page and write the ShortCode to extract the Posts for me.

    Here is a simple example of a ShortCode that gets the last 15 posts in the "news" category:

    $posts = get_posts( array(
    	'category_name' => 'news',
    	'numberposts'     => 15,
    	'orderby' => 'post_date',
    	'order' => 'DESC'
    	)
    );
    
    foreach ( $posts as $post ) {
    	setup_postdata($post);
    	echo '<h1><a href="';
    	the_permalink();
    	echo '">';
    	the_title_attribute();
    	echo '</a></h1><p class="premeta">';
    	the_time(get_option('date_format'));
    	echo '<span class="sep"> by </span> <span class="author vcard">';
    	the_author();
    	echo '</span>';
    	echo ', <span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links">posted in</span> ';
    	the_category(', ');
    	echo '</span>';
    	echo '</p>';
    	the_excerpt();
    }
    wp_reset_postdata();

    I use this plugin http://wordpress.org/extend/plugins/shortcode-exec-php/ to quickly build, test and implement my shortcodes. Later, I put the shortcodes into a plugin. The code above is as how it appears in the Shortcode EXEC PHP plugin.

  4. trickyninja
    Member
    Posted 2 years ago #

    Thanks for the reply deep. I may be mistaken, but I believe with a while loop in php, as long as you use the {} brackets you don't need an endwhile, correct?:

    <?php if (have_posts()) : ?>
    		<?php
            $i = 0;
            while (have_posts()) {
                the_post();
                include(dirname(__FILE__).'/post.php');
                if ($html = get_option($shortname.'_custom_html_'.$i)) {
                    echo "<div class='customhtml'>$html</div>";
                }
            $i++;
            }
    		?>
    	<?php endif; ?>
  5. deepbevel
    Member
    Posted 2 years ago #

    yes, I beleive that's right as i've done it too. sorry :)

  6. popper
    Member
    Posted 2 years ago #

    You should edit the post.php file.

    Look for:

    the_content()

    and replace with the_excerpt()

  7. deepbevel
    Member
    Posted 2 years ago #

    I'm wondering if jonradio's solution may be a work around for you, but I notice it still uses the_excerpt which I'm not clear if your theme uses or not.

  8. deepbevel
    Member
    Posted 2 years ago #

    if your index.php or main loop file has the_content you should be able to change it as popper said. But I think if you do that in single.php or post.php your post will be an excerpt even in single post view. Which you prpbably don't want.

  9. popper
    Member
    Posted 2 years ago #

    Actually, deepbevel, if you look at the code he posted, the home,php includes the file post.php to render the post, so he'll have to change it there. Easier and cleaner would be to simply put the logic inside the home.php post.

  10. deepbevel
    Member
    Posted 2 years ago #

    I didn't even know home.php was his blog page. I don't understand that code, I'm used to the twenty ten main loop code where I can make that change. His theme is out of my league. I'm out!

  11. trickyninja
    Member
    Posted 2 years ago #

    Popper, thanks a lot for the reply. I was able to locate the_content on the post.php page and swap it out for the_excerpt. It seems to be working as far as only showing an excerpt amount worth of text. However, it seems to have caused a minor issue as a result.

    I tend to start my posts with a [caption ... [/caption] tag to include an image with brief text (usually for sourcing an image if it's not my own). It appears that if I leave the excerpt field of the post empty, the caption/image don't show up at all. But if I manually set the Excerpt field with the caption/image and then the text I want for excerpt, it will actually show the caption tags on page before and after the image and not create the caption box. Is this something you have heard of before when using the_excerpt?

    Also, Deep, no worries mate! We can't solve them all, that's why I'm here :) Thanks for taking the time, its much appreciated.

  12. popper
    Member
    Posted 2 years ago #

    Yeah, that is the expected behavior of the_excerpt, since it strips all html tags from the post, returning only plain text.

    There's lots of good tutorials on changing that, for example: http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-preserve-html-tags-in-wordpress-excerpt-without-a-plugin/.

    Try that, if you run into any trouble post back here. :)

Topic Closed

This topic has been closed to new replies.

About this Topic