首页 > 解决方案 > 如何在其他页面上显示带有自定义字段的模板页面

问题描述

所以我有点卡住了。我是 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 结构。所以问题是获取模板页面+自定义字段。

我在这上面浪费了太多时间。

标签: wordpressadvanced-custom-fieldspolylang

解决方案


好的,所以在 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 的末尾,作为另一个参数。然后我将此页面添加为其他页面的包含,它可以工作!!!


推荐阅读