Why isn't get_post_meta working?

瘦欲@ 提交于 2019-11-27 08:28:44

问题


Simple Wordpress problem - get_post_meta is not retrieving custom field values. Here's the code that is pulling from the custom fields:

<img src="<?php echo FCG_PLUGIN_URL; ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, 'slider_image', true); ?>&h=250&w=400&zc=1" alt="<?php echo $post_title; ?>" />

In production, this is the HTML I get:

<img alt="Post Title" src="http://***.com/wp-content/plugins/jquery-slider-for-featured-content/scripts/timthumb.php?src=/&amp;h=50&amp;w=80&amp;zc=1">

You can see the src= point in the string is empty - as if there is nothing posting from it. I have isolated and echo'd just the get_post_meta and it's a whitespace. I am 100% sure it's named correctly within the post - is there something glaring I'm missing here?


回答1:


Search for the term "slider_image" in the wp_posts and wp_postmeta tables using phpmyadmin. Then view the row that has it to see if there's anything inside.

Also try changing the name of the custom value as a test and see if that works. I use this exact code to do something similar to you and it works:

<p><a href="<?php echo get_post_meta($post->ID, 'resume', true) ?>"><img src="<? bloginfo('template_url'); ?>/img/downloadresume.png"></a></p>



回答2:


If you are calling get_post_meta inside the loop then you should call get_post_meta(get_the_id(), 'YOURKEY', true) instead of get_post_meta($post->ID, 'YOURKEY', true)

Strange things happens when you call get_post_meta inside a loop. In some themes developers hack the $post at the beginning and get_post_meta stops working so this is one of the solution for those particular cases too.




回答3:


Its because of auto save. use these lines for preventing auto save and user privileges.

// Bail if we're doing an auto save  
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 

 // if our current user can't edit this post, bail  
if( !current_user_can( 'edit_post' ) ) return;



回答4:


You could also use get_post_meta( $loop->post->ID, 'yourkey', true ); if you are using $loop = new WP_Query( $args ); or something similar.




回答5:


Actually, it gave you '/', which is not nothing. I'd say that's what's saved as 'slider_image'. Check the post (or the database directly).




回答6:


I've written some simple templating functions that enable you to use the meta data (custom data) in your theme. You can write a template function for any meta data key/value pair, and render it in a theme file like so:

<?php the_meta_templates($meta_data_keys) ?>
<?php the_template_for($meta_data_key) ?>

Feel free to check out the basic functions from github and give them a try. You'll need to add them to your themes functions.php file.




回答7:


<?php get_post_meta(get_the_id(), 'YOURKEY', true) instead of get_post_meta($post->ID, 'YOURKEY', true) ?>

Works for me!




回答8:


could it be linked to the bug

#18210 (Update_post_meta is case insensitive on meta_key, but get_post_meta is NOT) – WordPress Trac

https://core.trac.wordpress.org/ticket/18210

It would explained the different experiences, depending on db_collation... (forgive me if it total nonsense, I am a newbie .. )

WordPress Database Charset and Collation Configuration | hakre on wordpress http://hakre.wordpress.com/2010/12/26/wordpress-database-charset-and-collation-configuration/




回答9:


<?php
// Get custum fields and values 
$mykey_values = get_post_custom_values('my_key');
foreach ( $mykey_values as $key => $value ) {
echo "$key  => $value ('my_key')<br />"; 
}

?>


来源:https://stackoverflow.com/questions/4189961/why-isnt-get-post-meta-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!