How to list all posts on a custom page? Wordpress

Asked

Viewed 2,424 times

1

Well, I created a personalized page where I need her to return all the posts you have registered on the site, as if it were the home. I tried to do it the normal way, creating a custom page and assigning the same code to index.php, however when I try to list all posts it just returns me the page as if it were a publication page...

<?php
/*
Template Name: Novos Posts
*/
get_header(); ?>
<div class="cont_marg">
    <div class="pad_sd">
        <?php if (have_posts()) : ?>
            <ul class="lista-filmes">
                <?php while ( have_posts() ) : the_post(); ?>
                    <?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE); ?>
                    <li id="post-<?php the_ID(); ?>" title="<?php the_title(); ?>">
                        <div class="titulo-box open-sans">
                            <h2 class="titulo-box-link">
                                <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                            </h2>
                        </div>
                        <div class="capa">
                            <div class="fluccs">
                                <div class="boxxer"><?php echo $my_meta['durat']; ?>min</div>
                            </div>
                            <a href="<?php the_permalink(); ?>" class="absolute-capa no-text effect"><?php the_title(); ?></a>
                            <?php the_post_thumbnail(array(158,226)); ?>
                            <div class="flutuador" style="background:none;">
                                <div class="audioy"><?php echo the_qualt($my_meta['qualt']); ?></div>
                                <div class="anolanc"><?php echo $my_meta['ano']; ?></div>
                            </div>
                        </div>
                        <div class="views"><?php echo getPostViews(get_the_ID()); ?> visitas</div>
                    </li><!-- #post-<?php the_ID(); ?> -->
                <?php endwhile; ?>
                <?php post_pagination();?>
            </ul>
        <?php endif; ?>
    </div><!-- .pad_sd -->
</div><!-- .cont_marg -->
<?php get_footer(); ?>

You know when we create a custom page? Ready just that I do not want this page to be a post, I want her to return me all the posts I already have, but she just returned me what she weighs be her own. They understood??

1 answer

2

Got it. You want that one page, through the loop in your template, show all posts that you have registered, and not the text of the page.

This is happening because you are using the loop standard, which accesses the global WP_Query. As your environment is a page, the WP believes that it should, in the loop, display that post (i.e., the page you’re on). To get around the problem, you can create a new instance of the query, specifying its need, and not leaving it up to WP. Taken from Codex:

<?php

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

To force this new query to search for posts (post_type => post), you must initialize the parameter $args as follows

$args = array(
    'post_type' => 'post'
)

Or, assemble the query as

$the_query = new WP_Query ( array( 'post_type'=>'post' ) );

Codex has all the parameters that the constructor of WP_Query can receive. Basically, they filter the result of the query. The way it is, it will rescue all the posts of the kind post.

Obviously, you must change this logic to suit your mark-up. But basically, you just invoke the methods have_posts() and related from its new object, and not from the global.

  • Hello. Excellent placement. I would like to ask for a compliment: How would I do for loop displays, just posts from a particular category? I imagine it would be something like this: &#xA; $the_query = new WP_Query ( array( 'post_type'=>'post', 'category' => 'any_category' ) ); &#xA;&#xA; That would be it?

  • 1

    @Lucianobezerra the path is this. You can see the category parameters here. Comprehensively, you could do $the_query = new WP_Query ( array( 'post_type'=>'post', 'category_name' => 'minha_categoria' ) );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.