我正在创建一个插件来添加一个定制的post类型。对于每个定制的post类型,我还创建了自定义的单个模板。定制的单个模板不使用get_header();也不使用wp_head()函数,而是手动从头开始编写的。我像这样排队:
当我提交插件时,WordPress团队鼓励我使用内置的WordPress函数,比如wp_enqueue_style(),而不是上面的方法。
由于我不使用get_header()和wp_head,因此无法将其排队到单个模板的头中。
我尝试过几种类似的方法:
function wp_myplugin_enqueue_style() {
global $post;
if ($post->post_type == 'myplugin') {
wp_enqueue_style( 'myplugin-public-css', plugin_dir_url( __FILE__ ) . ' public/css/wp-myplugin-public.min.css ' );
}
}
add_action( 'wp_enqueue_scripts', ' wp_myplugin_enqueue_style' );包括这样:
function wp_myplugin_enqueue_style() {
if ( get_post_type( get_the_ID() ) == 'myplugin' ) {
wp_enqueue_style( 'myplugin-public-css', plugin_dir_url( __FILE__ ) . ' public/css/wp-myplugin-public.min.css ' );
}
}
add_action( 'wp_enqueue_scripts', ' wp_myplugin_enqueue_style ' );也是这样:
function wp_myplugin_enqueue_main_css() {
if (is_page_template('wp-myplugin-base-template.php')){
wp_enqueue_style( 'myplugin-public-css', plugin_dir_url( __FILE__ ) . ' public/css/wp-myplugin-public.min.css ' );
}
}
add_action( 'wp_enqueue_scripts', 'wp_myplugin_enqueue_main_css' );以上代码根本不起作用。
单个模板的如下所示:
>
ID, '_yoast_wpseo_title', true ));
elseif ($meta_title = get_post_meta( get_the_ID(), myplugin_prefix( 'meta-title' ), true ));
else $meta_title = get_option(sanitize_text_field('myplugin_meta_title'));
if ($meta_description = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true ));
elseif ($meta_description = get_post_meta( get_the_ID(), myplugin_prefix( 'meta-description' ), true ));
else $meta_description = get_option(sanitize_text_field('myplugin_meta_description'));
?>
<?php echo $meta_title; ?>
'.$custom_css .'';
}
?>为了包含wp-myplugin-public.min.css样式表,我可以使用的最佳方法是什么?我真的需要你的帮助。
非常感谢您提前!
发布于 2020-05-29 04:22:56
您可以使用do_action( 'your_hook_name' );创建自己的钩子。
在cpt模板文件中:
...
...然后在钩子中添加一个回调:
function add_css_file() {
?>https://wordpress.stackexchange.com/questions/367821
复制相似问题