The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End checking if WordPress custom fields exists using php?

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • #31500

    I’ve been following Chris’ excellent WordPress tutorial lessons on
    So far I’ve learnt how to display any images I’ve entered as custom fields like this:

    However I need to create a flexible template as I won’t always have the same number of images per page.

    I’ve tried doing it with a php counter but I’m still learning php so I wasn’t really surprised that it didn’t work.
    Can anybody tell me why this isn’t working or if I’m even going about it the right way?

    $i = 1;
    while($i < 5)

    if (file_exists(get_post_meta($post->ID, 'pic_"$i"', true)))
    echo "";


    You’ll want to do something like this:

    ID, 'pic', false); ?>

    This post inspired by:

      echo '';
      } ?>

    Then you can use the same custom field name for each one.


    Cheers Doc, that works even better than what I wanted! :)
    Out of interest is there any way to generate alt tags in a similar way or would I have to use a counter to define each one as unique to the relevant image it’s associated with?


    That would be quite complicated, I’d imagine. Are you just trying to create a gallery? If that’s the case, using a plugin or WP’s built-in function would work a lot better.


    I’m creating portfolio pages, I have an old version ( which I built a while ago, but it needs updating (for both design and coding).

    The portfolio page is split into three sections:
    • Web and Email Design,
    • Print Design
    • Branding and Identity

    If you click on a project it goes through to it’s own page (which is what my original question about displaying custom images is used on). As I know there will never be more than 6 images I’ve set a php counter to work with assigning alt tags so I’ve almost got everything sorted.

    The only problem I’m still having is displaying the thumbnails of featured projects on my homepage (current version is hard coded) which click through to the relevant project. Not sure if you have suggestions (I’m using the code which Chris wrote for custom pages in his tutorial if thats of any use)?

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Back End’ is closed to new topics and replies.