Skip to content

Instantly share code, notes, and snippets.

@muhfaris
Created October 17, 2016 05:16
Show Gist options
  • Save muhfaris/6f47819b1afa6a3ee13c2038c53d072f to your computer and use it in GitHub Desktop.
Save muhfaris/6f47819b1afa6a3ee13c2038c53d072f to your computer and use it in GitHub Desktop.
Post related by tag, ini fungci untuk menampilkan post berdasarkan tag, yang akan di ambil hanya judul, link saja, tdk ada yg lainnya.
function ci_get_related_posts( $post_id, $related_count, $args = array() ) {
$args = wp_parse_args( (array) $args, array(
'orderby' => 'rand',
'return' => 'query', // Valid values are: 'query' (WP_Query object), 'array' (the arguments array)
) );
$related_args = array(
'post_type' => get_post_type( $post_id ),
'posts_per_page' => $related_count,
'post_status' => 'publish',
'post__not_in' => array( $post_id ),
'orderby' => $args['orderby'],
'tax_query' => array()
);
$post = get_post( $post_id );
$taxonomies = get_object_taxonomies( $post, 'names' );
foreach( $taxonomies as $taxonomy ) {
$terms = get_the_terms( $post_id, $taxonomy );
if ( empty( $terms ) ) continue;
$term_list = wp_list_pluck( $terms, 'slug' );
$related_args['tax_query'][] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term_list
);
}
var_dump($terms);
if( count( $related_args['tax_query'] ) > 1 ) {
$related_args['tax_query']['relation'] = 'OR';
}
if( $args['return'] == 'query' ) {
return new WP_Query( $related_args );
} else {
return $related_args;
}
}
function view_post(){
$related = ci_get_related_posts( get_the_ID(), 5 );
if( $related->have_posts() ):?>
<div class="scrollbar"> <div class="bar"></div></div>
<div id="top_artikel" class="horisontal">
<ul>
<?php $no=1;while( $related->have_posts() ): $related->the_post(); ?>
<li><div class="top_nomer"><?php echo $no;?></div><a href="<?php the_permalink();?>">
<span><?php potong_kar(get_the_title(),55,'...');?></span></a>
</li>
<?php
$no +=1;
endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_postdata();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment