Visualizzare tutti i post da categorie specifiche in una pagina

2 gen 2013, 13:43:28
Visualizzazioni: 28K
Voti: 5

Voglio visualizzare tutti i post da categorie specifiche in una singola pagina. Per questo ho modificato il file page.php nella cartella del mio tema. Ho aggiunto una 'clausola if' per controllare quale pagina viene attualmente visualizzata e caricare tutti i post dalle seguenti categorie.

<?php get_header(); ?>

<div id="primary">
    <div id="content" role="main">

<?php
    if (is_page(26)) {
        // Interroga i post dalle categorie 2,6,9,13, mostra tutti i post (-1) e ordina per data
        query_posts('cat=2,6,9,13&showposts=-1&orderby=date');    
        if (have_posts()) : 
            while (have_posts()) : 
                the_post(); 
                get_template_part( 'content', 'page' );
            endwhile; 
        endif;  
    } else {
        while ( have_posts() ) : 
            the_post(); 
            get_template_part( 'content', 'page' ); 
        endwhile; // fine del loop 
    }
?>

    </div><!-- #content -->
</div><!-- #primary -->

<?php get_footer(); ?>

Ma quando carico la mia pagina 26 non viene visualizzato nulla.

1
Commenti

Non utilizzare query_posts http://wordpress.stackexchange.com/a/50762/10911. Usa invece WP_Query per questo scopo http://codex.wordpress.org/Class_Reference/WP_Query

janw janw
2 gen 2013 13:47:58
Tutte le risposte alla domanda 2
0

Consiglierei di aggiungere l'argomento della categoria in un array. E non usare query_posts. Inoltre showposts è deprecato, usa invece posts_per_page.

$args = array (
    'cat' => array(2,6,9,13),
    'posts_per_page' => -1, //showposts è deprecato
    'orderby' => 'date' //Puoi specificare più filtri per ottenere i dati
);

$cat_posts = new WP_query($args);

if ($cat_posts->have_posts()) : while ($cat_posts->have_posts()) : $cat_posts->the_post();
        get_template_part( 'content', 'page' );
endwhile; endif;
2 gen 2013 13:52:16
0

Questo accade perché stai ancora utilizzando query_posts(). Smettila di farlo. Usa invece WP_Query:

$extra_posts = new WP_Query( 'cat=2,6,9,13&showposts=-1&orderby=date' );
if ( $extra_posts->have_posts() )
{
    while( $extra_posts->have_posts() )
    {
        $extra_posts->the_post();
        get_template_part( 'content', 'page' );
    }
    wp_reset_postdata();
}
2 gen 2013 13:49:26