• Resolved handhugsdesign

    (@handhugsdesign)


    I have created a custom taxonomy called ‘type’ that is all working perfectly, but I am trying to create on the single pages a list of all the posts in whatever taxonomy the single post is in. For example, if I have a post called ‘My Cat’ and it is under the ‘type’ => ‘kittens’ , I want to show content from all the other posts in kittens automatically. I know i can do this by adding a if or statement at the end of the single.php for every taxonomy, but I want a way that it will just query the posts automatically based on whatever taxonomy the single post is in.

    Here is what I have been trying to do:
    RATHER THAN THIS which is too specific and won’t work automatically if I add a new taxonomy

    <?php query_posts( array( ‘type’ => ‘kittens’ , ‘order’ => ‘ASC’, ‘showposts’ =>-1 )); ?>

    I want to do something like this:

    <?php query_posts( array( ‘type’ => $term->slug , ‘order’ => ‘ASC’, ‘showposts’ =>-1 )); ?>
    but I can’t get it to work. I think it should be easy, but I need helpz.

    Thanks!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Michael

    (@alchymyth)

    Thread Starter handhugsdesign

    (@handhugsdesign)

    How? can you explain more? I tried putting lots of stuff into the query_posts (array…) area but I can’t get anything to work other than a specific taxonomy name.

    Michael

    (@alchymyth)

    <?php $terms = '';
    $type_terms = get_the_terms($post->ID,'type');
    if($type_terms) foreach( $type_terms as $type_term ) {
    $terms .= $type_term->slug . ', '; }
    query_posts( array( 'type' => $terms , 'order' => 'ASC', 'posts_per_page' =>-1 ));

    line-by-line:

    <?php $terms = '';
    reset the term list.

    $type_terms = get_the_terms($post->ID,'type');
    get the terms of the ‘type’ taxonomy of the single post.

    if($type_terms) foreach( $type_terms as $type_term ) {
    if the post has any terms of the ‘type’ taxonomy (there can be more than one), start a foreach loop.

    $terms .= $type_term->slug . ', '; }
    build a string, containing all terms of the ‘type’ taxonomy.

    query_posts( array( 'type' => $terms , 'order' => 'ASC', 'posts_per_page' =>-1 ));
    use the $terms string in the query.

    (using this method, you can get the ralated posts, even if there is more than one ‘type’ term in the post.)

    (not extemely tested)

    ps: don’t forget to add wp_reset_query(); after your loop; to restore the original query, just in case it is needed for any following code.

    Thread Starter handhugsdesign

    (@handhugsdesign)

    THANK YOU!! That worked perfectly. You are the best.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom taxonomy help’ is closed to new replies.