php - Displaying Tags on taxonomy page -
i have option in cms, add tags custom post type single page.
now, wanting display tag 'featured' item. so, in taxonomy-'filename', use following code gathers tags , displays them in 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, issue is, display selected tag on every category page now, set on taxonomy page.
how can make set on current category.
so if item in 'category a', category page of 'a' show this, using items category?
any great
edit. used code, hoping should work, 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 custom query. 1 important note here is, never ever change replace main query custom 1 on type of archive page or home page. have explained in detail in this post recently. make sure read , linked posts benefit lot
your solution remove custom query , replace default loop know
if ( have_posts() ) { while ( have_posts() ) { the_post(); // template tags , html mark } }
if need change in main query, use pre_get_posts
so
edit
your best idea here use full tax_query
display posts in selected taxonomy term , tag
you can try this: (requires @ least php 5.4+. also, 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 slug ], ], ];
for older php versions, use 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 slug ), ), );
edit 2
to exclude posts in sector1
tag , other sectorx
tag, can following
you can try this: (requires @ least php 5.4+. also, 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 slug 'operator' => 'not_in' ], ], ];
for older php versions, use 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 slug 'operator' => 'not_in' ), ), );
just note, can pass array of tags terms
parameter this
'terms' => array( 'sector1', 'sector2', 'etc' ),
or short array syntax
'terms' => ['sector1', 'sector2', 'etc'],
edit 3
as main query, need make few changes. have said, remove custom query. main loop should 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 no posts, display error message --> <?php endwhile; else: ?> <p><?php _e('sorry, no posts matched criteria.'); ?></p> <?php endif; ?> <!-- if there no posts, display error message -->
you can use pre_get_posts
remove desired tag taxonomy pages. in functions.php, following: (requires php 5.3+, , 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 change 145
exact tag id or array of tagids
edit 4
if don't have tag ids, can use get_term_by()
tag id tag slug. do: (requires php 5.3+, , 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 have array of tag slugs, can replace 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 $tag ) { $tag_object = get_term_by( 'slug', $tag, 'post_tag' ); $tagids[] = $tag_object->term_id; } $q->set( 'tag__not_in', $tagids );
just remember change slugs accordingly
edit 5
your final code in functions.php 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 $tag ) { $tag_object = get_term_by( 'slug', $tag, 'post_tag' ); $tagids[] = $tag_object->term_id; } $q->set( 'tag__not_in', $tagids ); } }
Comments
Post a Comment