最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom shortcode to display posts, first post is full

programmeradmin1浏览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

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

Share Improve this question edited May 8, 2019 at 12:15 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 8, 2019 at 11:31 AlnedruAlnedru 1034 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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);
} );
发布评论

评论列表(0)

  1. 暂无评论