Search code examples
wordpressadvanced-custom-fieldspolylang

How to display a template page with custom fields on other pages


So I'm a little stuck. I'm a total newbie in WordPress, and yet, I need to get something done. Here's the problem.

I have a section with contacts on the site, that repeats itself several times on several pages. I made that page a template and added custom fields to it. So now, I need to have it displayed on several other pages. However, nothing I do seems to work. I realize that I lack some fundamental understanding of WordPress mechanics, I'm still learning it, and yet, I hope you can help me.

When I try to display it by page id,like this:

$post_id = 605;
$newpost = pll_get_post($post_id);
$queried_post = get_post($newpost);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;

It gives me a blank page. If I add some text to the page in the editor, it outputs only that text. It does not output the HTML structure of my template. So the problem is getting the template page + custom fields.

I've wasted way too much time on this.


Solution

  • OK, so with help from Wordpress guru, I have an answer:

    <?php $page_contid = 921; 
            $newpost = pll_get_post($page_contid);
      ?>  
        <div class="section-contacts" style="background: <?php echo the_field('contact-section-color', $newpost);?>">
          <div class="container">
            <div class="section-info">
              <div class="section-info_contacts">
                <div class="heading"><?php echo the_field('contact-section-heading1', $newpost);?></div>
                <div class="item">
                  <img src="<?php echo the_field('contact-section-address_img', $newpost);?>" alt="">
                  <p><?php echo the_field('contact-section-address', $newpost);?></p>
                </div>
                <div class="item">
                  <img src="<?php echo the_field('contact-section-email_img', $newpost);?>" alt="">
                  <p><a href="<?php echo the_field('contact-section-email', $newpost);?>"><?php echo the_field('contact-section-email', $newpost);?></a></p>
                </div>
                <div class="item">
                  <img src="<?php echo the_field('contact-section-phone_img', $newpost);?>" alt="">
                  <div class="item-numbers">
                    <p><a href="<?php echo the_field('contact-section-phone1', $newpost);?>"><?php echo the_field('contact-section-phone1', $newpost);?></a></p>
                    <p><a href="<?php echo the_field('contact-section-phone2', $newpost);?>"><?php echo the_field('contact-section-phone2', $newpost);?></a></p>
                  </div>
                </div>
              </div>
    

    Since I use polylang, I submit the page id to polylang's pll_get_post, it finds that post in another language, then I add that page id variable to the end of each acf field acho, as another argument. Then I add this page as an include to other pages, and it works!!!