Komoloves

Todo lo que pueda ser interesante, de entretenimiento con curiosidades y siguiendo la Actualidad y las noticias

1

Incluir Post Relacionados sin necesidad de Plugin

Ayuda Blogs y Wordpress, Ayuda Gadgets Utilidades, Como Hacer ..., Php - Css - Web, Plugins, Programacion

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

porkeno
Enviar a Twitter Enviar a Facebook
1 Comentario en “Incluir Post Relacionados sin necesidad de Plugin”
  1. Manolo dice:

    Parece buen post pero no definistes que codigo pondriamos para llamar la funcion hecha.

    La persona que no sabe sobre los codigos no entendera que funcion llamar lamentablemente tu post es copy&paste y de calificación te dejo -2

Algo que comentar

Suscripcion al Post por RSS