【问题标题】:Wordpress Shortcode will not return vars inside html stringWordpress Shortcode 不会在 html 字符串中返回 vars
【发布时间】:2023-04-06 17:36:01
【问题描述】:

我已经尝试了我能想到的一切。标题在内容包装器的顶部吐出,而 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();

}

【问题讨论】:

  • 首先,您的wp_reset_postdata(); 将不起作用。你的错误是什么(不清楚)?以及你想达到什么目标?
  • return = "

    ".$pt."

    "; ?? ...和@SamvelAleqsanyan 一样——不清楚你想在这里做什么。

标签: php html wordpress shortcode


【解决方案1】:

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

改用get_the_title() 等API 函数:

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 类。

【讨论】:

  • 我需要媒体库中图像的标题和永久链接,以便我可以吐出 html 以通过简码将其显示在页面上。
猜你喜欢
  • 2018-12-21
  • 1970-01-01
  • 2014-02-14
  • 2022-01-11
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
相关资源
最近更新 更多