WordPress.org

Support

Support » Plugins and Hacks » Include post B in post A

Include post B in post A

  • (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?

Viewing 9 replies - 1 through 9 (of 9 total)
  • 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; ?>

    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?

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

    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!

    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; ?>

    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?

    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.

    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.

    Marcomail
    Participant

    @marcomail

    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.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Include post B in post A’ is closed to new replies.