• Resolved srpd

    (@srpd)


    Hi, i’m trying to show random post in single.php behind the content using this function :

    <? $quantipost = '10'; // totale dei post da mostrare ?>
    <h2> <?=$quantipost; ?> Post a caso</h2>
        <ul>
          <?php
     $rand_posts = get_posts('numberposts=$quantipost&orderby=rand');
     foreach( $rand_posts as $post ) :
     ?>
          <li><a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
            </a></li>
          <?php endforeach; ?>
        </ul>

    this function works but conflict with comments… comments that are showed are not from post but random…
    how can i solve this? thanks in advance.

Viewing 10 replies - 1 through 10 (of 10 total)
  • @srpd,

    Have you had a look at the following Codex article?
    http://codex.wordpress.org/Template_Tags/get_posts

    r

    Thread Starter srpd

    (@srpd)

    hi rschilt, thanks for answer.. yes i’ looked that, and the problem is the same… the code work but post’s comment are showed random..

    @srpd,

    You might need to have another look…
    I don’t see global $post; anywhere in your code.

    R

    My apologies @srpd,

    I can now see the code that you have copied from the Codex article…

    Perhaps in single.php after your code there’s some code to display comments eg: comments.php. Is this the case?

    R

    Thread Starter srpd

    (@srpd)

    oh don’t worry rschilt 🙂

    well in comments.php my code is :

    <?php if ( have_comments() ) : ?>
    
    <div class="lista_commenti">
    <h3>Commenti per questo articolo.</h3>
    <?php foreach ($comments as $comment) : ?>
    
    <div class="lista_commenti_1">
    <div class="lista_commenti_2" >
    <span style="color:#f58b11; font-weight:bold;"><?php comment_author_link() ?> scrive:</span>
    <?php comment_text() ?>
    </div>
    </div>
    <?php endforeach; /* end for each comment */ ?>
    </div>

    let’s see if i understand this right:
    first – you show single post
    second – you show 10 random posts
    third – you want to show comments for first single post.

    in this case you could try and save the query from the first single post, and restore it after your random posts:

    <?php $temp = $wp_query; // save original query ?>
    
    <? $quantipost = '10'; // totale dei post da mostrare ?>
    <h2> <?=$quantipost; ?> Post a caso</h2>
        <ul>
          <?php
     $rand_posts = get_posts('numberposts='.$quantipost.'&orderby=rand');
     foreach( $rand_posts as $post ) :
     ?>
          <li><a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
            </a></li>
          <?php endforeach; ?>
        </ul>
    
    <?php $wp_query = $temp; // restore original query ?>

    not tested 😉
    if it does not work, please copy the code of the whole single.php into a pastebin and post the link here.

    Thread Starter srpd

    (@srpd)

    hi alchymyth yes you are right. it is what i want to do.thanks… i tried your suggestion but it doesn’t work, probably something wrong in my code.
    I post my single.php and my comments.php on pastebin 🙂

    try and put the code to save the original query into line 11 of single.php:

    <div id="singolo" style="border-top:5px solid <?php echo $color_codes_text[$colore_pagina_singola]; ?>">
       <?php $temp = $wp_query; // save original query  // ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    and then the line to restore the query just before you call the comments (line 62):

    <?php endwhile; else: ?>
    
    <p><?php _e('Sorry, no posts matched your criteria.', 'kubrick'); ?></p>
    
    <?php endif; ?>
    <?php $wp_query = $temp; // restore original query ?>
    <?php comments_template(); ?>
    </div>

    that’s my last idea on this, good luck 😉

    Thread Starter srpd

    (@srpd)

    thanks i really appreciate, but doesn’t work 🙁

    Thread Starter srpd

    (@srpd)

    uhmmm i solved putting the code of random post in top of comments.php and not in single.php 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘random post’ is closed to new replies.