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 |1 Answer
Reset to default 2Try $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' );
var_dump( $atts );
? – Q Studio Commented Jan 30, 2021 at 14:59