Skip to content

Instantly share code, notes, and snippets.

@iamchetanp
Created November 11, 2019 15:52
Show Gist options
  • Save iamchetanp/cc3b549cb3f546e2a3f32fb6bdf75bf6 to your computer and use it in GitHub Desktop.
Save iamchetanp/cc3b549cb3f546e2a3f32fb6bdf75bf6 to your computer and use it in GitHub Desktop.
Related Posts
//shortcode related products
add_shortcode('related_products','rel_prod');
function rel_prod(){
global $post;
ob_start();
?>
<div class="section section-post-related">
<div class="section_wrapper clearfix">
<?php
if (mfn_opts_get('blog-related') && $aCategories = wp_get_post_categories(get_the_ID())) {
$related_count = intval(mfn_opts_get('blog-related'));
$related_cols = 'col-'. absint(mfn_opts_get('blog-related-columns', 3));
$related_style = mfn_opts_get('related-style');
$args = array(
'category__in' => $aCategories,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'post__not_in' => array( get_the_ID() ),
'posts_per_page' => $related_count,
'post_status' => 'publish',
);
$query_related_posts = new WP_Query($args);
if ($query_related_posts->have_posts()) {
echo '<div class="section-related-adjustment '. esc_attr($related_style) .'">';
echo '<h4>'. esc_html($translate['related']) .'</h4>';
echo '<div class="section-related-ul '. esc_attr($related_cols) .'">';
while ($query_related_posts->have_posts()) {
$query_related_posts->the_post();
$related_class = '';
if (! mfn_post_thumbnail(get_the_ID())) {
$related_class = 'no-img';
}
$post_format = mfn_post_thumbnail_type(get_the_ID());
if (mfn_opts_get('blog-related-images')) {
$post_format = 'image';
}
echo '<div class="column post-related '. esc_attr(implode(' ', get_post_class($related_class))) .'">';
if (get_post_format() == 'quote') {
echo '<blockquote>';
echo '<a href="'. esc_url(get_permalink()) .'">';
the_title();
echo '</a>';
echo '</blockquote>';
} else {
echo '<div class="single-photo-wrapper '. esc_attr($post_format) .'">';
echo '<div class="image_frame scale-with-grid">';
echo '<div class="image_wrapper">';
echo mfn_post_thumbnail(get_the_ID(), 'related', false, $post_format);
echo '</div>';
if (has_post_thumbnail() && $caption = get_post(get_post_thumbnail_id())->post_excerpt) {
echo '<p class="wp-caption-text '. esc_attr(mfn_opts_get('featured-image-caption')) .'">'. wp_kses($caption, mfn_allowed_html('caption')) .'</p>';
}
echo '</div>';
echo '</div>';
}
echo '<div class="date_label">'. esc_html(get_the_date()) .'</div>';
echo '<div class="desc">';
if (get_post_format() != 'quote') {
echo '<h4><a href="'. esc_url(get_permalink()) .'">'. wp_kses(get_the_title(), mfn_allowed_html()) .'</a></h4>';
}
echo '<hr class="hr_color" />';
echo '<a href="'. esc_url(get_permalink()) .'" class="button button_left button_js"><span class="button_icon"><i class="icon-layout"></i></span><span class="button_label">'. esc_html($translate['readmore']) .'</span></a>';
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
}
wp_reset_postdata();
}
?>
</div>
</div>
<?php
$related_products = ob_get_contents();
ob_end_clean();
return $related_products;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment