Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using a custom function to create a shortcode that displays the latest blog post on the home page of a template. But I'm trying to NOT have it display any images.

I know I can use the Advanced Excerpt plugin to have it remove images, but the issue is that it will also remove images from the index.php feed which I want to keep which is using the_excerpt() in the template.

Here's my custom function that creates the shortcode:

function my_recent_news()
{
  global $post;
  $html = "";
  $my_query = new WP_Query( array(
       'post_type' => 'post',
       'posts_per_page' => 4
  ));

  if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

       $html .= "

       <article>
       <span class=\"date\">" . get_the_date() . "</span>
       <h2><a href=\"" . get_permalink() . "\">" . get_the_title() . "</a></h2>
       " . get_the_excerpt() . "
       </article>
       ";
  endwhile; 

  endif;

  wp_reset_query();

  return $html;

}

add_shortcode( 'news', 'my_recent_news' );

I've posted something about this before:

get excerpt without images

but the solution was to use the Advanced Excerpt plugin, but for this I am trying to use the excerpt on the home and the blog feed page but I want to preserve the img markup on the blog feed and remove the img tag from the custom shortcode.

I've tried to used just the_excerpt() in that custom shortcode function, but that just seems to break the whole function and display some really odd stuff.

I'm not too sure if I need to have a filter somewhere to strip that out or what. I'm also not sure that if I do need a filter, where that would go? Before the loop, after the loop, or does it need it's own constructed argument?

share|improve this question
    
When I do a quick test images are stripped from the generated excerpt. Do you have images in the manually created excerpt? –  s_ha_dum Nov 20 '13 at 16:17
    
@s_ha_dum: I have images within the posts. –  ultraloveninja Nov 20 '13 at 16:18
    
Auto-generated excerpts, or user-defined excerpts? The former should not display images, but I believe the latter will. –  Chip Bennett Nov 20 '13 at 16:19
    
@ChipBennett - You know what, I have no idea. And to be honest with you, I'm not sure where or how or what those are and where to modify them. This is the first time I am hearing about them today with my searching. If you have any insight on them, I'd be more than happy to know! –  ultraloveninja Nov 20 '13 at 16:39
    
I would start with the Codex entry on Excerpts. –  Chip Bennett Nov 20 '13 at 16:40
show 2 more comments

2 Answers

If you read the Codex entry for get_the_excerpt(), you will find this:

If the post does not have an excerpt, this function applies wp_trim_excerpt to the post content and returns that generated string with "[...]" at the end. wp_trim_excerpt is applied via the get_the_excerpt filter and can be removed.

The wp_trim_excerpt() function:

Generates an excerpt from the content, if needed.

The excerpt word amount will be 55 words and if the amount is greater than that, then the string ' [...]' will be appended to the excerpt. If the string is less than 55 words, then the content will be returned as is.

So, you could either re-apply wp_trim_excerpt() to the get_the_excerpt filter, or else just output it directly:

   $html .= "

   <article>
   <span class=\"date\">" . get_the_date() . "</span>
   <h2><a href=\"" . get_permalink() . "\">" . get_the_title() . "</a></h2>
   " . wp_trim_excerpt() . "
   </article>
   ";
share|improve this answer
    
I might try that as well, but I think that I found a solution. –  ultraloveninja Nov 20 '13 at 17:13
    
Great! Please be sure to post your solution as an answer. –  Chip Bennett Nov 20 '13 at 17:15
    
Yup! And thanks for your input! –  ultraloveninja Nov 20 '13 at 17:27
add comment
up vote 0 down vote accepted

Ok, so I did more digging and testing and I found by using strip_tags() basically removes any formatting within get_the_excerpt().

Here's my updated code:

function my_recent_news()
{
  global $post;
  $html = "";
  $my_query = new WP_Query( array(
       'post_type' => 'post',
       'posts_per_page' => 4
  ));

  if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

       $html .= "

       <article>
       <span class=\"date\">" . get_the_date() . "</span>
       <h2><a href=\"" . get_permalink() . "\">" . get_the_title() . "</a></h2>
       " . strip_tags(get_the_excerpt(), "<a>") . "
       </article>
       ";
  endwhile; 

  endif;

  wp_reset_query();

  return $html;

}

add_shortcode( 'news', 'my_recent_news' );

I added the <a> to preserve any hyperlinks within the excerpt of the post to display.

With this, I can preserve the markup with the blog feed page along with using the Advanced Excerpt plugin to assist with trimming the length and what not.

share|improve this answer
    
use wp_reset_postdata instead of wp_reset_query for more info –  Ravs Dec 2 '13 at 17:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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