如何在其他页面上显示带有自定义字段的模板页面

时间:2018-12-17 13:45:55

标签: wordpress advanced-custom-fields polylang

所以我有点卡住了。我是WordPress的新手,但是,我需要完成一些工作。这是问题所在。

我在网站上有一个包含联系人的部分,在几页上重复几次。我将该页面作为模板,并向其中添加了自定义字段。所以现在,我需要在其他几个页面上显示它。但是,我似乎无济于事。我意识到我对WordPress的机制缺乏基本的了解,我仍在学习它,但我希望您能为我提供帮助。

当我尝试通过页面ID显示它时,如下所示:

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

它给了我空白页。如果在编辑器中向页面添加一些文本,则仅输出该文本。它不会输出模板的HTML结构。 因此,问题在于获取模板页面+自定义字段。

我在此上浪费了太多时间。

1 个答案:

答案 0 :(得分:0)

好的,因此在Wordpress专家的帮助下,我得到了答案:

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

由于我使用polylang,所以我将页面ID提交到polylang的pll_get_post,它以另一种语言找到该帖子,然后将页面ID变量添加到每个acf字段acho的末尾,作为另一个参数。然后,我将此页面作为包含添加到其他页面,它可以正常工作!!!