Home » Php » php – Displaying Tags on taxonomy page

php – Displaying Tags on taxonomy page

Posted by: admin July 12, 2020 Leave a comment

Questions:

I currently have the option in my CMS, to add tags to my custom post type single page.

Now, I am wanting to display this tag as a ‘featured’ item.
So, In my taxonomy-‘filename’, I use the following code which gathers the tags and displays them in the taxonomy page:

            <?php 
        $args = array(
          'tag_slug__and' => array('sector1'),
          'post_type' => array( 'sectors' )
          );
        $loop = new WP_Query( $args );
        while ($loop->have_posts() ) : $loop->the_post();
        ?>
        <a href="<?php echo get_permalink(); ?>">
         <?php echo "<div class='col-md-6' style='margin-bottom:20px;'>"; ?>
         <div class="row mobilemargin">
          <div class="categorytiletextsector1">
            <div class="col-md-6 col-sm-6 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'sector1img hovereffect')); ?> </div>
            <div class="col-md-6 col-sm-6 col-xs-12">
              <div class="testdiv">
               <h5><?php the_title(); ?></h5>
               <p><?php the_excerpt(); ?></p>
             </div>
           </div>
         </div>
       </div>
       <?php echo "</div>"; ?>

     </a>
   <?php endwhile; ?>
   <?php wp_reset_query(); ?>

Now, my issue is, this will display the selected tag on every category page now, as it is set on the taxonomy page.

How can I make this only set on the current category.

So If my item is in the ‘category A’, only the category page of ‘A’ would show this, using the items category?

Any help would be great

Edit.
Used this code, hoping this should work, but no luck

$args = array(
    'tag_slug__and' => array( 'sector1' ),
    'post_type'     => array( 'sectors' ),
    'tax_query'     => array(
        array(
            'taxonomy' => 'sectors',
            'terms'    => get_queried_object_id(),
        ),
    ),
);
How to&Answers:

Your problem is your custom query. One very important note here is, never ever change replace the main query with a custom one on any type of archive page or the home page. I have explained everything in detail in this post recently. Make sure to read it and all the linked posts as this will benefit you a lot

Your solution would be to remove your custom query and replace this with the default loop that we all know

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();

        // Your template tags and html mark up

    }
}

If you need to change anything in the main query, use pre_get_posts to do so

EDIT

Your best idea here would be to use a full tax_query to display posts that is in the selected taxonomy term and tag

You can try something like this: (Requires at least PHP 5.4+. Also, this untested)

$q = get_queried_object();
$args = [
    'post_type' => 'sectors',
    'tax_query' => [
        [
            'taxonomy' => $q->taxonomy,
            'terms' => $q->term_id,
            'include_children' => false // Exclude child terms
        ],
        [
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => 'sector1', //I believe this is the slug
        ],
    ],
];

For older PHP versions, use the following

$q = get_queried_object();
$args = array(
    'post_type' => 'sectors',
    'tax_query' => array(
        array(
            'taxonomy' => $q->taxonomy,
            'terms' => $q->term_id,
            'include_children' => false // Exclude child terms
        ),
        array(
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => 'sector1', //I believe this is the slug
        ),
    ),
);

EDIT 2

To exclude posts that are in the sector1 tag and any other sectorX tag, you can do the following

You can try something like this: (Requires at least PHP 5.4+. Also, this untested)

$q = get_queried_object();
$args = [
    'post_type' => 'sectors',
    'tax_query' => [
        [
            'taxonomy' => $q->taxonomy,
            'terms' => $q->term_id,
            'include_children' => false // Exclude child terms
        ],
        [
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => 'sector1', //I believe this is the slug
            'operator' => 'NOT_IN'
        ],
    ],
];

For older PHP versions, use the following

$q = get_queried_object();
$args = array(
    'post_type' => 'sectors',
    'tax_query' => array(
        array(
            'taxonomy' => $q->taxonomy,
            'terms' => $q->term_id,
            'include_children' => false // Exclude child terms
        ),
        array(
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => 'sector1', //I believe this is the slug
            'operator' => 'NOT_IN'
        ),
    ),
);

Just note, you can pass an array of tags to the terms parameter like this

'terms' => array( 'sector1', 'sector2', 'etc' ),

or short array syntax

'terms' => ['sector1', 'sector2', 'etc'],

EDIT 3

As this is your main query, you need to make a few changes. As I have said, remove the custom query. Your main loop should look something like this

<?php if (have_posts()) : ?> 
    <?php while (have_posts()) : the_post(); ?> 
        <a href="<?php echo get_permalink(); ?>"> 
        <?php echo "<div class='col-md-6 col-sm-6 col-xs-12' style='margin-bottom:30px;'>"; ?> 
        <div class="row mobilemargin"> 
            <div class="categorytiletext2"> 
                <div class="col-md-6 col-sm-12 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'hovereffect newimgheight')); ?> </div> 
                <div class="col-md-6 col-sm-12 col-xs-12 mobilewhite"> 
                    <div class="testdiv"> 
                        <h5 class="captext"><?php the_title(); ?></h5> 
                        <?php $trimexcerpt = get_the_excerpt(); 

                        $shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… ' ); 

                        echo '<a href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>'; 

                        ?> 
                    </div> 
                </div> 
            </div> 
        </div> 
        <?php echo "</div>"; ?> 

        </a> 
        <!-- If there is no posts, display an error message --> 
    <?php endwhile; 
else: ?> 
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?> 
<!-- If there is no posts, display an error message -->

You can now use pre_get_posts to remove the desired tag from your taxonomy pages. In your functions.php, do the following: (Requires PHP 5.3+, and is also untested)

add_action( 'pre_get_posts', function ( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
       $q->set( 'tag__not_in', array( 145 ) );
    }
});

For older versions use

add_action( 'pre_get_posts', 'so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
       $q->set( 'tag__not_in', array( 145 ) );
    }
}

Just remember to change 145 to your exact tag id or an array of tagids

EDIT 4

If you don’t have the tag ids, you can use get_term_by() to get the tag id from the tag slug. Something like this will do: (Requires PHP 5.3+, and is also untested)

add_action( 'pre_get_posts', function ( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
        $tag_object = get_term_by( 'slug', 'sector1', 'post_tag' ); 
        $tagID = $tag_object->term_id; 

       $q->set( 'tag__not_in', array( $tagID ) );
    }
});

For older versions use

add_action( 'pre_get_posts', 'so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
        $tag_object = get_term_by( 'slug', 'sector1', 'post_tag' ); 
        $tagID = $tag_object->term_id; 

       $q->set( 'tag__not_in', array( $tagID ) );
    }
}

If you have an array of tag slugs, you can replace the following

$tag_object = get_term_by( 'slug', 'sector1', 'post_tag' ); 
$tagID = $tag_object->term_id; 

$q->set( 'tag__not_in', array( $tagID ) );/*

with

$tag_array = array( 'slug1', 'slug2', 'slug3' );
foreach ( $tag_array as $tag ) {
    $tag_object = get_term_by( 'slug', $tag, 'post_tag' ); 
    $tagids[] = $tag_object->term_id;
} 
$q->set( 'tag__not_in', $tagids );

Just remember to change the slugs accordingly

EDIT 5

Your final code in functions.php with pre_get_posts should be

add_action( 'pre_get_posts', 'so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
        $tag_array = array( 'sector1', 'sector2', 'sector3', 'sector4' );
        foreach ( $tag_array as $tag ) {
            $tag_object = get_term_by( 'slug', $tag, 'post_tag' ); 
            $tagids[] = $tag_object->term_id;
        } 
        $q->set( 'tag__not_in', $tagids );    
    }
}