admin管理员组

文章数量:1391934

I have a custom post type called 'software', contained within are various custom fields such as subtitle, price, screenshots, download link, etc. I created a function to allow use of the tinyMCE edit window for some of these custom fields. I have been trying to display these fields on the page but with no success.

The method I'm using is this:

<h1><?php the_title();?></h1>
<h3><?php echo get_post_meta(get_the_ID(), 'subtitle', TRUE); ?></h3>

Here is a link to the page.

Below the <hr/> on the page is a list of all the meta created. The ONLY one of the fields which will display is 'price' for some strange reason.

Anyone have any idea what I'm missing?

I have a custom post type called 'software', contained within are various custom fields such as subtitle, price, screenshots, download link, etc. I created a function to allow use of the tinyMCE edit window for some of these custom fields. I have been trying to display these fields on the page but with no success.

The method I'm using is this:

<h1><?php the_title();?></h1>
<h3><?php echo get_post_meta(get_the_ID(), 'subtitle', TRUE); ?></h3>

Here is a link to the page.

Below the <hr/> on the page is a list of all the meta created. The ONLY one of the fields which will display is 'price' for some strange reason.

Anyone have any idea what I'm missing?

Share Improve this question edited Sep 13, 2013 at 17:22 Mayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges asked Sep 13, 2013 at 16:14 dvmac01dvmac01 3831 gold badge3 silver badges5 bronze badges 2
  • it is indeed the get_post_meta() function, and if you are calling it inside the loop, it should work... Unless you're not using the right custom field name. They often come with a prefix if they are implemented via a plugin like meta-box. Can you post the code how you declare your custom fields? A solution would be to open the wp_postmeta table in PhpMyAdmin and search the column meta_key for LIKE %...% and specify "subtitle" as meta_key value. You will see exactly under what name Wordpress is storing your custom field. – pixeline Commented Sep 13, 2013 at 16:37
  • I know this is old, but I use this sql to get a list of all meta fields in phpmyadmin: SELECT m.meta_key FROM wp_postmeta m GROUP BY m.meta_key – ssaltman Commented Nov 10, 2015 at 15:31
Add a comment  | 

2 Answers 2

Reset to default 30

Well, you are using:

get_post_meta(get_the_ID(), 'subtitle', TRUE);

So, you are saying to Wordpress to get the meta value of the 'subtitle' field and that the returned value be in format of string. See get_post_meta() docu.

To get all meta data of a post you should use get_post_custom() function instead. For example, if you are inside the loop:

$custom = get_post_custom();
foreach($custom as $key => $value) {
     echo $key.': '.$value.'<br />';
}

This will return all meta data of the post. If you want to check, for example, the "price" meta field:

if(isset($custom['price'])) {
    echo 'Price: '.$custom['price'][0];
}

use this code for solving your problem.

$key_name = get_post_custom_values($key = 'Key Name');
echo $key_name[0];

本文标签: How to display value of custom fields in page