WordPress.org

Ready to get started?Download WordPress

Forums

Store metadata in variables or an array (2 posts)

  1. nieuws
    Member
    Posted 6 years ago #

    Hi everyone,

    Could someone please help me with the get_post_meta() function? I read the manual but it's a little meager when it comes to PHP functions and my OOP knowledge is not good enough (yet).

    I'd like to fetch data from the custom fields for every post using get_post_meta($post->ID, $key, $single); and save each value into a variable. Eventually i want to echo the data on certain places depending on the content.

    I tried several things but i always get empty echo/print_r. The solution is probably very easy but i can't seem to find it.

    I can rewrite this function in PHP using a new MySQL-connection, but i prefer to use the standard functions and connections instead so the site stays fast.

  2. stjohn79
    Member
    Posted 6 years ago #

    My solution to this problem was to fetch the array elements individually. Here's my code:

    <?php $links = get_post_meta($post->ID, 'port_links', false); ?>
    <?php echo $links[0]; ?>
    <?php echo $links[1]; ?>

    In this case I had two meta data values with the key port_links assigned to my post. I assigned this array to the variable $links. To display the values of an array you can call them by there placement in the array using [0], [1], [2]...etc.

    Cheers.

Topic Closed

This topic has been closed to new replies.

About this Topic