WordPress: How to Determine if a Certain Page Exists

StartThe Convo

January 25, 2009

I am developing a new premium theme called “Tidings” which will be a news theme unlike most I’ve seen available. One module on the front page will contain recent comments or “discussions”. I want to give the user the option of having an entire page for discussions (i.e. listing the most recent comments), and then provide a link to this page from the front page discussions module. But if they don’t choose to have that page, no link is necessary. So I set out to figure out how to test if a certain page named “discussions” exists. Combining a little knowledge I’ve gained from my experience in tweaking WordPress, and just my all around ingeniousness (one of those options is exaggerated, you can be the judge), I was able to come up with the following code. So use it, and enjoy it, and post back any questions you might have, or if you know a better way, then by all means, do share!

<?php $pages = get_pages();
foreach ($pages as $page) {
$apage = $page->post_name;
if ($apage == 'discussions') { ?>
<a class="more" href="<?php bloginfo('home'); ?>/discussions">More</a>
<?php } } ?>

NO COMMENTS

  1. Very nice start up posts, do u plan to extend your posting & or extend what you have already started.

    Maybe you could explain how to do things on wordpress as well as setting up extra mods & changing how the WP SCRIPT WORKS

  2. Writing isn’t one of my stronger areas, so most of the time I let it slip lower in my list of priorities. What specifically would you like to read about, perhaps it would encourage me enough to let loose some of the knowledge I’ve gained from my experience.

  3. How about going throught parts of wordpress and writting and explaining what each section of code dose.

    For example how another screen cast shows you about the comment file & how to display and format comments on your blog.

    Maybe you could teach us how to make custom features to go with wordpress as well as already existing wordpress functions

  4. I have thought about writing about those kinds of things, I have a few articles on here like that, but it’s tough for me to give away my secrets. :) I’ll schedule it in though, for sure. Subscribe to rss or email so you’ll know when I do! I’m about to make a few design changes around here, because my new logo is almost ready

  5. How about using get_page_by_title instead?

  6. what does that render if there is no page by that title?

 

Reply

Google Analytics Alternative