Custom Taxonomy get most recent item of sub categories
-
Good Day,
I’m creating my own little photo gallery plugin.
I’ve created a custom post type ‘photo’ and a custom taxonomy ‘gallery’.
When I browse to /photo/ the archive-photo.php returns the list of all galleries using this query:<?php //for a given post type, return all $post_type = 'photo'; $tax = 'gallery'; $tax_terms = get_terms($tax); $tax_link = get_term_link($tax); if ($tax_terms) { foreach ($tax_terms as $tax_term) { $args=array( 'post_type' => $post_type, "$tax" => $tax_term->slug, 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'menu_order', 'order' => 'ASC', 'caller_get_posts'=> 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?>
This then links to /gallery/category1/ where taxonomy-gallery.php lists all items within that category1.
This works fine. But now I’d like to make it work with subcategories. So that when I visit /photo/ it only returns top level categories and when I visit /gallery/category1/ and ‘category1’ has subcategories it lists those subcategories like archive-photo.php does with top level categories.
————–
Now I’ve got three questions:1. I’d like to have the query above in archive-photo.php only return the top level categories. Is there an argument for the query similar to
($parent->term_id=="")
I could use?2. I thought to edit taxonomy-gallery.php with something like
if((sizeof($children)>0)) { // has child get_template_part( 'parts/photo-galleries-overview'); } else { get_template_part( 'parts/photo-gallery-items'); }
What would that code exactly look like?
3. For ‘parts/photo-galleries-overview’ I’d need a query that returns the first item of each subcategory within the current category. Similar to the query above (1.) but instead of returning top level categories, it should return only the subcategories of the current category.
Something like
'child_of' => $current_term->id,
or something like$tax_terms = get_terms($tax, array('child_of' => $current_term->id));
Where as $current_term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ), get_query_var( ‘id’ ) );I’m more of a design/front-end guy with basic understanding of php. So any help with working code snippets would be very much appreciated.
Also, is this too much to ask for? Should I hire someone to help me with this?
Thanks a lot
- The topic ‘Custom Taxonomy get most recent item of sub categories’ is closed to new replies.