1

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 ?

  • 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 Aug 27 '19 at 16:46
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" );
  • That complicated if statement is entirely unnecessary. Just use is_tax( 'mytaxonomy' ). Also, it doesn't make any sense to use the the_content filter for that. Use the proper wp_enqueue_scripts() hook. – Jacob Peattie Aug 28 '19 at 0:33
  • Why are you using a shortcode anyway? that's not the proper way to list posts in a template. – Jacob Peattie Aug 28 '19 at 0:34
  • @JacobPeattie, is_tax() will return true on page like this www.my-site/mytaxonomy/my-post-name as well ? – J.BizMai Aug 29 '19 at 12:21
  • @JacobPeattie, I have to use a shortcode because this file is in the child theme and the content to display is in a plugin I made. – J.BizMai Aug 29 '19 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.