Why isn't get_post_meta working?

后端 未结 9 1054
予麋鹿
予麋鹿 2020-12-12 05:12

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



        
相关标签:
9条回答
  • 2020-12-12 05:46

    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.

    0 讨论(0)
  • 2020-12-12 05:50

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

    0 讨论(0)
  • 2020-12-12 05:51

    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>
    
    0 讨论(0)
  • 2020-12-12 05:52

    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.

    0 讨论(0)
  • 2020-12-12 06:01

    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/

    0 讨论(0)
  • 2020-12-12 06:01
    <?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 讨论(0)
提交回复
热议问题