Suporte » Temas e templates » posts relacionados sem plugin

  • Resolvido rware

    (@rware)


    Ola Amigos ! estou precisando de uma ajuda

    Estou usando o código abaixo no ficheiro functions.php do meu primeiro projeto em WordPress, ele funciona mais mostra tambem nos posts relacionados o post atual em exibição no single.php, como posso resolver essa situação?

    <?php
    function ll_related_posts() {
    $args = array(
    'posts_per_page' => 5,
    'post_in'  => get_the_tag_list(),
    );
    $the_query = new WP_Query( $args );
    echo '<section id="related_posts">';
    echo '<h2>Tambem podera gostar de: </h2> <br /><hr />';
    while ( $the_query->have_posts() ) : $the_query->the_post();
    ?>
    <section class="item">
    <?php if ( has_post_thumbnail() ) { ?>
    <section class="related_thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'related-post' ); ?></a></section>
    <?php } else { ?>
    <section class="related_thumb"><a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory')?>/lib/images/thumb.png" border="0" /></a></section>
    <?php } ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </section>
    <?php
    endwhile;
    echo '<div class="clear"></div></section>';
    wp_reset_postdata();
    }
    ?>
A visualizar 9 respostas - de 1 a 9 (de um total de 9)
  • RF

    (@rtbfreitas)

    Olá rware,

    altera o teu $args para o seguinte;

    $args = array(
      'posts_per_page' => 5,
      'post_in' => get_the_tag_list(),
      'post__not_in' => $post->ID
    );

    Experimenta e diz se funcionou.

    Um abraço

    Thread Starter rware

    (@rware)

    Olá rtbfreitas,

    Testei mais não funcionou. Ele ainda não filtra e exclui o post actual em exibição

    RF

    (@rtbfreitas)

    Olá rware,

    Com o código em cima já exclui o post actual?

    Thread Starter rware

    (@rware)

    rtbfreitas, Obrigado pela paciencia,
    mais ainda não exclui o post actual.

    RF

    (@rtbfreitas)

    Olá rware,

    Tente assim:

    $currentPost = get_the_ID();
    $args = array(
      'posts_per_page' => 5,
      'post_in' => get_the_tag_list(),
      'post__not_in' => $currentPost
    );
    Thread Starter rware

    (@rware)

    Olá rbtfreitas

    aparece os seguintes erros:
    Warning: array_map(): Argument #2 should be an array in

    e

    Warning: implode(): Invalid arguments passed

    RF

    (@rtbfreitas)

    E assim:

    $args = array(
      'posts_per_page' => 5,
      'post_in' => get_the_tag_list(),
      'post__not_in' => array( get_the_ID() )
    );
    Thread Starter rware

    (@rware)

    FUNCIONOU LINDAMENTE!!!
    Obrigado pela paciência rtbfreitas

    RF

    (@rtbfreitas)

    De nada, fico satisfeito por ter conseguido ajudar.

A visualizar 9 respostas - de 1 a 9 (de um total de 9)
  • O tópico ‘posts relacionados sem plugin’ está fechado a novas respostas.