How to show a listing of all posts on any page

1

I want to make a list to get the most commented posts, but my problem is that I can not call the right listing always, here's the code:

        <ul>
        <?php 
            global $query_string;
            query_posts($query_string.'&posts_per_page=-1, post_status=publish');

            if (have_posts()): while (have_posts()): the_post();

            $post_name = get_the_title();
            $post_url  = get_permalink();
        ?>
            <li>
                <a class="transition-2s" title="<?php $post_name; ?>" href="<?php $post_url; ?>" rel="bookmark"> 
                    <span class="reclink"><?php echo $post_name; ?></span>
                </a>
            </li>
        <?php 
            endwhile; 
            endif;
            wp_reset_query();
        ?>
    </ul>

On the homepage it works great, but on a specific page, such as a post, for example, this code only lists a single item, which is the one of the post in question.

I want to put this code in the footer, so that it always appears, regardless of which page the reader is on.

Thank you in advance!

    
asked by anonymous 02.04.2015 / 01:50

1 answer

2

There are some problems here, in the title you say you want to list all, but in the description you are saying that you want to list the most comments ...

Anyway, this is not going to work because you are catching $query_string which changes from page to page.

In addition you should not use query_posts() , just read in the documentation that the correct one is to use pre_get_posts to change loops that already exist and additional loops must be done with WP_Query .

To list all you could use:

<ul>
    <?php 
        $all_posts_query = new WP_Query( array(
            'nopaging' => true, // deve ser usado no lugar de posts_per_page -1
            'post_status' => 'publish'
        ) );

        if ( $all_posts_query->have_posts() ) :
            while ( $all_posts_query->have_posts() ) :
                $all_posts_query->the_post();
            ?>

            <li>
                <a class="transition-2s" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>" rel="bookmark"> 
                    <span class="reclink"><?php the_title(); ?></span>
                </a>
            </li>

            <?php endwhile;
        endif;
        wp_reset_postdata();
    ?>
</ul>

It's pretty simple now to also change this to get the most commented posts , as you simply sort by the number of comments.

<ul>
    <?php 
        $all_posts_query = new WP_Query( array(
            'nopaging' => true, // deve ser usado no lugar de posts_per_page -1
            'post_status' => 'publish',
            'orderby' => 'comment_count', // Ordena pelos mais comentados
            'order' => 'DESC' // Começa pelos números maiores, exemplo 10, 9, 8...
        ) );

        if ( $all_posts_query->have_posts() ) :
            while ( $all_posts_query->have_posts() ) :
                $all_posts_query->the_post();
            ?>

            <li>
                <a class="transition-2s" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>" rel="bookmark"> 
                    <span class="reclink"><?php the_title(); ?></span>
                </a>
            </li>

            <?php endwhile;
        endif;
        wp_reset_postdata();
    ?>
</ul>

Well that's it, now you can do both queries.

However, I do not always recommend uploading all posts at once, because if you have many posts this will cause performance problems. In the case of the most commented ones I recommend using posts_per_page of 10 or something like this.

    
02.04.2015 / 16:57