Incluir Post Relacionados sin necesidad de Plugin
La verdad que el Plugin Related Post no me ha funcionado desde que actualicé los blogs, por eso me puse a investigar hasta que encontré un estupendo código para mostrar 5 artículos relacionados por Categorías o por Tags
La idea es que incluyamos el siguiente código en el fichero functions.php y llamemos a la función desde donde queramos.
function RelatedByTags()
{
$tags = wp_get_post_tags(get_the_ID());
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(get_the_ID()),
'showposts'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
}
function RelatedByCategories()
{
$categories = get_the_category(get_the_ID() );
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array(get_the_ID()),
'showposts'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
}
Artículo original de bin-co
1 Comentario en “Incluir Post Relacionados sin necesidad de Plugin”
Algo que comentar
Tambien puede interesarte