WordPress.org

Forums

How to get custom field value in the code (11 posts)

  1. do1do2
    Member
    Posted 2 years ago #

    I have set a custom field "link" for post and give it a value,such as "http://www.abc.com" 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 http://www.123.com/array

    123.com is my webste
    abc.com 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.

  2. do1do2
    Member
    Posted 2 years ago #

    only get return http://www.123.com/array

    i want to get the custom field value "http://www.abc.com"

    how to fix the code?

  3. Ming Sheu
    Member
    Posted 2 years ago #

    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.
    get_post_custom($post_id);

  4. do1do2
    Member
    Posted 2 years ago #

    Hi,Ming

    Thank you.

    I replace the code with the following instead:

    get_post_meta($post_id, $link, $single)

    but get value "www.123.com"

    "www.123.com" 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?

  5. Ming Sheu
    Member
    Posted 2 years ago #

    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)

  6. do1do2
    Member
    Posted 2 years ago #

    Thank you.

    I have replaced it as yours but only get the hyperlink "www.123.com" (it's my website homepage website) unavailable to get the field value "www.abc.com"

    other suggestions?

  7. Ming Sheu
    Member
    Posted 2 years ago #

    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?

  8. do1do2
    Member
    Posted 2 years ago #

    hi,

    <?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

  9. Ming Sheu
    Member
    Posted 2 years ago #

    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.

  10. do1do2
    Member
    Posted 2 years ago #

    Thank you very much.

    The first post used in the public theme named "Selalu Ceria" download from WordPress.org Theme Channel.

  11. Ming Sheu
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic