WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying dynamic taxonomy term on category post template (9 posts)

  1. Gram3000
    Member
    Posted 2 years ago #

    Hello!

    The challenge: to display 2 posts pulled from the post taxonomy 'organizations' in the preceding post. The main post taxonomy 'organizations' has a term of 'x'. Then display 2 posts with the term 'x'. The code below does this but using the term is static. Not so good for a template.

    <?php $organizations_query = new WP_Query(
    array(
    'post_type' => 'post',
    'posts_per_page' => 2,
    'tax_query' => array( array(
          'taxonomy' => 'organizations',
          'field' => 'slug',
          'terms' =>  'x' ))
    )); ?>
    
    <?php if ( $organizations_query->have_posts() ): ?>		
    
    <ul><?php while ( $organizations_query->have_posts() ) : $organizations_query->the_post(); ?>
    
    <li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?></ul>
    <?php endif; ?>

    In the code queries from 'organizations' and displays posts with the term 'x'. How do I pull a dynamic term from 'organizations'? I've read the codex, but having trouble putting together the pieces.

    Thanks for the help,
    Graeme

  2. SiRa-wp
    Member
    Posted 2 years ago #

    Did you try to drop 'terms' in the WP_Query and replace the call for Permalink by this http://codex.wordpress.org/Function_Reference/get_term_link ?

  3. Gram3000
    Member
    Posted 2 years ago #

    Thanks for your reply,

    I'd like to display more than the link for the post. I have the 'the_title()' as a place holder for testing to see if the query is working.

    I'll be displaying custom fields, the_excerpt, the_title. I haven't understood that I could display post data from 'get_term_link' is that possible? I'm learning :)

  4. SiRa-wp
    Member
    Posted 2 years ago #

    Sorry, I can't tell that without testing and at the moment I can't acess my WP installation.

    When you use get_term_link in your code the way I suggested and replace '.$term->name.' with '.the_title().'. Does that give you a list of the titles? And add the_excerpt(); under the call for the link within <li></li>...

  5. Gram3000
    Member
    Posted 2 years ago #

    Tried removing the 'terms' from the query array and then replaced the 'the_permalink' with 'get_term_link'. For a moment as I was going through the steps I thought maybe it would work. It ended up removing all the post data.

    I feel like I'm close. I wish that I could get a variable from the 'the_terms' function and then pass it into the query to replace 'x'

    <?php the_terms( $post->ID, 'organizations', '', '', '' ); ?>

    This line the browser return my term 'x' but how can I use it with my query? What about this?

    $var = the_terms( $post->ID, 'organizations', '', '', '' );

    and then place it in the query.

    'tax_query' => array( array(
          'taxonomy' => 'organizations',
          'field' => 'slug',
          'terms' =>  '$var' ))
    )); ?>
  6. SiRa-wp
    Member
    Posted 2 years ago #

    Well, try it. If it tells you that it does not recognize $post, put global $post; in front of $var.
    But anyways, thinking this through: terms is still static, isn't it? And it always will be. Why add all those lines of extra code than?

  7. Gram3000
    Member
    Posted 2 years ago #

    I found almost prefect awesome solution here

    I have the term posts being displayed.

    place this in functions.php

    function get_posts_related_by_taxonomy($post_id,$taxonomy,$args=array()) {
    		  $query = new WP_Query();
    		  $terms = wp_get_object_terms($post_id,$taxonomy);
    		  if (count($terms)) {
    		    // Assumes only one term for per post in this taxonomy
    		    $post_ids = get_objects_in_term($terms[0]->term_id,$taxonomy);
    		    $post = get_post($post_id);
    		    $args = wp_parse_args($args,array(
    		      'post_type' => $post->post_type, // The assumes the post types match
    		      'post__in' => $post_ids,
    		      'post__not_in' => $post->ID,
    		      'taxonomy' => $taxonomy,
    		      'term' => $terms[0]->slug,
    		    ));
    		    $query = new WP_Query($args);
    		  }
    		  return $query;
    		}

    and this in the template

    $organiziation = get_posts_related_by_taxonomy($post->ID,'organizations');?>			
    
    <?php while ($organiziation->have_posts()): $organiziation->the_post();
    
    if( $post->ID == $do_not_duplicate ) continue;

    I'm learning! just. Thanks so much for taking time out of your day to help me.

    Any ideas on how to limit which category is displayed?

  8. SiRa-wp
    Member
    Posted 2 years ago #

    Welcome, though I did not help much.

    Categories should be ex-/included in args using 'cat' => '1, 2, 3' or 'cat' => 1,2,3, if you want to exclude them put a minus in front of them...

    Best wishes

  9. Murtaza Frosh
    Member
    Posted 2 years ago #

    Hello Gram3000;

    Thanks for the code man really helped me. Your code is working fine. I have used it on a product details page where it show all the products under a taxonomy term of the selected product. Now I need to hide the current selected product from the taxonomy term list. The code that you've given does not render the current post class so I can't use css to hide it. Please advice.

    Regards
    Murtaza Frosh

Topic Closed

This topic has been closed to new replies.

About this Topic