1

Something so simple should be much easier to do.

On my listing pages I have a sidebar that pulls certain custom post fields and displays the title and the information. It may be seen here. (Everything from "Price" down is a custom field.)

In the PHP file that displays this data, the code is:

if (!$preview){ 
    echo get_post_custom_listing_single_page($post->ID,'<p><span class="post_cus_field {#HTMLVAR#}">{#TITLE#} : </span>{#VALUE#}</p>');
} elseif ($preview && $_REQUEST['alook']){
    echo get_post_custom_listing_single_page(mysql_real_escape_string($_REQUEST['pid']),'<p><span class="{#HTMLVAR#}">{#TITLE#}</span> : {#VALUE#}</p>');
} else {
    echo get_post_custom_listing_single_page_preview($post->ID,'<p><span class="post_cus_field {#HTMLVAR#}">{#TITLE#} : </span>{#VALUE#}</p>');
}

I simply want to re-create this at the bottom part of the post (under the main information). I think I need to re-create the loop and then call the above information, but I'm having trouble figuring this out.

Any Wordpress gurus out there that are able to help?

(Also, ultimately, I want to be able to call each individual custom post field ("price", "Accomodation", etc..., and then display that in a either tabs or an accordion script.)

Thanks!

2 Answers 2

4

You can echo all post's custom fields by this loop inserted in you single.php or another correspondin theme file:

 $custom_fields = get_post_custom( get_the_ID() );
  $my_custom_field = $custom_fields['my_custom_field'];
  foreach ( $my_custom_field as $key => $value )
    echo $key . " => " . $value . "<br />";

Than, when you get to know key's of your custom post fields, you can call them one by one via

echo get_post_meta( get_the_ID(), 'custom-field-key', true);

The first loop is just for you to get to know what proper keys are, so you do not need it anymore after that.

Sign up to request clarification or add additional context in comments.

6 Comments

Hey David, thanks so much for the help. Sorry in advance that I'm such a newbie at the type of coding! Would you happen to know how I stick the above in exactly? I tried researching creating a loop, etc..., but have had no such luck. The portion of the .php file that I want the above to be displayed is where I marked: INSERT CODE HERE in the following text file: .txt file
Paste following code to get to know exact post_meta_keys of desired values: <?php $custom_fields = get_post_custom( get_the_ID() ); $my_custom_field = $custom_fields['my_custom_field']; foreach ( $my_custom_field as $key => $value ) echo $key . " => " . $value . "<br />"; ?> When you know what keys you want to retrieve, insert, instead of this code, this one: <?php echo get_post_meta( get_the_ID(), 'key', true ); ?> If you are still lost, reveal me the output from the first piece of code.
Thanks @david.binda, that worked! I implemented it in the following page with an accordion dropdown: here I wonder if I'm able to hide the entire custom post field if there is nothing to return? For example, "Included Amenities" has no value, so when you click it nothing shows. I wonder if it could just be hidden instead?
Yes, you can hide that. <?php if( get_post_meta(get_the_ID(), 'Included Amenities', true) && trim(get_post_meta(get_the_ID(), 'Included Amenities', true)) != '' ) { echo get_post_meta(get_the_ID(), 'Included Amenities', true); } ?>
Hmmmm, didn't seem to work. I was hoping that if there's no data for "Included Amenities", the the words "Included Amenities" and that entire result would just not appear. Here's what I inserted: <li id="li3">Included Amenities <ul> <li> <?php if( get_post_meta(get_the_ID(), 'Included Amenities', true) && trim(get_post_meta(get_the_ID(), 'Included Amenities', true)) != '' ) { echo get_post_meta(get_the_ID(), 'Included Amenities', true); } ?> </li> </ul> </li>
|
1
$custom_fields = get_post_custom( get_the_ID() );

if  ($my_custom_field = $custom_fields['custom_field Name'])
{

  foreach ( $my_custom_field as  $value )
{
   echo 'Price: '.$value ;
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.