0

I got a post that should be embeded, included or outputted - however you call it - into another post.

I prefer a solo code variant.

So far I have tested various shortcode plugins - but they don't seem to catch the attached acf fields of my post.

The embedded post is a wall of several elements that should be attached now to other posts.

Is this possible in Wordpress? In Drupal you can easily load a node content (page content) into the template of another.

How to achieve this in WP?

Is there a way to import a RENDERED version of a post with all of its contents (also acf fields)?

Thanks?

  • If I understand correctly your question I would make a custom _render_x_cpt_html.php file and then include it in the single.php of the x custom post type, and also where you want to embed it, make a new WP_Query there, and afterward reset postdata. – D. Dan Jun 28 '18 at 10:43
  • Thanks for your reply. Would that "_render_x_cpt_html.php" just be static html or a WP post entry? Could you show me an example? How is that _render_x_cpt_html.php file bound to an actual post element, that is changed via the backend? – mogio Jun 28 '18 at 10:46
  • I'll expand it in an answer. – D. Dan Jun 28 '18 at 10:49
1

To not duplicate code you could make a file in your theme/child-theme for the html of the post which you intend to embed, let's say x_cpt_render_html.php:

function get_x_cpt_html(){ ?>
<div class="x-cpt">
<h2> <?php the_title(); ?> </h2>
<div class="content"> <?php the_content(); ?></div>
<div class="custom"> <?php the_field('custom'); ?> </div>
</div><?php
}

Then you could embed this file in the single.php or wherever it's needed anyway for display for that particular custom post type:

get_template_part('x_cpt_render_html');
while ( have_posts() ) : the_post();
get_x_cpt_html();
endwhile;

Somewhat similar where you want to embed it:

$loop = new WP_Query( array( 'post_type' => 'that_post_type',
        'posts_per_page' => -1,);
get_template_part('x_cpt_render_html');
while ( $loop->have_posts() ) : $loop->the_post();
get_x_cpt_html();
endwhile;
wp_reset_postdata();
  • Works. Very good answer. I do my query now in the template of an acf field element. So I am able to position the content exactly where I want it to sit. The field defines an reference to a post. This should be powerful enough. Thanks again. – mogio Jun 28 '18 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.