4

I'm in a complicated situation. I'll try to explain it as easy as possible.

Imagine the following pages:

  • Page 1
  • Page 2
    • Subpage 1
    • Subpage 2
    • Subpage 3

Each subpage of page 2 lists posts from a custom post type with a specific taxonomy. Let's call the post type Objects, and the taxonomy just Categories, to keep it simple.

To list the different posts from the different Categories, i have three different page templates for the subpages: subpage-1-templ.php, subpage-2-templ.php and subpage-3-templ.php. Each one of these templates lists the posts from a specific Category for the post type (Objects). It's hardcoded.

So far all good, it works as expected. It's a little ugly to have to hardcode it but i don't know of any other way, so this'll have to work for now. (I'd prefer to ditch wordpress alltogether but that's another story)

All of those subpages lists the siblings in a sub menu. So, if you're on subpage 2 it will show subpage1, 2 and 3 (but subpage 2 has a current class on it). Works good.

Now to the actual problem; when you click one of the posts listed in a subpage, you get to a page which displays more info for the post, as expected. The problem is i want to show the submenu for page 2 in there (so, subpage1, 2 and 3). I don't know how to do that without hardcoding it.

Perhaps, and i don't know if this will work, but maybe i can find a page by it's template. I can create a template for page 2 (not for it's children, the actual parent page) called objects-template.php and then on a post, i can grab the page that uses the template, then get all it's children, and then just simply list them up. I just don't know how to get a page by it's template, if that's even possible (Preferably by the template file name, but the template name works too).

Any suggestions?

Edit

    // Query looks fine right?
    $query = new WP_Query(array(
        'meta_key' => '_wp_page_template',
        'meta_value' => 'objects-parent-page-template.php'
    ));
    // Dump the id (also tried just getting the object with get_queried_object(), still get null)
    echo '<pre>'; var_dump($query->get_queried_object_id()); die();

1 Answer 1

5

The page template's filename is stored as a post meta with key '_wp_page_template', so basically you can use get_post_meta($post_id, '_wp_page_template', true); to get the template filename for the page with ID $post_id.

You can also do the reverse (i.e. getting id from page template filename) using Custom Field Parameters in WP_Query or other wordpress functions.

Also there is a conditional function is_page_template which checks if the current page's template is the one specified in parameter

These will probably solve your problem & maybe help to get rid of the hardcoded stuff.

8
  • Awesome, i'll take a look at it!
    – qwerty
    Oct 29, 2012 at 12:09
  • I tried using WP_Query to get the id of the page with the template, but it returns null for some reason. If i do a custom query against the database selecting the key and value it is found, but WP_Query returns null. Please see edited OP for code snippet. Any ideas?
    – qwerty
    Oct 29, 2012 at 12:50
  • did you set 'post_type' to 'page'? The default value is only 'post' Oct 29, 2012 at 13:15
  • I'm guessing you only add another item in the array? 'post_type' => 'page' <- that didn't work. No error messages, just still null.
    – qwerty
    Oct 29, 2012 at 13:28
  • this has to be something with parameters. Is the page published? Does the filename matches that in the database? Is the filename showing when you use get_post_meta with that page's id? Oct 29, 2012 at 13:46

Your Answer

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

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