So I am working on a site for a client of mine. We have about 50+ posts filed in a category called "Articles". I created a custom page template that has a custom query to display all the posts in a bullet list on this page, and if you click on it you can go directly to the post to read more or open a PDF directly. Where I am hitting a road block is we have 2 specific articles that are more then just PDF's, they are about 3-6 paragraphs instead. SO I wanted to know if it is possible to display all the posts in the list like I am now, and if it exceeds around 100 characters then revert to the excerpt we made for those posts specifically.

Currently my code looks like this:

            //for each child of category 16 (articles), show title of posts
            $cat_args=array('child_of' => 16, 'orderby' => 'name', 'order' => 'DESC');
              foreach($categories as $category) {
                $args=array('showposts' => -1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1);
                  if ($posts) {
                    echo '<h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></h3> ';
                    foreach($posts as $post) {
                      setup_postdata($post); ?>
                        <!--<li><?php the_title_attribute(); ?></li>-->
                            <li><?php echo get_the_content(); ?></li>
                    } // foreach($posts
                  } // if ($posts
                } // foreach($categories

Thank you! Been pulling my hair out for days on this one.


replace the_content(); with echo wpse_limit_content();

   function wpse_limit_content() {
   $content = $post->post_content;
   $MAX_LENGTH = 100;

    if ( strlen(  $content )  <= $MAX_LENGTH )
        return apply_filters('the_content', $content );

    $s2  = substr( $content, 0, $MAX_LENGTH  );
    $s3  = preg_split( "/\s+(?=\S*+$)/", $s2 );
    $s4  = $s3[0];

        return apply_filters( 'the_excerpt', $s4 );


If the string is to long it trims it to give you a nice 100 char excerpt.

you could also just do:

$content = $post->post_content;

if (strlen( $content > 100 ) {
} else {

You could just print out the_excerpt and set the excerpt length to whatever you wanted your limit to be.

  • Yeah but if I do that won't I need all the posts to be called by the excerpt then? I only made 2 excerpts for the posts that have a lot of content. I would like to avoid creating an excerpt for each post because of only 2 posts that have extensive content.
    – Danny
    Jun 25 '12 at 18:43
  • hmm actually I changed it to the "get_the_excerpt" instead, which now outputs the way I want. Only issue now is I can't click on the item and have it open the PDF like before. Is there a way to link this item to open the PDF?
    – Danny
    Jun 25 '12 at 19:00
  • thank you for your response. I used your method with the_excerpt so it is displaying the way I want. Previously when I had "echo get_the_content" it would make each <li> item linked to the content which in turn would open the PDF/Article directly. Is there something I could use on the anchor tag before the_excerpt to get this to work? Right now I am trying: ` <li> <a href="<? echo get_the_content (); ?>"><?php echo get_the_excerpt(); ?></a> </li>` which isn't working, but I feel like its close. Thank you for your help, im going crazy!
    – Danny
    Jun 25 '12 at 20:31
  • If you want the link to open the article directly, you need to use get_permalink
    – fdsa
    Jun 25 '12 at 21:15
  • To link to files use wp_get_attatchment_url.
    – fdsa
    Jun 25 '12 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.