我知道这很古老,但这是我的答案,它可以帮助任何面临类似挑战的人。基于@fuxia code,她的代码工作正常,但只显示一个输出。假设您在一个页面上有多个短代码实例,则只有一个操作挂钩会添加到页脚。
如果要根据短代码在页面上的使用次数添加多个操作,请使用以下代码;
add_shortcode( \'izzycart_popup\', array ( \'MFP_PopUP_Shortcode\', \'load_shortcode\' ) );
class MFP_PopUP_Shortcode {
protected static $var = array();
public static function load_shortcode( $atts, $content = \'\' ) {
/**
* Write all your shortcode stuff here.
* Make sure you use either unique_id() or mt_rand() to generate ID in your
* shortcode for later use
**/
/**
* Depending on what you\'re using this for, send parameters from shortcode to
* new function. I used this for a iPad frame Video, So i parsed the following
* @params $video_url - Video URL from the shortcode_atts()
* @params $video_poster - Video poster id from the shortcode_atts()
* @params $el_id - element ID generated from either unique_id() or mt_rand()
* @params array $OtherContent - Array of other values you want to parse to the function
**/
self::$var[] = self::load_the_popupcontent($video_url, $video_poster, $el_id, $OtherContent);
add_action( \'wp_footer\', array ( __CLASS__, \'send_to_footer\' ) );
}
public static function send_to_footer() {
foreach (self::$var as $theElementToFooter) {
echo $theElementToFooter;
}
}
public static function load_the_popupcontent( $url=\'\', $poster=\'\', $video_id = \'\', $args = array() ) {
// Write all your code to be added to footer here and use all the variables where needed.
}
}
NOTE: 如果你只想在页脚上添加一个元素,而不管这个短代码使用了多少次,@fuxia的答案就是你想要的。
经过测试,工作正常<快乐的编码!