首页 > 解决方案 > Wordpress Shortcode 不会在 html 字符串中返回 vars

问题描述

我已经尝试了我能想到的一切。标题在内容包装器的顶部吐出,而 html 标记保留在应有的位置。

我正在从页面执行此简码。我需要媒体库中图像的标题和永久链接,以便我可以吐出 html 以通过短代码在页面上显示它

add_shortcode( 'ispimg', 'isp_gallery_item' );

function isp_gallery_item( $atts ) {

// Attributes
$a = shortcode_atts( array(
    'id' => '',
), $atts );


$isp_post_id = get_post($a['id']);
setup_postdata($isp_post_id);

$pt = the_title();

return "<h3>".$pt."</h3>";


wp_reset_postdata();

}

标签: phphtmlwordpressshortcode

解决方案


不完全确定这是否是您的问题,但我建议您不要弄乱主查询,因此您可能会影响页面的其他部分。

请改用以下 API 函数get_the_title()

function isp_gallery_item( $atts ) {
    // Attributes
    $a = shortcode_atts( array(
        'id' => '',
    ), $atts );

    return sprintf( '<h3>%s</h3>', get_the_title( $a['id'] ) );
}
add_shortcode( 'ispimg', 'isp_gallery_item' );

或者,如果您需要循环,请查看WP_Query该类。


推荐阅读