I use ACF and have a page template called 'About' which my 'About Us' page uses. I also have a homepage template that uses fields from my 'homepage field group'. My 'About Us' page shows ACF fields from the 'About field group' AS WELL AS some fields from my 'Homepage field group'.
I get the fields from my homepage group by referencing the ACF fields with the following php code:
<?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>
The H3
is from the homepage field group on the homepage and the paragraph is from my 'About Us' page intro field from the 'About field group'.
I want to do something similar but pull in fields from a custom post type (NOT the homepage).
My custom post type is called 'Common Modules' (slug = common_modules).
Is there a way to do this e.g:
<?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>
I have quite a few fields on my page that reference the homepage fields, so i need to be able to call $custom_post_type frequently throughout my page.
I cant figure it out?
Thanks for any help.
I'd use a custom query using 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>
Let me know if it works for you!
UPDATE
<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>