WordPress.org

Ready to get started?Download WordPress

Forums

Add custom field to sidebar (4 posts)

  1. seemsse
    Member
    Posted 5 years ago #

    I have read a lot about using global ID to display a custom field in a sidebar, but I'm having no luck.

    I have a page setup to display 3 posts from a specific category:
    <?php query_posts("cat=$cat&showposts=3"); while ( have_posts() ) : the_post() ?>

    which is working just fine. After this line I have the following:
    <?php global $wp_query; $postID = $wp_query->post->ID; ?>

    Then in the sidebar I have the following:
    <?php
    global $postID;
    $key1 = get_post_meta($postID, 'sidebar1', true); if($key1) { echo "<p>$key1</p>"; }
    ?>

    But the custom field sidebar1 is not appearing. What am I doing wrong? Thank You

  2. MichaelH
    Member
    Posted 5 years ago #

    In your sidebar.php add echo to test variable

    <?php
    global $postID;
    echo 'this is $postID ' . $postID;
    $key1 = get_post_meta($postID, 'sidebar1', true);
    if ( $key1 ) {
    echo '<p> This is the sidebar1 value: ' . $key1 . '</p>'";
    }
    ?>
  3. seemsse
    Member
    Posted 5 years ago #

    Ok, I got the postID to output, but nothing for the custom field. I double checked and the custom field is setup correctly with key of "sidebar1" and value of "test".

  4. Sarah Gooding
    Member
    Posted 4 years ago #

    I just posted a blog about creating custom sidebars, headers, footers, etc. using custom fields. Check out the quick tutorial here:
    http://wpmu.org/making-wordpress-more-modular-with-multiple-sidebars-headers-and-footers

Topic Closed

This topic has been closed to new replies.

About this Topic