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

Display a post by ID attribute with shortcode

programmeradmin0浏览0评论

I'm trying to create a shortcode that displays a banner containing a post content (thumbnail, title and excerpt), using ID attribute. My goal is to be able to use a shortcode like [postbanner id=123] to display any post by its ID.

Here is my code (in function.php child theme):

function post_banner_shortcode($atts) {
    
    $atts = shortcode_atts( array(
        'id' => ''
    ), $atts );
    
    $post_id = $atts['id'];
    
    $HTML  = '<div class="postbanner">';
    $HTML .= '<div class="pb-thumb">' . get_the_post_thumbnail($post_id, 'medium') . '</div>';
    $HTML .= '<div class="pb-ttl-txt">';
    $HTML .= '<h4>' . get_the_title($post_id) . '</h4>';
    $HTML .= '<p>' . get_the_excerpt($post_id) . '</p>';
    $HTML .= '</div>';
    $HTML .= '</div>';
    
    return $html; 
}   
add_shortcode( 'postbanner', 'post_banner_shortcode' );

Actually this code return nothing and I don't understant why. I'm missing something here. I'm still a rookie with PHP so that's why I need some help guys :)

I'm trying to create a shortcode that displays a banner containing a post content (thumbnail, title and excerpt), using ID attribute. My goal is to be able to use a shortcode like [postbanner id=123] to display any post by its ID.

Here is my code (in function.php child theme):

function post_banner_shortcode($atts) {
    
    $atts = shortcode_atts( array(
        'id' => ''
    ), $atts );
    
    $post_id = $atts['id'];
    
    $HTML  = '<div class="postbanner">';
    $HTML .= '<div class="pb-thumb">' . get_the_post_thumbnail($post_id, 'medium') . '</div>';
    $HTML .= '<div class="pb-ttl-txt">';
    $HTML .= '<h4>' . get_the_title($post_id) . '</h4>';
    $HTML .= '<p>' . get_the_excerpt($post_id) . '</p>';
    $HTML .= '</div>';
    $HTML .= '</div>';
    
    return $html; 
}   
add_shortcode( 'postbanner', 'post_banner_shortcode' );

Actually this code return nothing and I don't understant why. I'm missing something here. I'm still a rookie with PHP so that's why I need some help guys :)

Share Improve this question asked Jan 30, 2021 at 14:57 dragowebdragoweb 2494 silver badges13 bronze badges 1
  • Did you try to debug $atts inside the function - for example var_dump( $atts ); ? – Q Studio Commented Jan 30, 2021 at 14:59
Add a comment  | 

1 Answer 1

Reset to default 2

Try $HTML instead of $html Linux based systems are case-sensitive ...

function post_banner_shortcode($atts) {
    
    $atts = shortcode_atts( array(
        'id' => ''
    ), $atts );
    
    $post_id = $atts['id'];
    
    $HTML  = '<div class="postbanner">';
    $HTML .= '<div class="pb-thumb">' . get_the_post_thumbnail($post_id, 'medium') . '</div>';
    $HTML .= '<div class="pb-ttl-txt">';
    $HTML .= '<h4>' . get_the_title($post_id) . '</h4>';
    $HTML .= '<p>' . get_the_excerpt($post_id) . '</p>';
    $HTML .= '</div>';
    $HTML .= '</div>';
    
    return $HTML; 
}   
add_shortcode( 'postbanner', 'post_banner_shortcode' );
发布评论

评论列表(0)

  1. 暂无评论