0

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

1
  • when I've deleted other widgets / shotcodes, the first posts, doesn't show any content now :(
    – Alnedru
    Commented May 8, 2019 at 11:46

1 Answer 1

1

There are several issues in your code. Like, syntax error in line 4. Undefined $out variable PHP notice. And main issue is that you are not using the loop properly. In foreach, you need to use setup_postdata() and it also needs to be reset properly. Please check following.

    add_shortcode( 'custom-home-posts', function ( $atts ) {
    extract(shortcode_atts(array(
                "num" => '5',
                "cat" => '',
        ), $atts));
        global $post;
        $out = '';
        $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
        $i = 0;
        foreach($myposts as $post){
            setup_postdata( $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>';
            }
            wp_reset_postdata();
        }
        return html_entity_decode($out);
} );
1
  • thanks, resolved my issue =)
    – Alnedru
    Commented May 10, 2019 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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