Display tags on taxonomy page

I currently have an option in my CMS to add tags to my separate message type page.

Now I want to display this tag as a 'featured' element. So, in my taxonomy - β€œfile name” I use the following code that collects the tags and displays them on 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 problem is that now the highlighted tag will be displayed on each page of the category, as it is set on the taxonomy page.

How can I do this only for the current category.

So, if my item is in category β€œA”, only this category of category β€œA” will show this using the item category?

Any help would be great

Change This code is used, hoping it will 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(), ), ), ); 
+8
php tags wordpress taxonomy custom-post-type
source share
1 answer

Your problem is your user request. It is very important to note here that never change the main request using a custom archive on any type of page or on the home page. I have explained in detail everything in this post recently. Be sure to read it and all related messages, as this will be of great benefit to you.

Your solution would be to remove your user request and replace it 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 request, use pre_get_posts for this

EDIT

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

You can try something like this: (At least PHP 5.4+ is required. Also, this has not been verified)

 $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 versions of PHP, 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 messages that are in the sector1 tag and any other sectorX tag, you can do the following

You can try something like this: (At least PHP 5.4+ is required. Also, this has not been verified)

 $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 versions of PHP, 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 notice, you can pass an array of tags to a terms parameter like this

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

or syntax for short arrays

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

EDIT 3

Since this is your main request, you need to make a few changes. As I said, delete the user request. 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 --> 

Now you can use pre_get_posts to remove the desired tag from your taxonomic pages. In your functions.php, follow these steps: (Requires PHP 5.3+, and also not verified)

 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 ) ); } } 

Remember to change 145 to your exact id tag or ids tag array

EDIT 4

If you do not have tag identifiers, you can use get_term_by() to get the tag identifier from the tag. Something like this will do: (Requires PHP 5.3+, and also not verified)

 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 tags, 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 ) );/* 

from

 $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 ); 

Remember to change bullets correctly.

EDIT 5

Your last 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 ); } } 
+1
source share

All Articles