WordPress.org

Ready to get started?Download WordPress

Forums

I need widget to display a linked post (6 posts)

  1. photocurio
    Member
    Posted 2 years ago #

    I have a client who wants complete control over his posts. He wants to display a related post (thumbnail and excerpt) in his sidebar but wants control over what post shows up.

    It seems to me that there must be a way to link posts. In a post's edit screen you could specify link to this other post. Then a widget could grab that linked post and put it in the sidebar. Ideally, if no post was linked, nothing would show in the sidebar.

    Anyone know of such a tool?

  2. photocurio
    Member
    Posted 2 years ago #

    If I were editing a post, and I added another post's ID in a custom field (i.e. link two posts together), could I write a one-post loop that would pick up that ID and show that post?

    Would the ID would have to be saved to global in some way?

  3. esmi
    Forum Moderator
    Posted 2 years ago #

  4. photocurio
    Member
    Posted 2 years ago #

    OK, it seems that I don't really need a loop in my sidebar. The template tag get_post should do the trick. I've come up with the following php that could be a widget. This assumes:
    a) the widget is only on single posts
    b) there is a custom field called 'linked_post' and the value is the ID of the other post.

    <?php if ( get_post_meta($post->ID, 'linked_post', true) ) :
    $the_ID = get_post_meta($post->ID, 'linked_post', true);
    $my_linked_post = get_post($the_ID);
    if( $my_linked_post ) {
    echo the_title();
    }
    else();
    endif; ?>

    Unfortunately there seem to be errors in my code. Anyone want to try to fix it? [edit: changed get_posts to get_post, no plural]

  5. photocurio
    Member
    Posted 2 years ago #

    Um, I see why that's not working:
    get_post_meta($post->ID, 'linked_post', true);
    would only work inside the loop. I need to get a post's custom field value from outside the loop.

  6. photocurio
    Member
    Posted 2 years ago #

    Does anyone know how to get a post's custom field value from outside the loop?

    i.e. you are on a single-post, you have a sidebar widget, and the widget needs something from the post's custom field.

Topic Closed

This topic has been closed to new replies.

About this Topic