Tammy Hart Designs

WordPress: How to Determine if a Certain Page Exists

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 }
} ?>

10 Responses to “WordPress: How to Determine if a Certain Page Exists”

  1. Rocky  

    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. Rocky  

    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

  3. tammyhart  

    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

  4. Manne  

    How about using get_page_by_title instead?

  5. tammyhart  

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

  6. Downloadtaky  

    Hi
    1 thing: I LOVE YOUR TEMPLATE! I woul like to be able to create something like your!
    2 thing: (I’m creating my first template :P ) and I would like to insert a function like vBullettin Forums, I would like to be able to know if there is a Duplicate Title for the post I’m writing. I don’t know if you know it any way vBullettin forums have this function.
    I’m not a completely noob but a Starting-Self-Made-Programmer so I need help!
    I think that using % inside the function will help to find also Misspelled title, what do you think about it? Thanks a lot for you time!
    (Excuse me 4 my bad english! I’ve studied it at school a few years ago…)

  7. Downloadtaky  

    Maybe % isn’t the good solution but I hope you’ll understand what I mean :-)

  8. Tammy Hart  

    I’m not sure of any automated way to do this, but you might try Googling “WordPress Prevent Duplicate Content”. I came across a few good tips when I did, not sure which ones might best suit your needs.

  9. Downloadtaky  

    I’ve found this: // check if title $my_post_name already exists in wpdb
    global $wpdb;
    if($wpdb->get_row(“SELECT post_title FROM wp_posts WHERE post_title = ‘” . $my_post_name . “‘”, ‘ARRAY_A’)) {
    echo $my_post_name.’ already exists’; }
    Do you know how to run this function when me (or an author) click on Post?

  10. tammyhart  

    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.

Leave a Reply