• Resolved noartworkavailable

    (@noartworkavailable)


    Is there a way to list the pages that are under a current taxonomy term? I have been able to get it to display the pages when the taxonomy term is hardcoded, but I can’t figure out how to get the term for the current page and put it in a wp_query. The reason for not having it be hardcoded is so that as the user is navigating, the taxonomy term would change accordingly.

    Is there a way I can replace ‘hardcodedterm’ with ‘$currentartist’ or something similar?

    This is what I am using to display the list for the ‘hardcodedterm’.

    <?php
        $query = new WP_Query( array( 'artist' => 'hardcodedterm', 'post_type' => 'page' ) );?>
            <ul>
            <?php while ( $query->have_posts() ) : $query->the_post();?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile;?>
    </ul>

Viewing 7 replies - 1 through 7 (of 7 total)
  • If I understand your question correctly, one solution is to put the term in a custom field and retrieve the field for the current page.

    See here for help on building taxonomy templates.

    Depending on your situation I have also passed the taxonomy term to a custom page via GET in the url. Be sure to validate the data, then simply add it to your query as you mentioned above.

    Thread Starter noartworkavailable

    (@noartworkavailable)

    A custom field wouldn’t be practical as I’d have to start anew (as opposed to the custom taxonomy which has been already previously defined).

    With taxonomy templates, as I understand it, you can only make archive pages for custom taxonomy templates and not for individual posts. The code I’m trying to use above is to put on the single-post templates that are members of a term in a custom taxonomy.

    The WP function get_the_terms() will let you get all the terms of a given taxonomy that are attached to a post. Does that help?

    Thread Starter noartworkavailable

    (@noartworkavailable)

    Ah ha! You’re great! using get_the_terms() I was able to get it to find the pages of the associated taxonomy term. The problem that I’m having now is that on posts without any taxonomy term specified, it still shows some pages, just seemingly random ones.

    Is there a way to tell it to hide my list when there are no taxonomy terms assigned? I was trying to do it with if(is_object_in_taxonomy( ‘post’ , ‘artist’ )) {} but I can’t get it to work yet.

    <?php
        if(is_object_in_taxonomy( 'post' , 'artist' )) {
        	$arterms = get_the_terms( $post->ID, 'artist' );
        	foreach( $arterms as $artax );
        	}
        $query = new WP_Query( array(
       		'festivals' => "$artax->slug" ,
       		'post_type' => 'page',
       		'order' => 'ASC'
       	 ) );?>
            <ul>
            <?php while ( $query->have_posts() ) : $query->the_post();?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    
        <?php endwhile;?>
           </ul>

    Looks like you may have a problem in the nesting of statements. Give this a try:

    <?php
       if(is_object_in_taxonomy( 'post' , 'artist' )) :
        	$arterms = get_the_terms( $post->ID, 'artist' );
        	foreach( $arterms as $artax ) :
       		$query = new WP_Query( array(
       		   'festivals' => "$artax->slug" ,
       		   'post_type' => 'page',
       		   'order' => 'ASC'
       		) );?>
       		<ul>
       		<?php while ( $query->have_posts() ) : $query->the_post();?>
                <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    
             <?php endwhile;?>
             </ul>
          <?php endforeach;
       endif;
    ?>
    Thread Starter noartworkavailable

    (@noartworkavailable)

    Holy moly. HOLY MOLY!

    It works! and it works perfectly!
    Thank you so so so much!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to define a taxonomy term?’ is closed to new replies.