Support » Fixing WordPress » if a specific key exists, change the output in get_pages

  • OK, I am using this code:

    $pages = ( get_pages("child_of=18&sort_column=post_title&title_li="));
    foreach ($pages as $page) {
    echo '<li><a href="'.$page->post_name.'">'.$page->post_title.'</a></li>';
    }

    which works like wp_list_pages without adding all the page_item and other classes. All good.

    but I’d like to say:

    If a specific custom field key exists (let’s call it “linkme”) do the above logic ELSE just show the title without the link

    I think I can write up the output without the link, but I don’t know how to test for the specific key…

    $pages = ( get_pages("child_of=18&sort_column=post_title&title_li="));
    foreach ($pages as $page) {
    echo '<li>'.$page->post_title.'</li>';
    }

    make sense? My question is A) how to test for the specific “linkme” key and if it exists echo the first thing, if not, echo the second.

    thanks in advance for any help, ideas, direction, etc!

Viewing 4 replies - 1 through 4 (of 4 total)
  • thanks… but the problem is that I don’t want the value (so that whole thing introduces a lot of other stuff)

    what I am looking for is a way to say — in the context of my code above, where I am going to output a list of pages — if said key (‘linkme’) exists (for $page), echo this else (if it doesn’t) echo that.

    my above code will write out the pages using the formatting around the link and title. I’ve tested it.

    What I don’t know is A) how to check if a specific key exists (for each of the pages) and B) where, in my working code, to insert it.

    I get that I somehow need to test for the existence of the key (linkme) but don’t know how to “remember” that for each specific page while it completes the foreach statement. Or, perhaps the test for the key happens after that statement… I don’t know.

    I totally appreciate your guidance to the other post, but it just introduces so many other things AND it is focusing on outputting the VALUE rather than the state of existence?

    nevermind. I found another way to do this by using the Excerpt plugin to create a linked page title or not linked page title to a page…

    not ideal, but easy to manage and works.

    Now, if I can just get all that hair and blood off of my computer keyboard….

    As a follow-up, I am using the Excerpt Editor plugin (creates excerpts for pages with a nice admin page) and this code to pull “client pages” (where I can show project slideshows if desired or just some text about that client):

    <?php
    
    $pages = (get_pages("child_of=18&sort_column=post_title&title_li="));
    foreach ($pages as $page) {
    echo '<li>'.$page->post_excerpt.'</li>';
    }
    ?>

    The excerpt then can either show the client’s name linked to a project page or just the client’s name (where there is project page, but no projects to show).

    By generating the list of pages FROM actual child page, I can use the default published or draft status of said project page to control whether a client lists or not as I don’t always want all clients to show. I don’t have to delete anything or hardcode the ids of said pages to get them to list.

    As a bonus, I am showing these in 4 columns, so I’ve set up 4 parent pages for all clients (each is a range alphabetically)

    It’s working.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘if a specific key exists, change the output in get_pages’ is closed to new replies.