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>
TheH3
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 usingWP_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>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.