I'm trying to create a custom shortcode to display the last x-amount of recent posts.
But the feature of this shortcode is that there should be a possibility to show the first post, most recent, at the full extension, not only exerpt but fully, any idea how can I achieve that?
Here is the code I have ...
add_shortcode( 'custom-home-posts', function () {
extract(shortcode_atts(array(
"num" => '5',
"cat" => ’
), $atts));
global $post;
$myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
$i = 0;
foreach($myposts as $post){
$i++;
if ($i == 1)
{
$out.='<div class="post">
<h3 class="post-title"><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></h3>
<span class="author-date-blog">'.get_the_author().'|'.get_the_date('d M Y').'</span>
<div class="excerpt">
'.get_the_content().'
<div class="read-more">
<a href="'.get_the_permalink().'#disqus_thread">Leave a Comment >></a>
</div>
</div>
</div>';
}
else
{
$out.='<div class="post">
<div class="thumb">
<a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_post_thumbnail('blog-thumb').'</a>
</div>
<h3 class="post-title"><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></h3>
<span class="author-date-blog">'.get_the_author(). '|'.get_the_date('d M Y').'</span>
<div class="excerpt">'
.get_the_excerpt().
'<div class="read-more">
<a href="'.get_the_permalink().'">Read More >></a>
</div>
</div>
</div>';
}
}
return html_entity_decode($out);
} );
This code looks ok, BUT if($i==1)
i get an issue with get_the_content
, because it returns the previous array of posts from another plugin which is used on the same page, which is very strange tho, i thought the get_the_content
is in the scope of the current post, but it seems not so ... I hope I could explain what I mean. So basically the content of the first post is shown the gridview from the previous plugin/shortcode on the page.
All the other posts, 2,3,4 etc are shown correctly, but there I only use get_the_excerpt
.
!!! 1 more question, is there a possibility to show not full post, but excerpt, and only so many words like 200 for example.
Thanks in advance