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(),
),
),
);
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 you need to change anything in the main query, use
pre_get_posts
to do soEDIT
Your best idea here would be to use a full
tax_query
to display posts that is in the selected taxonomy term and tagYou can try something like this: (Requires at least PHP 5.4+. Also, this untested)
For older PHP versions, use the following
EDIT 2
To exclude posts that are in the
sector1
tag and any othersectorX
tag, you can do the followingYou can try something like this: (Requires at least PHP 5.4+. Also, this untested)
For older PHP versions, use the following
Just note, you can pass an array of tags to the
terms
parameter like thisor short array syntax
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
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)For older versions use
Just remember to change
145
to your exact tag id or an array of tagidsEDIT 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)For older versions use
If you have an array of tag slugs, you can replace the following
with
Just remember to change the slugs accordingly
EDIT 5
Your final code in functions.php with
pre_get_posts
should be