Why isn't get_post_meta working?

我是研究僧i 提交于 2019-11-28 14:22:02

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>

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.

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;

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

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).

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.

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

Works for me!

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/

<?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 />"; 
}

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