wordpress - 如何在其他页面上显示带有自定义字段的模板页面
问题描述
所以我有点卡住了。我是 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,它会找到另一种语言的帖子,然后我将该页面 id 变量添加到每个 acf 字段 acho 的末尾,作为另一个参数。然后我将此页面添加为其他页面的包含,它可以工作!!!
推荐阅读
- javascript - 使用 TOAST UI 编辑器,我无法上传超过 50kb 的图像
- vue.js - Vuetify 选项卡作为嵌套动态路由在父道具上出错
- vbscript - 如何在经典asp中将文本分隔为列表
- python - 使用游标在python中调用存储过程
- c - 没有 fclose() 的 FILE * 重新分配好吗?
- typescript - 在您的 Facebook 主页接受 Facebook 的潜在客户开发服务条款之前,您无法投放潜在客户广告
- php - PHP Regex - 如何在 preg_replace 数字反向引用上执行 str_replace?
- clion - 使用 Unreal Engine 4.25 时 CLion 不显示任何符号
- r - 如果值与 data.frame 中的向量匹配,则返回相邻列中的值
- c# - 如何让 WinForm 在后台运行/从 TaskManager/Apps 中隐藏