How do I get FeedWordPress to display only a short summary or excerpt of syndicated posts, instead of the full content

Contents

[ hide ]

    For more how-tos and advice, return to How Do I …?

    Q. How do I get FeedWordPress to display only a short summary or excerpt of syndicated posts, instead of the full content?

    A. The best way for you to do this is to edit your WordPress template files so as to change what appears in the content section of your Post Loop. (You can edit template files directly from the WordPress administrative interface using Appearance –> Theme Editor, or you can edit them in a text editor on your own computer and upload them to your theme directory.)

    The Templates section of the Documentation Wiki documents the template tags that FeedWordPress provides when it is activated; the most important tags for this particular task is is_syndicated(). What you need to do is identify the Post Loop for your Theme, which typically looks something like this:

        <?php while (have_posts()) : the_post(); ?>
    
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark"
    title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?>
    <!-- by <?php the_author() ?> --></small>
    
    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>
    
    <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?>
    Posted in <?php the_category(', ') ?> | <?php
    edit_post_link('Edit', , ' | '); ?>  <?php
    comments_popup_link('No Comments &#187;', '1 Comment &#187;',
    '% Comments &#187;'); ?></p>
    </div>
    
    
    <?php endwhile; ?>
    

    (This example is taken from Kubrick, the default theme for WordPress 2.x. Different themes may have different post loops, but you can almost always identify them by finding while (have_posts()) : the_post(). In most themes, you will need to find the post loop in the Main Index Template (index.php), the Archives template (archive.php), and possibly the Single Post (single.php) and Search Results (search.php) templates. Some Themes, in order to avoid duplication, use a different template file, often called post-content.php or something similar, or a function in the Theme Functions (functions.php) template, to handle all displaying of post contents.)

    Anyway. Once you have found your post loop, you need to replace the template function the_content() with a code that will take an excerpt and display that. For example, by changing this portion of the loop:

                <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>
    

    To read like this instead:

                <div class="entry">
    <?php if (is_syndicated()) :
    // Strip down to an excerpt
    $text = get_the_content(); $text = strip_tags($text);
    if (strlen($text) > 255) :
    $text = substr($text, 0, 252).'...';
    endif;
    print $text;
    else :
    the_content('Read the rest of this entry &raquo;');
    endif; ?>
    </div>
    

    Does that answer your question? If not, use the Talk page to comment on this post or contact me by e-mail for help. Be sure to describe what you are trying to do, and the problems you are running into, with as much detail as possible.

    For more how-tos and advice, return to How Do I …?

    This page is a Wiki! Log in or register an account to edit.

    14 thoughts on “How do I get FeedWordPress to display only a short summary or excerpt of syndicated posts, instead of the full content

    1. It worked to clip the content to, for me 800 characters. BUT – the images disappears! Is there any other way to go around:

                   $text = get_the_content(); $text = strip_tags($text);
                   if (strlen($text) > 255) :
                        $text = substr($text, 0, 252).'...';
      
    2. I tried implementing both codes (short & long) and neither did anything. I have to assume it’s not recognizing the “is_syndicated()” function. Is there anyway to fix this?

    3. It looks like my syndicated posts are being truncated with [...]

      I’m sure it has something to do with the excerpt, but I’ll need to dig further. Have you seen this before?

      • I think I found it – If the blog I’m syndicationg has their settings to only display a summary in a feed that would only allow me to syndicate that blog’s ‘excerpt’.

        Correct?

        This could be construed as subversive, but is there a way around that?

        It’s more about not needing to manage the technical knowledge of the users whose blogs I have consent to syndicate.

    4. I couldn’t get it to work! I entered to my template index like this:

                  <div class="post-content">
      
                  <?php if (is_syndicated()) :
                       // Strip down to an excerpt
                       $text = get_the_content(); $text = strip_tags($text);
                       if (strlen($text) > 255) :
                            $text = substr($text, 0, 252).'...';
                       endif;
                       print $text;
                  else :
                       the_content('Read the rest of this entry &raquo;');
                  endif; ?>
      
                 </div>
      

      Something error with the code?

    5. just find the ” the_content” (use ctr+f in mozilla to find it easily)and then replace the

              <div class="entry">
                  <?php the_content('Read the rest of this entry &raquo;'); ?>
              </div>
      

      with bellow code

              <div class="entry">
                  <?php if (is_syndicated()) :
                       // Strip down to an excerpt
                       $text = get_the_content(); $text = strip_tags($text);
                       if (strlen($text) > 255) :
                            $text = substr($text, 0, 252).'...';
                       endif;
                       print $text;
                  else :
                       the_content('Read the rest of this entry &raquo;');
                  endif; ?>
              </div>
      
    6. Hello Charles, I’m trying to set up FeedWordPress on one of my sites. Looks great so far, but I have one problem I’m hoping you can help me with.

      In the case of some feeds, I’d like to post the official feed “Excerpt” i.e. theexcerpt() , and in the case of other feeds, I’d like to post the official full feed “Content” i.e. thecontent()

      I know that you have listed two ways to accomplish something similar: (1) on your How Do I…? you show how to strip down the syndicated post based on string size and (2) we have your Limit Size of Posts Add-on. Both of these solutions are great, but they simply strip down the number of words or characters.

      I am after the actual, official theexcerpt() – not a simple strip-down of text from thecontent()

      In my case, I’d like to present an IF ELSE scenario where we post the full feed post i.e. thecontent() for certain authors and just theexcerpt() for other authors.

      Based on your examples, I came up with the below code for my Post Loop. However, in every case, theauthor() gets displayed i.e. printed to HTML and it always posts theexcerpt()

      Its like the IF ELSE conditional is never getting evaluated or executing because it just prints the_author() to HTML every time.

      Can you help me with this conditional such that for certain authors it posts thecontent() and for other authors it posts theexcerpt() ????

      Sorry, I am not an engineer and don’t know much about php or wordpress, and so any help would be appreciated.

                       if (the_author('John Doe','Jane Doe','Ricky Bobby','Frank Michlick')) :
                             the_excerpt('<span class="readmore">Read the rest of '. get_the_title('', '', false). ' &raquo;</span>', FALSE);
                  else :
                             the_content('<span class="readmore">Read the rest of '. get_the_title('', '', false). ' &raquo;</span>', FALSE);
                  endif;
      
    7. Your code strips out HTML tags. That’s the quick-n-dirty solution to the problem. A better solution would be to keep track of tags on a stack and also keep track of how many characters have been included. Then once you acquire ‘x’ characters, bail out. Before displaying the content, append a bunch of closing tags based on the stack. It isn’t perfect but it is a lot better than having a blob of text as output. Here’s what I came up with and placed into index.php:

      < ?php
          if (is_syndicated())
          {
              // Strip down to an excerpt.
              $text = get_the_content();
              $length = 0;
              $finaltext = "";
              $tags = explode("<", $text);
              $stack = array();
              foreach ($tags as $tag)
              {
                  $pos = strpos($tag, ">");
                  $pos2 = strpos($tag, " ");
                  if ($pos !== false)
                  {
                      if ($pos2 === false || $pos2 > $pos)  $pos2 = $pos;
                      $tagname = trim(substr($tag, 0, $pos2));
                      if (substr($tagname, -1) == "/")  $tagname = substr($tagname, 0, -1);
                      if (substr($tagname, 0, 1) == "/")
                      {
                          $tagname = substr($tagname, 1);
                          array_pop($stack);
                      }
                      else
                      {
                          $closed = (substr($tag, $pos - 1, 1) == "/");
                          if (!$closed)  $stack[] = $tagname;
                      }
      
                      $finaltext .= "<" . substr($tag, 0, $pos + 1);
      
                      $text = preg_replace('/\s+/', " ", substr($tag, $pos + 1));
                      if ($length + strlen($text) < 500)
                      {
                          $length += strlen($text);
                          $finaltext .= $text;
                      }
                      else
                      {
                          $finaltext .= substr($text, 0, 500 - $length) . "...";
                          $length = 500;
      
                          break;
                      }
                  }
              }
              while (count($stack))
              {
                  $tagname = array_pop($stack);
                  $finaltext .= "</" . $tagname . ">";
              }
      
              echo $finaltext;
      
              if ($length >= 500)
              {
      ?>
                  <p><a href="<?php the_permalink() ?>" target="_blank">Read the rest of this entry »</a></p>
      < ?php
              }
          }
          else
          {
              the_content('Read the rest of this entry »');
          }
      ?>
      

      Your mileage might vary but so far the results look pretty good on my site. Just replace ’500′ with whatever you want as your character limit. If you feel like getting fancy, you could try locating the nearest space to the limit and cut off there instead of in the middle of a word. I was feeling lazy.

    8. I’ve worked how to add the code in. When I use the original code suggested on the site, it works fine but is not very pretty. When I use the longer code suggested below, it’s prettier, but it breaks my sidebar (the sidebar appears after the post content instead of next to it. Could some tag not be closed properly (or something?)

    9. WordPress has a built in excerpt function, why use all the extra code? Just change thecontent to theexcerpt. that’s it.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>