Wordpress ACF 字段如何从自定义 post 类型 get_option

Wordpress ACF fields how to get_option from custom post types

我使用 ACF 并有一个名为 'About' 的页面模板,我的 'About Us' 页面使用该模板。我还有一个主页模板,它使用了我 'homepage field group' 中的字段。我的 'About Us' 页面显示来自 'About field group' 以及 我的 'Homepage field group'.

的一些字段的 ACF 字段

我通过使用以下 php 代码引用 ACF 字段从我的主页组中获取字段:

<?php $home = get_option('page_on_front'); ?>
<h3><?php the_field('featured_text' , $home); ?></h3>
<p><?php the_field('intro');?></p>
<h2><?php the_field('header', $home);?></h2>

H3 来自主页上的主页字段组,段落来自 'About field group'.

我的 'About Us' 页面介绍字段

我想做类似的事情,但是从自定义 post 类型(不是主页)中拉入字段。

我的自定义 post 类型称为 'Common Modules'(slug = common_modules)。

有没有办法做到这一点,例如:

<?php $custom_post_type = get_post_type('Common Modules'); ?>
<h3><?php the_field('featured_text' , $custom_post_type); ?></h3>
<p><?php the_field('intro');?></p>
<h2><?php the_field('header', $custom_post_type);?></h2>

我的页面上有很多引用主页字段的字段,因此我需要能够在整个页面中频繁调用 $custom_post_type。

我想不通?

感谢您的帮助。

我会使用自定义查询 WP_Query:

<?php
$custom_query = new WP_Query(array(
  "post_type" => "common_modules" // use the name of your cpt here. The name when you register it using "register_post_type"
));

while ($custom_query->have_posts()) {
  $custom_query->the_post();?>
<h3><?php the_field('featured_text'); // you don't need to pass the second argument. it defaults to the current post in the while loop?></h3>
<?php };
wp_reset_postdata();
?>

<p><?php the_field('intro');?></p>

让我知道它是否适合你!


更新

<h3><?php the_field('featured_text', '495'); //You could use the id of that specific post to get the value of your acf field?></h3>

<p><?php the_field('intro');?></p>

<h2><?php the_field('header', '495');?></h2>