最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom taxonomy - How to check if do_shortcode will be execute directly in a template php file

programmeradmin0浏览0评论

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
  • If you know that your taxonomy template uses a shortcode, then just check is_tax( ‘mytaxonomy’ ). The specific use of that function doesn’t particularly matter. – Jacob Peattie Commented Aug 27, 2019 at 16:46
Add a comment  | 

1 Answer 1

Reset to default 1

I 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" );
发布评论

评论列表(0)

  1. 暂无评论