5

I'm coding a theme where I want to display information in the footer that's only available on a template page. I have been using get_page_by_title() which works well if you know what the page will be called, but in my opinion it's not very dynamic, it's impossible to predict what a user will name a page in your theme.

In my research I found a function called get_all_page_ids() which returns a numerically indexed array of ids from the site. This function would be a lot cooler if it was an associative (or even a post object), but I digress.

I have considered using something like this:

$all_page_ids = get_all_page_ids();

foreach ($all_page_ids as $id) {
    // get_post() {@link https://developer.wordpress.org/reference/functions/get_post/}
    $post_object = get_post($id);

    // preg_match() {@link http://php.net/manual/en/function.preg-match.php}
    if(preg_match("/\b(contact)\b/i", $post_object->post_title)) {
        // do something here
    } else {
        // for readability
        continue;
    }
}

However, this still creates the problem of having to know that the user chose to have the word contact in their title. What if they created a page called "Get in Touch" that is their contact page? This would break.

So, in short, my question revolves around best practice for theme designers. Is get_page_by_title() the best way to get another page's ID, or is there a way to combine a few functions/variables to get another page's ID without knowing the title?

Or are there WordPress functions that do this kind of heavy lifting or is get_page_by_title() the only close one? My suspicion is there's a hackier, but better way to do this, I just don't have any ideas. Thanks for any help you're able to give on this.

6

If you don't force a title, and obviously do not know the id, the only way you can do it is by letting the user select which page to use, usually done in the theme's options page, but can be done in the costumizer, or even as part of page editing

3
  • Hm, I was trying to avoid something like that, and do it more programmatically. What do you think about a metabox (for all 'post_type' => 'page') with a boolean checkbox that designates whether it's a contact page or not? That way it's not some setting hidden away that the user has to find. – mrClean Jan 6 '16 at 15:53
  • 2
    You can do it that way, but in the end the user still need to search for it :(. You can output an error/info type of message at the admin so the user will at least know he have to configure it. The best advantage of option page/custumizer is that a user can explicitly indicate that he doesn't want a contact page in the footer. – Mark Kaplun Jan 6 '16 at 16:33
  • Check for the option then fallback to page search if not found. – jgraup Jan 6 '16 at 17:24
3

If your page is a template page then you can use is_page_template($template_name)

Otherwise as Mark mentioned you have to create a theme option and let the user decide.

More: https://codex.wordpress.org/Function_Reference/is_page_template

UPDATE

I misread your question. You can get all pages using a certain template name but again a user could made more than one such pages with the same template so you are not sure which one to choose.

 $args = array(
    'meta_key'         => '_wp_page_template',
    'meta_value'       => 'page-about.php',
    'post_type'        => 'page'
 );
 $posts_array = get_posts( $args );

Another idea is to create yourself your page with code at the activation of the theme/plugin and then save the id as an option but also provide a theme option to the user to change the page if he/she wants. That's how WooCommerce works with the Shop Page.

Again I believe Mark's approach is better and safer.

3
  • is_page_template() will conditionally let me know if I am viewing that template file. Which means I would have to be on that page. What I am looking for is a way to grab the data associated with the page template id of XX and display that meta data in the footer (which is on every page). Is there a function like is_page_template() where I can pass in the name of the template file to get the id? – mrClean Jan 6 '16 at 18:16
  • 1
    Sorry I misread your question. I updated my answer. – Laxmana Jan 6 '16 at 21:19
  • Thanks for the update. Yeah, I think I agree with you about Mark's approach. I was just waiting to accept the answer to see if there were more viable alternatives. Thank you for your approach, I didn't know that about WooCommerce. – mrClean Jan 6 '16 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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