Forums

how to retrieve custom field value as a function (4 posts)

  1. dlusionz
    Member
    Posted 10 months ago #

    Hi, im interested in having a custom field "url" and in the value, add a url, that part is easy

    however I want to create a function $site, which can be echo´d inside of single.php in order to reproduce the value, I have no idea how to do this, Ive searched the codex, and it provides examples for slightly more complex things, such as showing the values of all custom fields in an array or ordered list and it gets a bit confusing for me

    I've tried using

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

    thanks in advance!

  2. vtxyzzy
    Member
    Posted 10 months ago #

    Is the name of your Custom Field 'site'? If so, the code you showed should work. If the name of the CF is 'url', then you need this:

    <?php $site = get_post_meta($post->ID, 'url', true); ?>
    <?php echo $site; ?>
  3. dlusionz
    Member
    Posted 10 months ago #

    hi, thanks for the answer, the name of the CF is indeed "site", but its not exactly working, It apparently is not retrieving de post ID correctly, if I refer to it manually it works, but if I don't it does´t echo anything

    do you have any ideas?

  4. dlusionz
    Member
    Posted 10 months ago #

    hey! I read up on some similar but significantly different posts on here and I fixed it now, it seems I needed to have
    <?php global $post; ?> before anything

    thanks again though!

Reply

You must log in to post.

About this Topic