• I have a WP site that uses custom fields for many different things, including product descriptions on the single.php page. Normally with my other sites I would just include: <?php the_meta(); ?> and list them all – but I cannot do this with this site as there are many other fields that are completely unrelated to the description.

    I would like to have just the fields for descriptions show up on this page, and not the 20 others. How can I code this to display only selected custom fields on this page?

    Thanks

Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter ultimat1

    (@ultimat1)

    Okay, thanks for that – it helped me accomplish what I was hoping for.
    So it now looks like this in “single.php”:

    Brand: <?php $key=”Brand”; echo get_post_meta($post->ID, $key, true); ?>

    But what it doesn’t say is how to get the $key name to come up as well.

    So if there is no brand name available then I still have the word “Brand:” with a blank space after.

    What I would like to do is get the name of the ($key) to come before the meta data dynamically, so that if there is no brand entered into the custom field, it would show nothing at all for that string.

    Is there a way to do this?

    I’m not sure it’s the best way to do it but here is what i use
    <?php $key = get_post_meta($post->ID, 'YOURCUSTOMFIELD', true) ; if (!empty($key)) { ?>Brand:<?php echo ($key)} ?>

    Thread Starter ultimat1

    (@ultimat1)

    I copied the code you provided and changed the ‘YOURCUSTOMFIELD’ part to the name of my custom field and returned an error.

    Parse error: syntax error, unexpected ‘}’, expecting ‘,’ or ‘;’ in XXXXX/XXXXXX/XXXXX/single.php on line 434

    Thanks for trying to help, though. Maybe I did something wrong?

    add a semicolon after echo ($key)};

    Thread Starter ultimat1

    (@ultimat1)

    Didn’t work. πŸ™

    Thanks anyway.

    hmmm I’m really bad at troubleshooting php… I’ll post the code I use without any modifications. Maybe it will be more helpfull
    <?php $email = get_post_meta($post->ID, 'ccio_email', true) ; if (!empty($email)) { ?><li>Courriel: <?php echo get_post_meta($post->ID, 'ccio_email', true) ; ?></li><?php } ?>

    Thread Starter ultimat1

    (@ultimat1)

    Unfortunately so am I, lol.

    Thread Starter ultimat1

    (@ultimat1)

    Is there a way to get the name of the ($key) to come before the meta data dynamically, so that if there is no brand entered into the custom field, it would show nothing at all for that string?

    Try using the get_post_custom() function (Codex ref), which returns an associative ( ‘key’ => ‘value’ ) array of all post metadata. e.g.

    $mypostmeta = get_post_custom();

    Then, you can do whatever you need to, with your specific meta keys.

    Thread Starter ultimat1

    (@ultimat1)

    That would be perfect but instead of showing the name of the key (i.e. – “Brand”) it shows a numeric value from 0-whatever.

    So if the custom field metadata is “Budweiser” it shows “0 => Budweiser”.

    I need it to show “Brand => Budweiser”. And so on with any other keys that are specified. Almost there, though. Any idea how to change the number to the actual key name?

    Thanks everyone for your help, I do appreciate it!

    So if the custom field metadata is “Budweiser” it shows “0 => Budweiser”.

    I need it to show “Brand => Budweiser”.

    $postmeta = get_post_custom();
    $mymetakey = $postmeta['my-meta-key'];
    $mymetavalue = $mymetakey[0];
    Thread Starter ultimat1

    (@ultimat1)

    Well I figured this out, so I decided to share just in case anyone else has this issue.

    Instead of trying to select which keys I wanted to show, I decided to try to focus on the ones I DIDN’T want to show, and specified that like so (just replace “yourkey” with the name of the key that you do not want to show to visitors, all other keys:values will show.) :

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I don’t know that this is the best way to do it, but it worked for me. Thanks to all who were kind enough to offer help!

    Moderator keesiemeijer

    (@keesiemeijer)

    How to post code in WordPress forum topics: http://codex.wordpress.org/Forum_Welcome#Posting_Code

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Custom Fields Help…’ is closed to new replies.