带有当前页面 ID 的简码
Shortcode with current page id
我对 php 知之甚少,碰壁了。
有什么方法可以在 wordpress 中使用 "put current page id" 而不是 do_shortcode 函数中的特定 ID 号吗?还是插件内部文件的问题?
<?php echo do_shortcode('[plugin-review id="2"]'); ?>
当我将此代码放入侧边栏时,我不想在我网站的每个页面上显示 id=2 的页面内容。
感谢您提供可能的解决方案。
编辑:我刚刚也发现了这个功能(还有更多功能相同的功能)这可能会有帮助?
public function do_shortcode_ratings( $atts )
{
extract( shortcode_atts( array(
'id' => '',
'template' => '',
'post' => get_the_ID()
), $atts ) );
$shortcode = '[plugin-review id="'. $id .'" branch="ratings" post="'. $post .'" template="'. $template .'"]';
return do_shortcode( $shortcode );
}
当前 post/page ID 可通过全局 $post
变量获得,即
global $post;
$current_id = $post->ID;
现在您可以在短代码中使用 $current_id
:)
可以关注,这个
global $post;
$current_id = $post->ID;
echo do_shortcode('[plugin-review id="'.$current_id.'"]');
我对 php 知之甚少,碰壁了。
有什么方法可以在 wordpress 中使用 "put current page id" 而不是 do_shortcode 函数中的特定 ID 号吗?还是插件内部文件的问题?
<?php echo do_shortcode('[plugin-review id="2"]'); ?>
当我将此代码放入侧边栏时,我不想在我网站的每个页面上显示 id=2 的页面内容。
感谢您提供可能的解决方案。
编辑:我刚刚也发现了这个功能(还有更多功能相同的功能)这可能会有帮助?
public function do_shortcode_ratings( $atts )
{
extract( shortcode_atts( array(
'id' => '',
'template' => '',
'post' => get_the_ID()
), $atts ) );
$shortcode = '[plugin-review id="'. $id .'" branch="ratings" post="'. $post .'" template="'. $template .'"]';
return do_shortcode( $shortcode );
}
当前 post/page ID 可通过全局 $post
变量获得,即
global $post;
$current_id = $post->ID;
现在您可以在短代码中使用 $current_id
:)
可以关注,这个
global $post;
$current_id = $post->ID;
echo do_shortcode('[plugin-review id="'.$current_id.'"]');