I have the following two functions in my theme:
function content($limit) {
global $post;
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content);
$content = wp_strip_all_tags($content, true);
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
function contentnoimg($limit) {
global $post;
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content);
$content = wp_strip_all_tags($content, true);
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/(<img.+?>)/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
And in the loop, it is called this way:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'full', array('class'=>'post_thumbnail_common', 'alt' => get_the_title() , 'title' => get_the_title(), 'itemprop'=>'image' ));
echo contentnoimg(41);} else { echo content(41); } ?>
I would like the site to show the_excerpt();
only if it exists. If has_excerpt()
is negative, then it should work as it is right now showing the content of get_the_content()
How should the code look like? I tried different ways, but my site stops working, so I am doing something wrong.
if( has_excerpt() ) { the_excertp(); }
? It seems you know those functions but I don't understand where do you have the problem to implement them.