WordPress.org

Ready to get started?Download WordPress

Forums

Include post B in post A (10 posts)

  1. Zuperfly
    Member
    Posted 7 years ago #

    (How) is it possible to include a post B (or parts of it like the title or other fields) in another post A? (How) can two posts be linked/associated?

  2. Zuperfly
    Member
    Posted 7 years ago #

    It is possible.
    To include title and content of post B (ID70) in post A, add this to the single.php:

    <?php $order = "70";
    $myposts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID IN ($order) ORDER BY FIELD(ID, $order)"); ?>
    <?php if($myposts) : foreach($myposts as $post) : setup_postdata($post); ?>
    <h3><?php the_title(); ?></h3>
    <?php the_content(); ?>
    <?php endforeach; endif; ?>

  3. Zuperfly
    Member
    Posted 7 years ago #

    Problem is: How do I get back to the original loop afterwards? It seems that after I inserted the code above inside the main loop, every tag after this code is referring to post B, not to the main loop (post A) .. any hints on this?

  4. moshu
    Member
    Posted 7 years ago #

    Why don't you try this plugin?
    http://guff.szub.net/2005/01/27/get-a-post/

  5. Zuperfly
    Member
    Posted 7 years ago #

    I tried this before, but it's not working either. When you look at the plugin code, how is the plugin supposed to know when the second loop is finished and that I want to return to the original loop?

    <?php get_a_post(70); ?>
    <h3><?php the_title(); ?></h3>
    <?php the_content(); ?>

    I think the plugin will only work outside the loop. But thanks anyway!

  6. Kafkaesqui

    Posted 7 years ago #

    You're going to have to nix the various Loop-based template tags for your endeavor here. But first, you can collect the post this way:

    <?php $post_B = new WP_Query('p=70'); ?>

    Then to display various bits of the post, you can echo the 'post' properties in $post_B:

    <h3><?php echo apply_filters('the_title', $post_B->post->post_title); ?></h3>
    <?php echo apply_filters('the_content', $post_B->post->post_content); ?>

    Note how I've passed them through the apply_filters() function (with the appropriate hook) to make sure all the usual formatting, etc occurs. But you can bypass this if you don't need it:

    <h3><?php echo $post_B->post->post_title; ?></h3>
    <?php echo $post_B->post->post_content; ?>

  7. Zuperfly
    Member
    Posted 7 years ago #

    Thanks, that's exactly what I was looking for!
    And it works well, except with the custom fields - I don't manage to display the custom fields values for post B:

    <?php echo $post_B->post->post_title; ?>
    <?php echo $post_B->post->get_post_custom_values('field1'); ?>

    Maybe get_post_custom_values isn't the right tag?

  8. Zuperfly
    Member
    Posted 7 years ago #

    I looked up the custom fields stuff in the codex and voilĂ :

    <?php echo get_post_meta($post_B->post->ID,'field1', $single=TRUE); ?>

    That does the trick.

  9. farlane
    Member
    Posted 6 years ago #

    Thank you.

    This post answered my question about how to include a post within a page (or a post within a post or a page within a post).

    This would work using exec-php as well.

  10. Marcomail
    Member
    Posted 6 years ago #

    could you show me the complete code ? I was searching something similar some days ago.

    Now i'm using your code outside the loop and use the css to move the block where i want show it.

Topic Closed

This topic has been closed to new replies.

About this Topic