Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Dynamic link to single CPT taxonomy term WordPress

  • SOLVED

Hi all,

I have a custom post type called solution_type. Within that CPT, I have a taxonomy called solution_tax, within that taxonomy I have two terms: packaging-solutions-by-machine and packaging-solutions-by-industry.

Within my homepage, I have two loops that display the posts from each of those terms. Those loops work ok. What I am struggling with is creating a dynamic link at the bottom of both those loops that goes through to the taxonomy term archive.

So far, using the Wordpress get_term_link function, spits out links to both taxonomy terms, however in the loop for packaging-solutions-by-machine, I only want a link to the term in question.

Here is an example of the original loop that works ok:

<nav id="industry-menu" class="sixcol first">
<?php $args=array(
'taxonomy' => 'solution_tax',
'term' => 'packaging-solutions-by-industry',
'post_type' => 'solution_type',
'orderby'=>'title',
'order'=>'asc'
);
?>
<?php $temp_query = $wp_query; query_posts($args);?>
<h4 class="border"><?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?></h4>
<ul class="clearfix border-bottom">
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$extra_class = "";
if($count % 3 == 0) {
$extra_class = "first";
} elseif($count % 3 == 2) {
$extra_class = "last"; }
?>
<li class="fourcol <?php echo $extra_class ?> nav-grid-item text-center">
<?php if(has_post_thumbnail()) :?>
<a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a>
<?php else :?>
<?php endif;?>
<h5 class="no-margin-bottom">
<a href="<?php the_permalink();?>"><?php the_title();?></a>
</h5>
</li>
<?php $count++; ?>
<?php endwhile; ?>
<?php wp_reset_query(); // end showreel loop. ?>
</ul>
<a href="HERE IS WHERE I NEED THE LINK TO THE TAX TERM ARCHIVE" class="button">More information</a>
</nav><!-- end industry nav -->


Can anyone help please?

Thanks,

Adam.

Answers (2)

2013-07-29

Dbranes answers:

You could try this :

<?php
if( isset( $args['term'] ) && isset( $args['taxonomy'] ) ):
echo get_term_link( $args['term'], $args['taxonomy'] );
endif;
?>


(if I'm not misunderstanding you)


flint_and_tinder comments:

Hi Dbranes,

Nearly there. That code is outputing the correct link, but only in text form.

Ideally I'd like the link to be outputed like this:

<a href="THE TERM ARCHIVE LINK" class="button">More information</a>

Can your code be adapted to do that?


Dbranes comments:

ok, try this:

<?php

if( isset( $args['term'] ) && isset( $args['taxonomy'] ) ):

printf( '<a href="%s" class="button">%s</a>', get_term_link( $args['term'], $args['taxonomy'] ), _e( 'More information here' ) );

endif;

?>


Dbranes comments:

take two:

<?php
if( isset( $args['term'] ) && isset( $args['taxonomy'] ) ):
printf( '<a href="%s" class="button">%s</a>', get_term_link( $args['term'], $args['taxonomy'] ), __( 'More information here' ) );
endif;
?>


flint_and_tinder comments:

Excellent. Thank you!

2013-07-29

Giri answers:

I still don't understand your question. But to get a specific term link use link this

$term_link = get_term_link( 'packaging-solutions-by-industry', 'solution_tax' );

if( is_wp_error( $term_link ) )
continue;

echo '<a href="' . $term_link . '">' . $term->name . '</a>';


flint_and_tinder comments:

Nearly. But I need the link text to say 'More Information'.

e.g:

<a href="THE TERM ARCHIVE LINK" class="button">More information</a>


Giri comments:

Or if you are in a loop, you may want to hide terms other than packaging-solutions-by-industry.

In that case use like this

if ($term = 'packaging-solutions-by-industry') {
$term_link = get_term_link( $term, 'solution_tax' );
if( is_wp_error( $term_link ) )
continue;
echo '<a href="' . $term_link . '">' . $term->name . '</a>';
}



Please note the above code should be placed before <?php endwhile; ?>


Giri comments:

In that case just use like this

$term_link = get_term_link( 'packaging-solutions-by-industry', 'solution_tax' );
if( is_wp_error( $term_link ) )
continue;
echo '<a href="' . $term_link . '">More information</a>';


flint_and_tinder comments:

This works too. Thank you.

Not sure what to do here. You've both given correct answers pretty much at the same time.

Splitting it seems the fairest.


Giri comments:

Ya sure go ahead and split that..