I am trying to create single page theme.
What i have trouble with is understanding this whole wordpress query logic.
There is query object and supposedly i get all published posts like this:
$query = new WP_Query( array (
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => array( 'page' ),
'post_status' => array( 'publish' ) ) );
and can loop over them like this :
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . the_title() . '</li>';
}
But then what? I cant even figure out that codex – where are all the functions of post object that i can use? Like the_title()? Where is that in codex?
What i want to achieve seemed pretty simple and straightforward – load all pages with nested subpages and then just print them out on single page similar to the loop above… But this codex is driving me crazy 😛
But is this even best approach? Should i be defining my query somewhere else (as opposed to index.php like im doing now)? Should i be somehow using wordpress original page loading which displays pages using their templates (if they have one) or using something similar to content-page.php like the one that default themes have?
Alan
3 Answers
WordPress queries are indeed represented by WP_Query
objects. The snippet example you have is secondary query, as opposed to main query – which is run by WP itself during core load and stored in global $wp_query
variable.
Typically it is better (for performance and compatibility) to modify main query for set of posts that is main to page – see pre_get_posts
hook documentation.
When you are running query loop, what happens is that WP fills number of global variables (main being $post
) with data for current post. Functions that access those variables and output information (such as the_title()
) are called template Tags
and Codex has list of them that should get you started.