开发者

Why isn't get_post_meta working?

开发者 https://www.devze.com 2023-01-25 10:26 出处:网络
Simple Wordpress problem - get_post_meta is not retrieving custom field values. Here\'s the code that is pulling from the custom fields:

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?


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.


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>


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

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消