[resolved] Display Custom Field 'Name' Only (7 posts)

  1. PG73
    Posted 3 years ago #


    I've been trying to find an answer to this for hours without success, so I really hope someone can help.

    My index page shows several recent blog posts (title, excerpt), some of these posts also have an image (which is added via a custom field). I want to be able to style the posts with images differently to those that are just text, using CSS. So I was looking to add a class name to posts that have an image.

    All I need is a way to show the 'Name' of a custom field, not the 'Value'.

    Everything I've tried, gives me the value, or the name and value of all my custom fields. I just need the one name of a specific custom field.

    I hope that all makes sense.

    Thanks for any help.

  2. Andrew Bartel
    Posted 3 years ago #

    Why not run a check to see whether or not the image has been added and the append a css class (or not) in the template?

  3. PG73
    Posted 3 years ago #

    That sounds good, but how would I go about doing that Andrew?

  4. Andrew Bartel
    Posted 3 years ago #

    What is the code to display the image in the custom field?

  5. PG73
    Posted 3 years ago #

    <?php if(get_post_custom_values('preview-image')) : foreach(get_post_custom_values('preview-image') as $image) : ?>
          <img class="post-preview-image" src="<?php echo home_url( '/' ); ?>wp-content/uploads/preview-images/<?php echo $image; ?>" />
          <?php endforeach; endif; ?>
  6. PG73
    Posted 3 years ago #

    Probably not the best way to do it, but I've got it working at least.

    <?php $values = get_post_custom_values("preview-image");
          if (isset($values[0])) {
          <article <?php post_class("preview-image module-teaser") ?> id="post-<?php the_ID(); ?>" >
          <?php }
          else { ?>
          <article <?php post_class(" module-teaser") ?> id="post-<?php the_ID(); ?>" >
          <?php } ?>
  7. manthono
    Posted 2 years ago #

    Hey there, may be this could help?

    I tried, it worked. But the problem is, I want to show the "Label" of the custom field, not the un-friendly key name like the trick does. Have you come across this need that may be you could help me?


Topic Closed

This topic has been closed to new replies.

About this Topic