Ready to get started?Download WordPress


Flexible Posts Widget
[resolved] How to display a custom field in sidebar? (6 posts)

  1. nicklumina
    Posted 1 year ago #

    I've successfully created a customized html output template by renaming widget.php and changing template filename, following your instructions. Works fine. Sidebar shows Title, and main content field from a custom post type I set-up. All good.

    Now I want to show a custom field in this post type (name of custom field = "description-presentation", INSTEAD OF each post's main content field. To do that, I've tried inserting this "get_custom_post" function into my custom template for this plug-in:
    $custom_fields = get_post_custom();
    $description-presentation = $custom_fields['description-presentation'];
    foreach ( $description-presentation as $key => $value )
    echo $key . " => " . $value . "
    I've placed it both inside and outside the loop (before and after the "while have-posts" statement).

    Nothing works. The sidebar is blank.

    Can you tell me how to display my custom field in the sidebar?

    Thank you in advance


  2. DaveE
    Plugin Author

    Posted 1 year ago #

    Hi Nick,

    Since we're in a sidebar and not within the "main" loop, the global $post variable isn't available for your call to get_post_custom. Try getting the ID for the current post being looped through and using that in your call like this:

    get_post_custom( get_the_ID() )

    Alternatively, you could use get_post_meta() instead of get_post_custom() like this:

    $description-presentation = $get_post_meta( get_the_ID(), 'description-presentation' );.

    Using get_post_meta() saves you having to find the description keys in the full set of meta key/values for the post.

    get_the_ID() reference: http://codex.wordpress.org/Function_Reference/get_the_ID

    get_post_meta() reference: http://codex.wordpress.org/Function_Reference/get_post_meta


  3. nicklumina
    Posted 1 year ago #

    Thanks, Dave. Still no success and I'm not sure what I'm doing wrong (I don't program PHP). Adding the code you suggested, here's my custom template file, starting after
    ... "if( $flexible_posts->have_posts() )?>" ,
    and ending before "echo $after_widget"

    <ul class="dpe-flexible-posts">
            <?php while ( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); ?>
          <li style="margin:0;padding:0" id="post-<?php the_ID(); ?>" <?php post_class(); ?>
          <h4 class="title" style="margin:0;padding:0;font-family:Arial,Verdana,sans-serif"><?php the_title(); ?></h4>
          <p style="margin:0;padding:0">
    <?php $description-presentation = $get_post_meta( get_the_ID(), 'description-presentation' );?>
    <?php the_excerpt();?>
        <?php endwhile; ?>
    		<!-- .dpe-flexible-posts -->

    Thanks again.


    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum's parser.]

  4. DaveE
    Plugin Author

    Posted 1 year ago #

    You'll still need the foreach loop from your original example. Here's the whole code block together: http://pastebin.com/mvS0QXA4. Cheers!

  5. nicklumina
    Posted 1 year ago #

    Thanks again, Dave. I carefully inserted your code example into my script but this time only the Title of one post appeared. Not the custom field ('description-presentation') and not the excerpt. What's more, the other 3 posts of this category (I have set the widget to show 4 most recent) didn't show up either.

    At this point I will not ask you to do anymore work on it, since you're not paid for your time on this. I'll figure out a work-around, still using your plug-in but not relying on displaying a custom field in the sidebar post (instead of the default).

    In case you're interested, here's the URL of the dummied-up, not fully styled website. Your Flexible Posts Widget produces the right sidebar. Content for each post is the main, default edit screen:

    Thank you again for your time.


  6. DaveE
    Plugin Author

    Posted 1 year ago #

    Hi Nick,

    I'm sorry we weren't able to get this to work together. Let me know if I can be of further assistance debugging it. For now, I'm going to consider this thread closed.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic