• 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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You will need to adopt the array format for arguments to get_terms(). This means assigning $tax like this:
    $tax = array( 'taxonomy'=>'gallery',);
    Doing this does not alter your code’s functionality, it’s just a different way to achieve the same thing. However, the array format is not valid for get_term_link(). You do not appear to be using the term link result anyway.

    Top level terms always have their parent ID value set to 0 (zero). So you can add 'parent'=> 0, to the argument array to get only top level terms:

    $tax = array(
       'taxonomy'=>'gallery',
       'parent'=> 0,
    );

    How you decide if a term has children or not depends on whether you want any child term, even if there are no posts using it, or if you want child terms only if there are posts using it. For the first you use get_terms() where the parent ID is set to the current term ID.

    For the latter you use get_posts() with ‘tax_query’ arguments. There are different ways to determine the presence of posts with child terms. Which way you go depends on whether you want posts using the current term or not in the results.

    Once you’ve collected the results in a variable, you can use that variable alone as a test for if there is any content because an empty array returns false.

    if ( $results ) get_template_part('has-children');
       else get_template_part('no-children');

    To only use the first post in a child category, you can use get_posts() as mentioned above and just pick the first post out of the results.
    echo get_permalink( $results[0]->ID );

    If you have a lot of posts, getting all matching posts to only use one is not very efficient. You can use the 'posts_per_page'=> 1, argument with get_posts() to limit the number of posts returned to one.

    Should you hire someone? You’ll get this done a lot faster that way, but if you have the time and want to improve your PHP skills, it’s worth tackling this yourself. If you break the task down into small achievable parts, you’ll eventually get the entire thing coded and learn a lot along the way. It’ll probably be frustrating at times, but if you get stuck you know where to get help ??

    Thread Starter monkeybrain

    (@monkeybrain)

    Hey @bcworkz,

    Thanks a lot! I’ll give it a try but am not great with frustrating moments and might end up hiring someone to help me with this. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Taxonomy get most recent item of sub categories’ is closed to new replies.