How to list all posts in a custom page? WordPress

0

Well, I've created a custom page where I need it to return everyone the posts you have registered on the site, just like home. I tried to do it in the normal way, creating a custom page and assigning elá the same code that is in index.php , but when I try to list all the posts it only 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(); ?>

Do you know when we create a custom page? Okay, just that I do not want this page to be a post, I want it to return all the posts I already have, but it only returned me what it weighs to be hers. Did you understand ??

    
asked by anonymous 16.12.2015 / 15:12

1 answer

1

I understood. You want a loop page in your template to show all the posts you have registered, not page text.

This is happening because you are using the default loop , which accesses the global WP_Query . Since your environment is a page, WP understands that you must, in the loop , make the display of that post (i.e., the page you are on). To work around the problem, you can create a new instance of the query, specifying your need, and not leaving it to WP . Retrieved 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 $args parameter as follows

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

Or, mount the query as

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

Codex has all the parameters that the WP_Query constructor can receive. Basically, they filter the result of the query. As it is, it will retrieve all posts from the post type.

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

    
17.12.2015 / 17:53