WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. PG73
    Member
    Posted 1 year ago #

    Hi

    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
    Member
    Posted 1 year 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
    Member
    Posted 1 year ago #

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

  4. Andrew Bartel
    Member
    Posted 1 year ago #

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

  5. PG73
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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?

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic