Instantly share code, notes, and snippets.
Created
August 16, 2018 09:00
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save fencermonir/dda2561f86a88fd5b94342e6f26a122b to your computer and use it in GitHub Desktop.
This script is for showing related post in WordPress.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* | |
* Get Related Posts | |
*function to get related post having codition inside | |
* | |
*/ | |
function get_related_post($postID){ | |
$themeTemplateDirectoryUri = get_template_directory_uri(); | |
?> | |
<div class="row"> | |
<?php | |
$tags = wp_get_post_tags($postID); | |
$foundPosts = 0; | |
$excludes=[$postID]; | |
if ($tags) { | |
$tag_ids = array(); | |
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; | |
$args=array( | |
'tag__in' => $tag_ids, | |
'post__not_in' => array($postID), | |
'showposts'=> 3, | |
'ignore_sticky_posts'=>1 | |
); | |
$my_query = new WP_Query($args); | |
$foundPosts = $my_query->found_posts; | |
if( $my_query->have_posts() ) { | |
while ($my_query->have_posts()) : $my_query->the_post(); | |
$excludes[]=$my_query->post->ID; | |
$thumbImage = wp_get_attachment_url(get_post_thumbnail_id($my_query->post->ID) ); | |
$image = ( $thumbImage && $thumbImage != '' ) ? $thumbImage : $themeTemplateDirectoryUri.'/img/product-img6.jpg'; | |
?> | |
<div class="col-sm-4"> | |
<div class="related-post-content area"> | |
<a href="<?php the_permalink(); ?>"><img src="<?php echo $image; ?>" alt="" class="img-responsive"></a> | |
<a href="<?php the_permalink(); ?>"><h4 class="font-bold"><?php the_title(); ?></h4></a> | |
<?php the_excerpt(); ?> | |
</div> | |
</div> | |
<?php | |
endwhile; | |
} | |
wp_reset_query(); | |
} | |
if ($foundPosts <3){ | |
$remainingPosts = 3 - $foundPosts; | |
get_category_posts($postID, $remainingPosts, $excludes); | |
} | |
?> | |
</div> | |
<?php | |
} | |
//Get remaining post from category by that given post id | |
function get_category_posts($postID, $remainingPosts, $excludes){ | |
$themeTemplateDirectoryUri = get_template_directory_uri(); | |
$catArgs = array( | |
'category__in' => wp_get_post_categories($postID), | |
'showposts' => $remainingPosts, | |
'post__not_in' => $excludes | |
); | |
$cat_post_query = new WP_Query($catArgs); | |
$foundPosts = $cat_post_query->found_posts; | |
if( $cat_post_query->have_posts() ) { | |
while ($cat_post_query->have_posts()) : $cat_post_query->the_post(); | |
$excludes[]=$cat_post_query->post->ID; | |
$thumbImage = wp_get_attachment_url(get_post_thumbnail_id($cat_post_query->post->ID) ); | |
$image = ( $thumbImage && $thumbImage != '' ) ? $thumbImage : $themeTemplateDirectoryUri.'/img/product-img6.jpg'; | |
?> | |
<div class="col-sm-4"> | |
<div class="related-post-content area"> | |
<a href="<?php the_permalink(); ?>"><img src="<?php echo $image; ?>" alt="" class="img-responsive"></a> | |
<a href="<?php the_permalink(); ?>"><h4 class="font-bold"><?php the_title(); ?></h4></a> | |
<?php the_excerpt(); ?> | |
</div> | |
</div> | |
<?php | |
endwhile; | |
} | |
wp_reset_query(); | |
if ($foundPosts < $remainingPosts){ | |
$stillRemaing = $remainingPosts-$foundPosts; | |
get_latest_posts($excludes, $stillRemaing); | |
} | |
} | |
//Get rest of the posts from latest post types | |
function get_latest_posts($excludes, $stillRemaing){ | |
$latestPost = array( | |
'showposts' => $stillRemaing, | |
'post__not_in' => $excludes, | |
'post_type' => 'post', | |
'posts_per_page' => $stillRemaing, | |
'offset'=>0, | |
'orderby' => 'post_date', | |
'order' => 'DESC', | |
); | |
$latest_post_query = new WP_Query($latestPost); | |
if( $latest_post_query->have_posts() ) { | |
while ($latest_post_query->have_posts()) : $latest_post_query->the_post(); | |
$thumbImage = wp_get_attachment_url(get_post_thumbnail_id($latest_post_query->post->ID) ); | |
?> | |
<div class="col-sm-4"> | |
<div class="related-post-content area"> | |
<a href="<?php the_permalink(); ?>"><img src="<?php echo $image; ?>" alt="" class="img-responsive"></a> | |
<a href="<?php the_permalink(); ?>"><h4 class="font-bold"><?php the_title(); ?></h4></a> | |
<?php the_excerpt(); ?> | |
</div> | |
</div> | |
<?php | |
endwhile; | |
} | |
wp_reset_query(); | |
} | |
//echo get_related_post(get_the_id()); | |
//echo get_related_post($post->ID); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment