这既是一个WordPress问题,也是一个PHP问题,因为PHP程序员可能知道WP小组不知道的技术。
我一直在尝试编写一个短代码,用于激发Foundation Reveal代码,以便与WP编辑器内联使用。我设想它可以这样工作:
[reveal size="medium" bg="true" slug="page-slug"]Open a modal with this link![/reveal]
 shortcode指定模式大小、是否使用黑色背景,以及在自定义单页循环中合并来自哪个页面段塞的内容(这允许我们使用非公共自定义post类型来保存模式数据)。还有其他属性,但为了简单起见,我还没有编写它们。
这就是问题的症结所在。
模态分为两部分,链接和隐藏DIV。DIV通常位于站点的页脚。我可以把它内联起来解决我的问题,但这会对谷歌的网络缓存造成严重破坏。否,模态需要是页脚中的旁白。
所以我有点聪明。我让短代码返回目标链接,但我也让它注册了一个挂钩(放在footer.php中),其中附加了一个content DIV。在我意识到变量范围有问题之前,一切都很正常。首先,这是我当前的代码:
/**
 * Foundation Reveal Shortcode
 */
// Create an action to be placed in the site footer. We will target this with the second half of the function
function foundation_reveal() {
    do_action(\'foundation_reveal\');
}
// Set up the shortcode for the modal
function reveal_setup($atts, $content) {
    // Extract the attributes
    extract(shortcode_atts(array(
        \'size\'  =>  \'medium\',
        \'slug\'  =>  \'dummy\',
        \'bg\'    =>  \'reveal-modal\'
    ), $atts));
    $modal_link  = \'<a href="#" data-reveal-id="\' . $slug . \'">\';
    $modal_link .= $content;
    $modal_link .= \'</a>\';
    if ($slug == \'dummy\') {
        $the_modal = \'<div id="\' . $slug . \'" class="\' . $size . \' \' . $bg . \'" data-reveal>\' . \'reminder/dummy content goes here\' . \'</div>\';
    } else {
        $the_modal = \'<div id="\' . $slug . \'" class="\' . $size . \' \' . $bg . \'" data-reveal>\' . \'post loop based on page slug goes here\' . \'</div>\';
    }
    function reveal_content($the_modal) {
        echo $the_modal;
    }
    add_action(\'foundation_reveal\', \'reveal_content\');
    return $modal_link;
}
/**
 * Setup shortcodes for this theme
 */
// Register all shortcodes
function gmfi_shortcodes() {
    add_shortcode(\'reveal\', \'reveal_setup\');
}
// Add shortcodes to the init hoook
add_action( \'init\', \'gmfi_shortcodes\');
 而且,看到IDE对整个事情进行颜色编码是很有帮助的,
here\'s a link to that image.
这个问题对于任何PHP程序员来说都是显而易见的。为了将$slug之类的内容传递给第二个函数(该函数连接到页脚),我需要通过引用传递变量。
i、 例如,我必须这样做:
add_action(\'foundation_reveal\', \'reveal_content($slug)\');
 不幸的是,WordPress并不是这样工作的。
那么问题来了。。。你能想出另一种方法通过引用传递那个变量吗?因为如果没有,我可能不得不接受它并编写一个定制的钩子/动作脚本。如果有一种方法可以使用可用的WordPress组件来实现,那么这个选项似乎是浪费。但是,如果有另一种方法,WordPress/Foundation插件套件可能已经做到了。
不管怎样,我都需要你们帮我想出下一步。
非常感谢你坚持我的解释!
另外注意:页脚中的foundation\\u reveal()钩子肯定会被触发,它只是没有得到传递给它的任何类或ID数据。
 
                    最合适的回答,由SO网友:gmazzap 整理而成
                    有不同的方法可以获得结果。您可以使用类,将div内容存储在类或实例变量中,并在需要时输出它。
也可以使用带有静态变量的函数来保存内容。
我将使用第二种选择,在类中转换它对您来说是一种练习;)
在上瘾中,你可以使用核心\'wp_footer\' 钩子来输出内容,这样您就不必添加额外的钩子,也不必使用模板标记。
function reveal_setup($atts = array(), $content = \'\') {
  // Setup the static variable. Use an array to allow multiple calls per page
  static $the_modals = array();
  // if the function is called from wp_footer hook
  if ( current_filter() === \'wp_footer\' ) {
    if ( is_array($the_modals) && ! empty($the_modals) ) {
      foreach( $the_modals as $amodal ) {
        echo $amodal;
      }
    }
  // if the function is called from shortcode
  } else {
    // Get the attributes
    $atts = shortcode_atts(
      array( \'size\' => \'medium\', \'slug\' => \'dummy\',\'bg\' => \'reveal-modal\' ),
      $atts,
      \'reveal\' // enable filtering
    );
    // prepare the_modal link
    $modal_link  = \'<a href="#" data-reveal-id="\' . $atts[\'slug\'] . \'">\';
    $modal_link .= $content;
    $modal_link .= \'</a>\';
    // prepare the_modal content
    $modal_format = \'<div id="%s" class="%s %s" data-reveal>\';
    $the_modal = sprintf( $modal_format, $atts[\'slug\'], $atts[\'size\'], $atts[\'bg\'] );
    if ( $atts[\'slug\'] == \'dummy\' ) {
      $the_modal .= \'reminder/dummy content goes here\';
    } else {
      $the_modal .= \'post loop based on page slug goes here\';
    }
    $the_modal .= \'</div>\';
    // save the modal content in the static modals array
    $the_modals[] = $the_modal;
    // add the present function to wp_footer hook if it is not already added
    if ( ! has_action(\'wp_footer\', __FUNCTION__) ) {
      add_action( \'wp_footer\', __FUNCTION__ );
    }
    // return the modal link
    return $modal_link;
  }
}
add_shortcode(\'reveal\', \'reveal_setup\');
 仅此而已。
Untested.
PS:extract 使用是一种不好的做法,(尽管我知道它主要用于核心代码和Codex示例中…)