Skip to content

Instantly share code, notes, and snippets.

@mikeoberdick
Last active December 10, 2015 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeoberdick/5f69aa0586d45e3281e0 to your computer and use it in GitHub Desktop.
Save mikeoberdick/5f69aa0586d45e3281e0 to your computer and use it in GitHub Desktop.
Custom Taxonomy Archive Code
<?php get_header(); ?>
<div class="container clearfix">
<header class="entry-title">
<h1 class = "archive-title"><?php single_cat_title(); ?></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)) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo '<a href="' . $large_image_url[0] . '">';
echo '<div class = "taxonomy_thumbnails">';
echo the_post_thumbnail( 'thumbnail' );
echo '<p>';
echo get_the_title();
echo '</p>';
echo '</div><!-- .taxonomy_thumbnails -->';
echo '</a>';
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(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment