我有一个特定分类法的模板:
taxonomy-mytaxonomy.php
<?php
defined( \'ABSPATH\' ) || exit;
get_header( );
?>
<!-- Content -->
<div id="content" class="content" role="main">
<?php
the_archive_description( \'<div class="taxonomy-description">\', \'</div>\' );
if ( have_posts() ) {
$queried_object = get_queried_object();
$term_slug = $queried_object->slug;
$shortcode = sprintf( \'[my-shortcode attr1="value1" /]\',
$term_slug
);
echo do_shortcode($shortcode);
} else {
get_template_part( \'no-results\', \'search\' );
}
?>
</div><!-- #content -->
<?php get_footer(); ?>
在另一个php文件中,我需要检查do\\u shortcode是否将与一起运行
my-shortcode
标记以将样式和脚本排队。
public function check_page(){
global $post;
if( !empty( $post->post_content ) && has_shortcode( $post->post_content, \'my-shortcode\' ) ){
add_action( "wp_enqueue_scripts", array( $this, "set_scripts" ) );
}
}
就我而言,我不能使用
has_shortcode( $post->post_content, \'my-shortcode\' )
因为它不在
post_content
但直接在模板php文件中。
Soemone有主意了?
SO网友:J.BizMai
我找到了解决办法。首先,最好的方法似乎没有用do_shortcode
php中的direclty。你可以知道更多here.
所以
taxonomy-mytaxonomy.php
<?php
defined( \'ABSPATH\' ) || exit;
get_header( );
?>
<!-- Content -->
<div id="content" class="content" role="main">
<?php
the_archive_description( \'<div class="taxonomy-description">\', \'</div>\' );
if ( have_posts() ) {
$queried_object = get_queried_object();
$term_slug = $queried_object->slug;
$atts = [
\'attr1\' => "value1"
];
echo ListCPTShortcode::getCallBack( $atts, null, "my-shortcode" );
} else {
get_template_part( \'no-results\', \'search\' );
}
?>
</div><!-- #content -->
<?php get_footer(); ?>
为了让脚本和样式排队,我这样做了
my-theme/functions.php
function my_shortcode_category_load( $content ){
$current_post = get_queried_object();
if( !empty( $current_post ) && isset( $current_post->taxonomy ) && $current_post->taxonomy === "mytaxonomy" ){
wp_enqueue_style(...);
wp_enqueue_script(...);
}
return $content;
}
add_filter( "the_content", "my_shortcode_category_load" );