|
<?php |
|
/** |
|
* Outputs related posts with thumbnail |
|
* Works for Custom Post Types |
|
* |
|
* @author Samedi Amba |
|
* Code heavily borrowed from AJ Clarke's Total Responsive Theme |
|
*/ |
|
|
|
// Hook it into the Genesis Entry Footer |
|
add_action( 'genesis_entry_footer', 'church_related_posts' ); |
|
|
|
function church_related_posts() { |
|
|
|
// Return if disabled |
|
if ( ! get_theme_mod( 'staff_related', true ) ) { |
|
return; |
|
} |
|
|
|
// Vars |
|
global $post; |
|
$post_id = $post->ID; |
|
$post_count = get_theme_mod( 'staff_related_count', '3' ); /*3 corresponds to the number of posts to return. Can be any number*/ |
|
|
|
|
|
// Return if pass required |
|
if ( post_password_required() ) { |
|
return; |
|
} |
|
|
|
// Create an array of current category ID's |
|
$cats = wp_get_post_terms( $post_id, 'church_department' ); |
|
$cats_ids = array(); |
|
foreach( $cats as $church_related_cat ) { |
|
$cats_ids[] = $church_related_cat->term_id; |
|
} |
|
if ( ! empty( $cats_ids ) ) { |
|
$tax_query = array ( |
|
array ( |
|
'taxonomy' => 'church_department', /*The Custom Taxonomy church_department is used to return posts*/ |
|
'field' => 'id', |
|
'terms' => $cats_ids, |
|
'operator' => 'IN', |
|
), |
|
); |
|
} else { |
|
$tax_query = ''; |
|
} |
|
//end - array of current category IDs |
|
|
|
|
|
// Related query arguments |
|
$args = array( |
|
'post_type' => 'staff', /*This corresponds to the staff post type. Modify appropriately*/ |
|
'posts_per_page' => $post_count, |
|
'orderby' => 'rand', |
|
'post__not_in' => array( $post_id ), |
|
'no_found_rows' => true, |
|
'tax_query' => $tax_query, |
|
); |
|
$args = apply_filters( 'wpex_related_staff_args', $args ); |
|
$church_related_query = new wp_query( $args ); |
|
// end - related query arguments |
|
|
|
|
|
// If posts were found display related items |
|
if ( $church_related_query->have_posts() ) : ?> |
|
|
|
<div class="related-staff-posts "> |
|
|
|
<?php |
|
// Create counter var and set to 0 |
|
$church_count = 0; |
|
|
|
// Loop through related posts |
|
echo '<h3 class="related-title">Related Profiles</h3>'; |
|
|
|
foreach( $church_related_query->posts as $post ) : setup_postdata( $post ); |
|
// Counter for clearing floats |
|
$church_count++; |
|
$col_number = $church_count; |
|
|
|
switch ($col_number) { |
|
case 1: |
|
echo '<div class="one-third first">'; /*This applies to the first post to be returned |
|
substitute one-third with say one fourth, sixth etc, depending on the columns you want to |
|
return. More at: https://gist.github.com/studiopress/5700003*/ |
|
break; |
|
default: |
|
echo '<div class="one-third">'; /*applies to other classes apart from the first one*/ |
|
} /*Switch statement*/ |
|
|
|
$related .= '<a href="' . get_permalink() . '" rel="bookmark" title="View details about ' . |
|
get_the_title() . '">' . $img . get_the_title() . '</a>'; |
|
|
|
|
|
if ( $related ) { |
|
|
|
?> |
|
<!-- <li> --> |
|
<a href="<?php the_permalink(); ?>" title="Read more about <?php the_title_attribute(); ?>"> |
|
|
|
<?php the_post_thumbnail(); ?> |
|
|
|
<div class="related-staff-name"> |
|
<?php the_title(); /*The name of the church staff member */ ?> |
|
</div> |
|
</a> |
|
|
|
<div class="related-staff-position"> |
|
<?php |
|
$position = get_field( 'position' ); /*echoes a field generated by the Advanced Custom Fields Plugin*/ |
|
echo $position; |
|
?> |
|
</div> |
|
<!-- </li> --> |
|
</div> <!-- <div class="one-third"> --> |
|
<?php |
|
} //if ( $related ) |
|
|
|
// Reset loop counter |
|
if( $church_count == $post_count ) { |
|
$church_count = 0; |
|
} |
|
// Related posts loop ends here |
|
endforeach; |
|
|
|
?> |
|
|
|
</div><!-- .related-staff-posts --> |
|
|
|
<?php endif; |
|
wp_reset_postdata(); // This is what prevents duplicate/funny looking entries |
|
|
|
|
|
} //function church_related_posts() |
Feel free to critique, modify, debug and extend