1

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 :P

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 3

3

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.

6
  • Okay. modifying original query sounds like a better thing to do.
    – Zayatzz
    Commented Jun 29, 2013 at 19:47
  • So if i modify the original query i can assume that have_posts() can pick up its results and i can start putting the template together? ... This is so far from the oop and mvc patterns im used to...Can you perhaps point me towards some decent blogpost that can explain all that? Cause all i find is some half-assed snippets that dont explain me anything :/
    – Zayatzz
    Commented Jun 29, 2013 at 19:50
  • @Zayatzz see this answer for crash course on queries wordpress.stackexchange.com/a/1755/847 in a nuthsell have_posts() function acts on main query and have_posts() method acts on specific query object it's called on.
    – Rarst
    Commented Jun 29, 2013 at 19:58
  • Man its difficult at this time of day. So what... I modify theoriginal query with my stuff, so it will return all pages for main query. Then what.. Use example 1 from here: codex.wordpress.org/Function_Reference/setup_postdata. To loop through returned posts and print them out using my own html? Or is there a better way to do it, using some pre defined templates?
    – Zayatzz
    Commented Jun 29, 2013 at 20:57
  • 1
    @Zayatzz please do not use comments for extensive discussion, they are not meant for it :) it's better to search site and/or ask new questions about specific challenges. You can also drop by site's chat room.
    – Rarst
    Commented Jun 29, 2013 at 21:04
2

Usually "single page" refers to sites that manipulate content via Javascript. I can't tell if that is what you are trying to do. If fact, it kinda sounds like it isn't, so I am assuming that it isn't for now.

WordPress populates the query for all of the major pages. That default query is commonly called the "main query" and it stored in a global variable called $wp_query, which the Loop functions assume unless told otherwise. In other words, this...

if (have_posts()) {
  while (have_posts()) {

  }
} 

... is the same as this...

if ($wp_query->have_posts()) {
  while ($wp_query->have_posts()) {

  }
} 

You should not as a rule, nor should you need to, create a new query for those basic pages. If you need to alter the query, you would do so via pre_get_posts.

The other functions you refer to-- the_title, etc-- are "Template Tags"

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?

Probably, yes. But again, I am not 100% sure what you are doing.

1

According to

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

I guess he tries to create a One-Pager-Theme like http://dev.zeotheory.com/micropage/ . It would be very helpful to know how complex your theme will get. Basically you could start with altering the mainquery on your home page with pre_get_posts and the Conditional Tag is_home to show top level pages only. This could be done by adding this to your functions.php:

function show_all_pages_on_index( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set('post_type', 'page');
        $query->set('showposts', '-1');
        $query->set('post_parent', '0');
    }
}
add_action( 'pre_get_posts', 'show_all_pages_on_index' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.