monkeybrain on "Help with the loop (custom post type taxonomy)"

ساخت وبلاگ

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 Idea 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!

WordPress ...
ما را در سایت WordPress دنبال می کنید

برچسب : نویسنده : استخدام کار wpss بازدید : 282 تاريخ : جمعه 31 ارديبهشت 1395 ساعت: 17:38