• Resolved monkeybrain

    (@monkeybrain)


    Hi everybody,

    For my custom post type ‘photos’ and its taxonomy ‘photo-gallery’ I’ve created a taxonomy-photo-gallery.php

    Now I figured out how to return the posts from a certain term using this:

    <?php $term_loop = new WP_Query( array(
        'post_type' => 'photos',
        'photo-gallery' => 'black-and-white',
        )
      );
    ?>
    <?php the_thumbnail() ?>
    <?php endwhile; ?>

    How can I make this work for all terms? How can I modify the line 'photo-gallery' => 'black-and-white'', to take the name of the gallery dynamically from the URL (which in this case is /gallery/black-and-white/)

    Is there something like ‘photo-gallery’ => $term->slug, so that my taxonomy-photo-gallery.php works for all new terms that are created?

    Thanks a lot!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter monkeybrain

    (@monkeybrain)

    I still haven’t found a solution for this. If someone has an idea, help would be very much appreciated.

    Thanks!

    TrishaM

    (@trisham)

    Hi @monkeybrain

    1. It’s always recommended that you NOT post a second time when you have an unswered question (unless it’s to post your solution). The reason is that while this might (very) temporarily bump your question up, it doesn’t always help you. Many of the more advanced WP users and developers focus on the list of ‘no replies’ (those posts that have no responses) in order to help those whom no one else is helping (as we have time, which often requires patience on your part). If you respond to your own initial post, it drops off the list of ‘no replies’ and we assume someone else is helping you…..which actually slows the likelihood of getting real help.

    2. ‘photo-gallery’ => ‘black-and-white’ is not valid. In your code you might use something like ‘taxonomy’ => ‘photo-gallery’, but it all depends on what you’re trying to do.

    SO what are you trying to do? Display all the photos that are tagged (in this example) as ‘black-and-white’? Or just display the terms associated with each photo in the gallery (to show what other terms a particular photo in this gallery is tagged with?)

    Knowing what you’re trying to do on your template page will help us to come up with the correct solution.

    Thread Starter monkeybrain

    (@monkeybrain)

    Hi TrishaM

    Thanks for your reply!

    1. Sorry about that, makes sense, thanks.

    2. I have a custom post type named ‘photos’ which has a custom taxonomy called ‘photo-gallery’. For each taxonomy term (e.g. ‘black and white’, ‘portrait’, etc…) I’d like to have an overview which lists all photos that are within that term.

    So, what I’m trying to do is list all photos that are tagged with ‘black-and-white’ but also not hardcode the name of the tag, so that the template file works with all future tags/terms.
    I first thought that I could use

    <?php if ( have_posts() ): ?>
    <?php while ( have_posts() ) : the_post(); ?>

    because I thought taxonomy-photo-gallery.php is associated with all the terms that I create within the taxonomy ‘photo-gallery’, but that does not seem to work as expected. So I tried and looked around on how to return the photos for one term and then go from there and adapt it for all other terms.

    If you see my mistake in code or thinking, any help would be greatly appreciated.

    Thanks!

    TrishaM

    (@trisham)

    Great! Now that I understand what you’re trying to do, I *may* be able to help, as I’m doing something similar on one site, where I have a custom post type with a custom taxonomy, and for each taxonomy I want to list the post titles……I think you can modify this code to do what you want (I added some comments to my code and I’ll explain more below):

    $terms = get_terms( 'retail_category', array('orderby' => 'name', 'order' => 'ASC') );

    The first thing is to get all the terms in your custom taxonomy – in this part you would replace ‘retail-category’ with your custom taxonomy which is ‘photo-gallery’ – I’m sorting by name, which makes sense in your case too, but you can change the order by to whatever you like.

    if ($terms) {
         foreach($terms as $term) {
         $args = array(
          'post_type' => 'retail',
          'posts_per_page' => -1,
          'ignore_sticky_posts' => 1,
          'orderby' => 'title',
          'order' => 'ASC',
          'tax_query' => array(
                array(
                'taxonomy' => 'retail_category',
                'field' => 'name',
                'terms' => $term->name)
          )
        );

    In this section I am setting up my arguments for doing a new query (below) to find ALL (the -1) of the related custom posts fore EACH taxonomy term where the assigned taxonomy term matches (by name) the taxonomy in the FOREACH statement – my CPT is ‘retail’, yours is ‘photos’ (note: it’s a better practice to name your CPTs with the singular term, not the plural, so your CPT really *should* be named ‘photo’ as each post is one photo, right? But honestly it’s your call, you can keep it plural if you like 🙂 )

    global $post;
        $myposts = new WP_Query($args);
          if ( $myposts->have_posts() ) {
            echo '<ul><li><h5 class="retailcatname">' . $term->name . '</h5><ul>';

    Here is the query and if it finds posts within a term, then I’m echoing the name of the Taxonomy as a ‘header’ for the list of post titles that appear below it, you may not need to do this – and of course I’m using the UL and LI because mine is a real ‘list’. If you don’t want a header with the taxonomy term name, just leave out this echo and move right into the “while” part below.

    while( $myposts->have_posts() ) : ($myposts->the_post());
               echo '<li><a href="<?php the_permalink() ?>">';
               echo the_title();
               echo '</a></li>';
             endwhile;

    SO while I have matching posts for the taxonomy term, I list each and display it’s title as a link (A). You can display whatever you want, the thumbnail and/or the content. NOTE THAT the “global $post” in the above section is required to access the post data.

    echo '</ul></li></ul>';
       }
     }
    }

    Closing the list….not necessary if you’re not using a list.

    I think if you modify this to use your custom taxonomy (photo-gallery) and custom post type (photos) and then in the section with the “while” statement just echo what you want displayed, this might work for you.

    Good luck!

    Thread Starter monkeybrain

    (@monkeybrain)

    Hi TrishaM,

    Thanks a lot, but if I understand correctly this is not exactly what I’m trying to do. I’m trying to create multiple photo galleries, each with its own url/page.

    I actually have an archive-photos.php with a query very similar to your code, except the ‘posts_per_page’ => 1, … this page will be used as an overview of all terms/galleries, from there I link to < a href=”/photo-gallery/<?php echo $tax_term->slug; ?>/” on the most recent photo thumbnail within that term. This terms/galleries overview will be found under /photos/ (Because there the user finds all photos is why I called the CTP photos and not photo, your reasoning makes sense though)

    What I’m now trying to do is a template file that lists the photos from within ONE term, so if I understand correctly, no foreach($terms as $term). It should work like /tag/black-and-white/ or /category/black-and-white/ works for normal WordPress posts.

    What confuses me is that the normal tag.php or category.php of WordPress uses a simple

    <?php if ( have_posts() ): ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php endwhile; ?>
    // Content like <?php the_title(); ?> etc...
    <?php else: ?>
    	No posts to display
    <?php endif; ?>

    but if I use this for my custom taxonomy and browse to /photo-gallery/black-and-white/, it returns “No posts to display”.

    And with my code

    <?php $term_loop = new WP_Query( array(
        'post_type' => 'photos',
        'photo-gallery' => 'black-and-white',
        )
      );
    ?>
    <?php the_thumbnail() ?>
    <?php endwhile; ?>

    the taxonomy-photo-gallery.php returns all photos within the term ‘black-and-white’. That’s why I thought if I could replace that line with something that grabs the term name from the URL, it would work for all terms (/photo-gallery/black-and-white/ aswell as /photo-gallery/portrait/ and all other terms/galleries.)

    Any ideas on how to solve that?
    And sorry if my explanation is not clear, English is not my first language.

    Thanks so much for your help!

    edit: This is the code I use to create the custom taxonomy, maybe there is something wrong with it?

    function build_photo_taxonomies() {
        register_taxonomy( 'photo-gallery', 'photos',
        	array(
        		'label' => 'Gallery',
                'public' => true,
        		'hierarchical' => true,
        		'query_var' => true
        		)
        	);
    }

    TrishaM

    (@trisham)

    May I just say that your English is actually very good – I’d never have guessed it isn’t your native language.

    And, apologies if I’ve misunderstood your intial post.

    The taxonomy template you’ve created (taxonomy-photo-gallery.php) *should* work fine without needing a query…..

    So in this example you’ve posted

    <?php if ( have_posts() ): ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php endwhile; ?>
    // Content like <?php the_title(); ?> etc...
    <?php else: ?>
    	No posts to display
    <?php endif; ?>

    All you should need to do is replace //Content like[…] with the_thumbnail and whatever else you want displayed (it sounds like just the image)……you might have to add global $post but aside from that it should work.

    Now that said, your query above will never display anything because you’re missing the very vital part “if have posts,while have post, the post”….without establishing ‘the post’ WP won’t know what ‘the_thumbnail’ is….it needs to look more like this:

    <?php $args = array(
        'post_type' => 'photos',
        'photo-gallery' => 'black-and-white'
      );
    $term_loop = new WP_Query($args);
    if(have_posts() : while(have_posts() : the_post(); ?>
    <div class="image">
    <?php the_thumbnail() ?>
    </div>
    <?php endwhile; ?>

    BUT you shouldn’t need that – the standard WP query and taxonomy-photo-gallery.php template should work out of the box by just copying the tag.php file and only leaving in the bits you want for the content.

    Lastly, you don’t need a template for each term – you could make one, but it isn’t necessary, if WP doesn’t find one it falls back to the taxonomy template you already created….but you don’t need to repeat the query on that page – WP should just use the template to display *any* content it finds that has that term applied.

    Check out this page on template hierarchy:
    https://developer.wordpress.org/themes/basics/template-hierarchy/

    In particular this section of the page:
    http://developer.wordpress.org/themes/functionality/categories-tags-custom-taxonomies/

    I really hope this helps, if not I’ll try to get the attention of a couple of forum moderators on this.

    Thread Starter monkeybrain

    (@monkeybrain)

    Hi TrishaM,

    Thanks for your reply! And thanks for the compliment regarding my English skills. Whenever misunderstandings happen I tend to think it’s because of not being a native English speaker.

    1. Oddly enough, my query above worked even without defining the_post, but it wasn’t dynamic and only worked for the defined term ‘black-and-white’.

    2. First off, I followed your advice and renamed the cpt to photo and also renamed the taxonomy to gallery … I then tried to find the problem with the normal

    <?php if ( have_posts() ): ?>
    <?php while ( have_posts() ) : the_post(); ?>

    approach. And while trying to find the problem I thought I’d test the custom taxonomy for normal posts and see if that works, and to my surprise it does.
    So, when I change

    register_taxonomy( 'gallery', 'photo',
        	array(

    to

    register_taxonomy( 'gallery', 'post',
        	array(

    the taxonomy-gallery.php template works as expected (using the normal WP posts).
    When I try to use it for the cpt photo it doesn’t.

    Now I wonder if the solution could be to tell the taxonomy-gallery.php loop to somehow use the post_type ‘photo’ and not ‘post’. (Even after changing register_taxonomy( 'gallery', 'post', back to register_taxonomy( 'gallery', 'photo', the page still displays the posts. It really seems like it is only looking for posts and not post_type photo at all.

    Do you have any ideas on how to get the attention of a mod?

    Thanks a lot! I feel like I’m really close to the solution. 🙂

    Thread Starter monkeybrain

    (@monkeybrain)

    I found a solution! When I put this into my taxonomy-photo.php it displays the correct posts from the photo cpt.

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
    <?php query_posts(array( 'post_type'=>'photo', 'gallery'=>$term->slug)); ?>
    <?php if ( have_posts() ): ?>
    <?php while ( have_posts() ) : the_post();?>

    The only thing I’m not sure about is using query_posts since I read somewhere that it shouldn’t be used. But a quick try with WP_Query did not work. If someone has a solution that works with WP_Query, let me know.

    Cheers and thanks again for the help.

    Thread Starter monkeybrain

    (@monkeybrain)

    Marking this as resolved, but feel free to let me know of a better solution using WP_Query.
    Thanks!

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

The topic ‘Help with the loop (custom post type taxonomy)’ is closed to new replies.