I want to show the excerpt from a post only if it exists. If it does not I want to show an excerpt of the content text of the post. Here is what I came up with:
<?php
$my_excerpt = $item->post_excerpt;
if ($my_excerpt) {
echo Str::limit($my_excerpt, 120);
}
else
{
$content = ($item->post_content);
$contentexcerpt = substr($content, 0, 150);
echo $contentexcerpt, '...';
}
?>
This worked perfectly until I tried to use it on an archive page inside a foreach loop. Now it shows both the custom excerpt and an excerpt taken from the content text.
Here is the code for the whole archive page archivepage
Any suggestions would be welcome.