如何在其他页面上显示带有自定义字段的模板页面
How to display a template page with custom fields on other pages
所以我有点卡住了。我是 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 结构。
所以问题是获取模板页面+自定义字段。
我在这上面浪费了太多时间。
好的,在 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,它发现post是另一种语言,然后我将该页面id变量添加到每个acf字段acho的末尾,作为另一个论点。然后我将此页面作为包含添加到其他页面,并且有效!!!
所以我有点卡住了。我是 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 结构。 所以问题是获取模板页面+自定义字段。
我在这上面浪费了太多时间。
好的,在 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,它发现post是另一种语言,然后我将该页面id变量添加到每个acf字段acho的末尾,作为另一个论点。然后我将此页面作为包含添加到其他页面,并且有效!!!