我写了第一节函数课。php,我想用它来创建多个选项卡,只需在我的模板文件上进行简单的调用。当我直接在函数中创建新实例时。php对于该实例来说一切都很好。但当我通过函数外的助手函数调用类时。php,所有操作挂钩都不会生效。有什么建议可以做到这一点吗?
当做
class EasyTabs {
private static $instance;
public static function get_instance() {
if ( !self::$instance instanceof self )
self::$instance = new self;
return self::$instance;
}
public function container_id() {
$id = rand(1000,9999);
return $id;
}
public function get_the_tabs() {
...
}
public function tabs_js() {
...
}
public function tabs_styles_and_scripts() {
// style
wp_register_style(\'jquery-easytabs-style\', get_stylesheet_directory_uri() . \'/inc/easytabs/style.css\', array(), \'\', \'all\');
wp_enqueue_style(\'jquery-easytabs-style\');
//script
wp_register_script(\'jquery-easytabs-min\', get_stylesheet_directory_uri() . \'/inc/easytabs/jquery.easytabs.min.js\', array(\'jquery\'), \'\', false);
wp_enqueue_script(\'jquery-easytabs-min\');
wp_register_script(\'jquery-hashchange-min\', get_stylesheet_directory_uri() . \'/inc/easytabs/jquery.hashchange.min.js\', array(\'jquery\'), \'\', false);
wp_enqueue_script(\'jquery-hashchange-min\');
}
public function __construct() {
//actions
add_action(\'wp_enqueue_scripts\', array ( $this, \'tabs_styles_and_scripts\' ));
add_action(\'wp_head\', array($this, \'tabs_js\'));
}
}
if ( !function_exists( \'the_tabs\' ) ) {
function the_tabs() {
echo EasyTabs::get_instance()->get_the_tabs();
}
}
Edit:
感谢您的提示,寻找一个稍后会开火的钩子成功了:
http://shibashake.com/wordpress-theme/wp_enqueue_script-after-wp_headclass RFRQ_Tabs {
public $containerID;
public $posttype;
public function __construct( $posttype ) {
// set vars
$this->set_container_ID();
$this->set_posttype($posttype);
//add scripts and styles
add_action(\'wp_footer\', array($this, \'scripts\'));
add_action(\'wp_print_footer_scripts\', array($this, \'ex_scripts\'), 1);
}
...
public function scripts() {
echo "<script type=\'text/javascript\'>";
echo "jQuery(document).ready( function() {";
echo "jQuery(\'#tab-container-" . $this->get_container_ID() . "\').easytabs();";
echo "});";
echo "</script>";
}
public function ex_scripts() {
if (is_admin())
return;
wp_enqueue_script(\'jquery-easytabs-min\', get_stylesheet_directory_uri() . \'/inc/easytabs/jquery.easytabs.min.js\', array(\'jquery\'), \'\', true);
wp_enqueue_style(\'jquery-easytabs-style\', get_stylesheet_directory_uri() . \'/inc/easytabs/style.css\', array(), \'\', \'all\');
}
}
/**
* RFRQ
* helper function to instance an tab-object
*/
if (!function_exists(\'the_tabs\')) {
function the_tabs( $posttype ) {
$easytabs = new RFRQ_Tabs( $posttype );
echo $easytabs->get_the_tabs();
}
}