Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Wordpress LOOP with pages instead of posts?

Tags:

php

wordpress

Is there a way to use THE LOOP in Wordpress to load pages instead of posts?

I would like to be able to query a set of child pages, and then use THE LOOP function calls on it - things like the_permalink() and the_title().

Is there a way to do this? I didn't see anything in query_posts() documentation.

like image 884
ashchristopher Avatar asked Oct 13 '08 02:10

ashchristopher


People also ask

What is WordPress page loop?

What Is WordPress Loop? A PHP code that displays WordPress posts is called a loop or WordPress loop. WordPress themes use a loop to display the posts on the current web pages. Loop is based on some functions designed to display the posts by running these functions.

What is a loop page?

The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.

How do I use a while loop in WordPress?

php // Start the main loop if ( have_posts() ) : while ( have_posts() ) : the_post(); the_title(); endwhile; endif; // Use rewind_posts() to use the query a second time. rewind_posts(); // Start a new loop while ( have_posts() ) : the_post(); the_content(); endwhile; ?>

What is WP_Query WordPress?

What is WP_Query? WP_Query is a PHP class for constructing queries to the WordPress database and returning posts, pages, or other custom objects to render on the page. It allows developers to build complex searches while removing the need to write separate SQL queries.


1 Answers

Yes, that's possible. You can create a new WP_Query object. Do something like this:

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: There are a lot of other parameters that can be used with query_posts. Some, but unfortunately not all, are listed here: http://codex.wordpress.org/Template_Tags/query_posts. At least post_parent and more important post_type are not listed there. I dug through the sources of ./wp-include/query.php to find out about these.

like image 56
Simon Lehmann Avatar answered Oct 08 '22 00:10

Simon Lehmann