Support » Plugins » Hacks » How to get custom field value in the code

  • I have set a custom field “link” for post and give it a value,such as “” differently for each post.

    In templates, I need to get the link value, some current code sentence is like the under:

    return '<span class=\'clear\'><a class=\'readmore\' href=\''. get_post_custom($post->ID, 'link', true). '\'>' . __( 'Read More', 'Jen') . '</a></span>';

    but only return is my webste is the value of custom field “link” for some post

    How should I fix the code sentence to get right link value return?

    Thank you.

Viewing 10 replies - 1 through 10 (of 10 total)
  • only get return

    i want to get the custom field value “”

    how to fix the code?

    Try this instead.

    Function: get_post_meta

    get_post_meta($post_id, $key, $single);

    You are using get_post_custom incorrectly. From the codex, get_post_custom

    Returns a multidimensional array with all custom fields of a particular post or page.


    Thank you.

    I replace the code with the following instead:

    get_post_meta($post_id, $link, $single)

    but get value “”

    “” is my website

    the “link” is my custom field key

    Should I lost something or change something or add something? (Sorry,I am newbie for PHP).

    How should I fix the code sentence?

    Sorry, I was unclear. The code I had was just an example.

    For your specific instance, try this

    get_post_meta($post->ID, 'link', true)

    Thank you.

    I have replaced it as yours but only get the hyperlink “” (it’s my website homepage website) unavailable to get the field value “”

    other suggestions?

    It’s hard to say without seeing more of your code and data.

    Are you sure you are getting the expected “post id” in your code?

    Are you sure the post id referenced has the custom field “link”?

    Do all posts have the a valid “link” value?


    <?php echo get_post_meta($post->ID, 'key', true); ?>

    the function is right.i can get the value in other themes.

    but i don’t know how to transfer it into the sentence i wrote in the first post correctly.

    if i replaced

    get_post_meta($post->ID, 'key', true)

    instead of

    echo get_post_meta($post->ID, 'key', true)

    in the theme used (the first post in)

    no right value return but incorrect pages

    The code from the first post looks to be inside a function based on the “return” statement.

    If so, you need to make sure you are passing the correct $post object to the function.

    What theme are you using? If it’s a public theme, I can reference the exact file and location to view the code/function.

    Thank you very much.

    The first post used in the public theme named “Selalu Ceria” download from Theme Channel.

    I believe you need the following in functions.php

    function selaluceria_excerpt_read_more() {
        global $post;
        return '<span class=\'clear\'><a class=\'readmore\' href=\'' . get_post_meta($post->ID, 'link', true) . '\'>' . __( 'Read More', 'selaluceria') . '</a></span>';

    I did not install or test the theme and code edits.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to get custom field value in the code’ is closed to new replies.