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.