Forums

Show Custom fields in the sidebar (4 posts)

  1. Odenberg
    Member
    Posted 4 months ago #

    I want some of my posts to have specific links, uniq to that post. And I want these links to show in the sidebar.

    So, I use Custom fields. But how do I get the Custom fields to my sidebar?

    Very, veeeery grateful for some help...

  2. zoonini
    help me help you
    Posted 4 months ago #

    Here's some code I use to display two custom fields in a sidebar. Replace MyField1 & MyField2 with your custom field names.

    <?php // display custom fields
    	global $wp_query;
    	$postid = $wp_query->post->ID;
    
            echo get_post_meta($postid, 'MyField1', true);
    
    	echo get_post_meta($postid, 'MyField2', true);
    	wp_reset_query();
    ?>

    Function Reference/get post meta

  3. Odenberg
    Member
    Posted 4 months ago #

    Thank you so much!
    I tried like 10 different "solutions", non worked. But yours did. Do you know if there is a way to combine your code with a UL? Like wrap it in a UL or something?

  4. zoonini
    help me help you
    Posted 4 months ago #

    You're welcome.

    I tried like 10 different "solutions", non worked. But yours did.

    That may be because most examples are meant to work only within the loop, whereas this is one specifically meant to work outside the loop, which is where the sidebar is.

    Do you know if there is a way to combine your code with a UL? Like wrap it in a UL or something?

    Sure - you can add whatever other HTML you want to the code, for example here's an opening ul & li:

    echo "<ul><li>";

    To "concatenate" code (put two bits together) you're echoing, use a dot in between the elements.

    Example:

    echo "<ul><li>" . get_post_meta($postid, 'MyField1', true);

    You can probably figure out the rest. :-)

    Ref: http://phphowto.blogspot.com/2006/12/concatenate-strings.html

Reply

You must log in to post.

About this Topic