WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to define a taxonomy term? (8 posts)

  1. NoArtworkAvailable
    Member
    Posted 1 year ago #

    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>
  2. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  3. racer x
    Member
    Posted 1 year ago #

    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.

  4. NoArtworkAvailable
    Member
    Posted 1 year ago #

    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.

  5. vtxyzzy
    Member
    Posted 1 year ago #

    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?

  6. NoArtworkAvailable
    Member
    Posted 1 year ago #

    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>
  7. vtxyzzy
    Member
    Posted 1 year ago #

    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;
    ?>
  8. NoArtworkAvailable
    Member
    Posted 1 year ago #

    Holy moly. HOLY MOLY!

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

Topic Closed

This topic has been closed to new replies.

About this Topic