php - 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();
}
解决方案
不完全确定这是否是您的问题,但我建议您不要弄乱主查询,因此您可能会影响页面的其他部分。
请改用以下 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
该类。
推荐阅读
- hbase - 扫描 hbase 表中列字段值为空的行
- javascript - |已解决| 从 Firebase 获取用户身份验证数据并将其添加到 Firebase DB
- c# - 为什么 if ( 'a'<= /*some char*/ <= 'z') 不起作用?
- api - 在 Express 中使用 Axios 进行 API 调用的错误处理
- wordpress - 使用 CPT 在 Wordpress 中制作考勤系统
- java - 将请求参数发送到同一个页面和同一个 servlet
- c++ - 为什么生成不同的汇编代码?哪个更好?
- java - 避免“HHH000104:使用集合提取指定的 firstResult/maxResults;在内存中应用!” 使用弹簧数据
- c# - 按自定义订单订购商品
- php - Symfony 本地服务器启动