admin管理员组文章数量:1415139
I have a template for a specific taxonomy :
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(); ?>
In an other php file I need to check if do_shortcode will be run with my-shortcode
tag to enqueue styles and scripts.
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" ) );
}
}
In my case, I cannot use has_shortcode( $post->post_content, 'my-shortcode' )
because it´s not in a post_content
but directly inside the template php file.
Soemone has got an idea ?
I have a template for a specific taxonomy :
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(); ?>
In an other php file I need to check if do_shortcode will be run with my-shortcode
tag to enqueue styles and scripts.
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" ) );
}
}
In my case, I cannot use has_shortcode( $post->post_content, 'my-shortcode' )
because it´s not in a post_content
but directly inside the template php file.
Soemone has got an idea ?
Share Improve this question asked Aug 27, 2019 at 16:00 J.BizMaiJ.BizMai 9002 gold badges10 silver badges30 bronze badges 1 |1 Answer
Reset to default 1I found a solution.
First of all, the best way seems to not use do_shortcode
direclty in the php.
You can know more here.
So...
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(); ?>
To enqueue scripts and styles, I did this
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" );
本文标签: custom taxonomyHow to check if doshortcode will be execute directly in a template php file
版权声明:本文标题:custom taxonomy - How to check if do_shortcode will be execute directly in a template php file 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745206340a2647656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
is_tax( ‘mytaxonomy’ )
. The specific use of that function doesn’t particularly matter. – Jacob Peattie Commented Aug 27, 2019 at 16:46