【问题标题】:Shortcode with current page id具有当前页面 ID 的简码
【发布时间】:2016-11-06 18:22:39
【问题描述】:

我对php的了解很少。

有什么方法可以在 wordpress 的 do_shortcode 函数中“放置当前页面 id”而不是特定的 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 );

}

【问题讨论】:

  • 请出示您的代码。
  • 很抱歉,我必须在发布前删除:/
  • 你不能尝试:` $id = get_the_ID();'
  • 好像不行:(

标签: php wordpress shortcode


【解决方案1】:

当前帖子/页面 ID 可通过全局 $post 变量获得,即

global $post;
$current_id = $post->ID;

现在您可以在短代码中使用$current_id :)

【讨论】:

    【解决方案2】:

    你可以关注,这个

    global $post;
    $current_id = $post->ID;
    echo do_shortcode('[plugin-review id="'.$current_id.'"]');
    

    【讨论】:

      猜你喜欢
      • 2014-04-16
      • 2020-06-24
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      相关资源
      最近更新 更多