Usando Wordpress LOOP con páginas en lugar de publicaciones?


¿Hay una manera de usar EL BUCLE en Wordpress para cargar páginas en lugar de publicaciones?

Me gustaría poder consultar un conjunto de páginas secundarias, y luego usar LA función LOOP la llama - cosas como the_permalink() y the_title().

¿Hay alguna manera de hacer esto? No vi nada en la documentación query_posts().

Author: A J, 2008-10-13

2 answers

Sí, eso es posible. Puede crear un nuevo objeto WP_Query. Haga algo como esto:

query_posts(array('showposts' => <number_of_pages_to_show>, 'post_parent' => <ID of the parent page>, 'post_type' => 'page'));

while (have_posts()) { the_post();
    /* Do whatever you want to do for every page... */
}

wp_reset_query();  // Restore global post data

Addition : Hay muchos otros parámetros que se pueden usar con query_posts. Algunos, pero desafortunadamente no todos, se enumeran aquí: http://codex.wordpress.org/Template_Tags/query_posts . Al menos post_parent y más importante post_type no están en la lista. Cavé a través de las fuentes de ./wp-include/query.php para averiguar acerca de estos.

 55
Author: Simon Lehmann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-02 08:21:31

Dada la edad de esta pregunta, quería proporcionar una respuesta actualizada para cualquiera que se tropiece con ella.

Yo sugeriría evitar query_posts. Aquí está la alternativa que prefiero:

$child_pages = new WP_Query( array(
    'post_type'      => 'page', // set the post type to page
    'posts_per_page' => 10, // number of posts (pages) to show
    'post_parent'    => <ID of the parent page>, // enter the post ID of the parent page
    'no_found_rows'  => true, // no pagination necessary so improve efficiency of loop
) );

if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
    // Do whatever you want to do for every page. the_title(), the_permalink(), etc...
endwhile; endif;  

wp_reset_postdata();

Otra alternativa sería usar el filtro pre_get_posts, sin embargo, esto solo se aplica en este caso si necesita modificar el bucle primario. El ejemplo anterior es mejor cuando se usa como un bucle secundario.

Más información: http://codex.wordpress.org/Class_Reference/WP_Query

 18
Author: Nathan Dawson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-13 08:19:11