• Resolved Mike Oberdick

    (@mike_oberdick)


    I’m working on a website for an artist. I have created a CPT called “artwork” and custom taxonomies for the attributes of each piece of work. One of the custom taxonomies is “album” which supports a hierarchy so that a user can navigate through the gallery. It is working somewhat here if you click on “2014” and then “Flowers”. What I’m missing is a thumbnail image for each folder that is pulled from the actual artwork (grandchild of parent term). Here is my code from the taxonomy-album.php file:

    <?php get_header(); ?>
    
    <div class="container clearfix">
    		<header class="entry-title">
    			<h1 class = "archive-title"><?php single_cat_title(); ?></h1>
    		</header>
    
    <h3 class = "category_description"><?php echo category_description(); ?> </h3>
    
    <div class = "taxonomy_thumbnails">
    
    <?php
    
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
    
    $parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
    
    $children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
    
    if(($parent->term_id!="" && sizeof($children)>0)) {
    
    // set the args for wp_list_categories
    
    $args = array(
        'child_of' => $term->term_id,
        'taxonomy' => $term->taxonomy,
    	'order' => DESC,
    	'hide_empty' => 0,
    	'hierarchical' => true,
    	'depth'  => 1,
    	'title_li' => ''
        );
    
    wp_list_categories( $args );
    
    }elseif(($parent->term_id!="") && (sizeof($children)==0)) {
    
    ?>
    
    <div class = "archive_thumbnail_box">
    
    <?php
    
    	$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
    	echo '<a href="' . $large_image_url[0] . '">';
    	the_post_thumbnail( 'thumbnail' );
    
    	echo '</a>';
    
    ?>
    
    <span><?php echo get_the_title(); ?></span>
    
    </div><!-- .archive_thumbnail_box -->
    
    <?php
    
    }elseif(($parent->term_id=="") && (sizeof($children)>0)) {
    
    // set the args for wp_list_categories
    
    $args = array(
        'child_of' => $term->term_id,
        'taxonomy' => $term->taxonomy,
    	'order' => DESC,
    	'hide_empty' => 0,
    	'hierarchical' => true,
    	'depth'  => 1,
    	'title_li' => ''
        );
    
    wp_list_categories( $args );
    
    }
    
    ?>
    
    </div> <!-- end of .taxonomy_thumbnails -->
    
    </div> <!-- end of container -->
    
    <?php get_footer(); ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    In the Flowers Gallery I see a thumbnail. Does this mean you’ve solved the problem? If not, more clarification is required.

    Thread Starter Mike Oberdick

    (@mike_oberdick)

    Thanks for the response…no that isn’t an issue. What I would like is a thumbnail to appear for the parent folders of that gallery. So in that case it would be the “Flowers” and “2014” folders which currently only have a link in a box.

    Moderator bcworkz

    (@bcworkz)

    So you need an ‘artwork’ post within the related sub-folder from which to get the attached image’s thumbnail. Use get_posts() with appropriate arguments (post_type, taxonomy term, etc.) to result in the proper artwork posts. Naturally you only need one result so set ‘posts_per_page’ to 1. Any WP_Query argument is acceptable here. Unless you specify otherwise, it’ll return the latest post meeting all other criteria.

    Once you have that post ID use get_attached_media() to get the image attachment ID, with which you can use wp_get_attachment_image() to get the actual img tag to echo out.

    Thread Starter Mike Oberdick

    (@mike_oberdick)

    The issue I’m running in to is that it’s grabbing a post from the first descendant which ultimately could be incorrect. For example see here where all of the years show the last post in one of the child subfolders. I think I was close along these lines here:

    <?php get_header(); ?>
    
    <div class="container clearfix">
    
    <header class="entry-title">
    <h1 class = "archive-title"><ul class = "gallery_breadcrumb"><?php be_taxonomy_breadcrumb(); ?></ul></h1>
    </header>
    
    <?php if (category_description( $category ) == '') : ?>
    
    <?php else : ?>
    
    <?php echo category_description( $category ); ?>
    
    <?php endif; ?>
    
    <?php
    
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
    
    $parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
    
    $children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
    
    if(($parent->term_id!="" && sizeof($children)>0)) {
    
    $term_children = get_terms(
    
    'album',
    array(
    
    'parent' => get_queried_object_id(),
    		'order' => 'DESC',
        )
    );
    
    if ( ! is_wp_error( $terms ) ) {
        foreach ( $term_children as $child ) {
    
    // get one random post in this child term
        $random_term_post = get_posts( array(
            'posts_per_page'    => 1, // only one post
            'orderby'           => 'rand', // selected randomly
            'tax_query'         => array(
                array(
                    'taxonomy'  => 'album', // from this child album
                    'terms'     => $child->term_id
                )
    
            )
    
        ) );
    
    echo '<a href="' . get_term_link( $child ) . '">
    <div class = "taxonomy_thumbnails">';
    echo get_the_post_thumbnail( $random_term_post[0], 'thumbnail' ); // show the random post's thumbnail
    echo '<p>' . $child->name .' </p>';
    echo '</div> <!-- .taxonomy_thumbnails -->';
    echo '</a>';
        }
    
    }
    
    }elseif(($parent->term_id!="") && (sizeof($children)==0)) {
    
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
    
    echo '<div class = "taxonomy_thumbnails">';
    echo '<a href="' . $large_image_url[0] . '">';
    echo the_post_thumbnail( 'thumbnail' );
    echo '</a>';
    echo '<p class = "artwork_info"><span style="font-weight:bold; font-style:italic;">'; echo get_the_title(); echo '</span>';
    
    $year_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'year-of-piece', ' &middot ', ', ', '' ) );
    echo $year_as_text;
    
    echo '</p>';
    
    $medium_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'medium', '', ', ', '' ) );
    echo $medium_as_text;
    
    $size_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'size', ' &middot ', ', ', '' ) );
    echo $size_as_text;
    
    echo '</div><!-- .taxonomy_thumbnails -->';
    
    endwhile; else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    
    <?php
    
    }elseif(($parent->term_id=="") && (sizeof($children)>0)) {
    
    $term_children = get_terms(
        'album',
        array(
            'parent' => get_queried_object_id(),
    
    		'order' => 'DESC',
        )
    
    );
    
    if ( ! is_wp_error( $terms ) ) {
        foreach ( $term_children as $child ) {
    
    // get one random post in this child term
    $random_term_post = get_posts( array(
      'posts_per_page'    => 1, // only one post
            'orderby'           => 'rand', // randomly selected
            'tax_query'         => array(
                array(
                    'taxonomy'  => 'album', // from this child album
                    'terms'     => $child->term_id
                )
            )
    
        ) );
    
    echo '<a href="' . get_term_link( $child ) . '">
    <div class = "taxonomy_thumbnails">';
    echo get_the_post_thumbnail( $random_term_post[0], 'thumbnail' ); // show the random post's thumbnail
    echo '<p>' . $child->name .' </p>';
    echo '</div> <!-- .taxonomy_thumbnails -->';
    echo '</a>';
        }
    
    }
    
    }
    
    ?>
    </div> <!-- end of container -->
    <?php get_footer(); ?>
    Moderator bcworkz

    (@bcworkz)

    WP_Query includes some powerful date arguments for your use in further restricting get_posts() results. I’m assuming doing so will return a suitable painting image. Without an understanding of how your taxonomy is actually used it’s difficult to make other suggestions. Using get_terms() with the ‘parent’ argument will restrict results to one level, where ‘child_of’ returns all levels. One can drill down any number of levels by using ‘parent’ repeatably.

    If artwork post type is hierarchical you have other options with the ‘post_parent’ argument, restricting results to paintings with a particular parent just as is done with terms.

    Thread Starter Mike Oberdick

    (@mike_oberdick)

    Thank you cbworkz for all the info…I’m banging my head against the wall but know I am close. I think I’m one level off because the 2015. 2014, and 2013 folders all show the same art thumbnail. How would I use parent repeatedly as you mentioned? “Artwork” post type is not hierarchical as the codex said doing so with many posts (e.g. over a hundred) would slow the queries. I am setup with a cpt of “artwork” with custom taxonomies associated. The one I’m targeting is album which supports a hierarchy. So I upload an image to the cpt artwork called sample artwork. I then give it an album name within the hierarchical taxonomy called “album”. So it might be Paintings->2015->Still-Life->Flowers where Flowers is the actually gallery name and the page that will display all of the images in that gallery. Essentially “sample artwork” is in the following terms: paintings, 2015, still-life, and flowers…correct? Here is my code if you can offer a tweak to get this working I’m all ears and very grateful, believe me!

    $term_children = get_terms ( //array of the terms
    'album',
    array(
    'parent' => get_queried_object_id(), //get terms based on the parent
    'order' => 'DESC', //order them descending so newest year is first
      )
        );
    
    if ( ! is_wp_error( $terms ) ) {
        foreach ( $term_children as $child ) { //iterate over terms and return each as their own using $child variable
    
    // grab one post from a child term
    $thumbnail_post = get_posts( array(
            'posts_per_page'    => 1, // only one post
            'tax_query'         => array(
                array(
                    'taxonomy'  => 'album', // from this taxonomy
                    'parent'     => $child->term_id
            )
            )
            )
            );
    
    echo '<a href="' . get_term_link( $child ) . '">
    <div class = "taxonomy_thumbnails">';
    echo get_the_post_thumbnail( $thumbnail_post[0], 'thumbnail' ); // show the random post's thumbnail
    echo '<p>' . $child->name .' </p>';
    echo '</div> <!-- .taxonomy_thumbnails -->';
    echo '</a>';
        }
    
    }
    Moderator bcworkz

    (@bcworkz)

    To drill down through the term children by generation we use the ‘parent’ argument. Start with the term ID of the current level as parent for the next get_terms() call. This will return only the immediate children, no grandchildren etc. Repeat as necessary with each level.

    So if the current term is ‘2015’, calling get_terms() with 2015’s ID as parent will yield ‘Still-life’ and it’s immediate siblings, but not ‘Flowers’ etc. Call get_terms() once more with one of those IDs as parent, such as the ID of Still-life. This will return ‘Flowers’ and its siblings. Presumably any thumb from these terms will work for your needs, you may as well grab the first one.

    BTW, the issue with hierarchical post types isn’t the query speed, it’s that you’ll run into memory issues when you load the hierarchical post type’s list table in admin. Even though it only displays 20 some posts, it retrieves all of them to ensure all descendants can be listed together. When there’s a lot of metadata involved with the posts or just a lot of posts, the available memory can be eaten up rather quickly. Still a problem, but not quite what you were thinking. Yeah, I know, I’m being pedantic. Yup, that’s me!

    Thread Starter Mike Oberdick

    (@mike_oberdick)

    Still no idea how this is supposed to work…finally gave in and grabbed a plugin called Categories Images. Worked like a charm. I’d love to see WordPress support this feature out of the box in the future. Seems like something many people would like to use.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Art Gallery using Custom Post Type and Custom Taxonomy’ is closed to new replies.