Home › Forums › Front-end Issues › CPT and ACF
I have two CPTs setup. One is Clients and the other is Projects. Each of specific ACF fields.
Clients has a field name called client_name and Projects has a relationship field based on client_name which is project_client. It nicely lists out the Client names.
What I need to accomplish is to display the full client name. Right now it’s just the post ID for the client. See http://taxonomystrategies.com/wp/services/clients/
I will be adding code to not display any link if a certain field is empty.
This is the full template:
<?php
/*
Template Name: Projects
*/
?>
<?php get_header(); ?>
<div id=”wrapper”>
<div id=”blurb”><?php the_field(‘page_blurb’); ?></div>
<div id=”content”>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class=”post” id=”post-<?php the_ID(); ?>”>
<div class=”entrytext”>
<?php the_content(‘<p class=”serif”>Read the rest of this page »</p>’); ?>
<?php link_pages(‘<p>Pages: ‘, ‘</p>’, ‘number’); ?>
</div><!– / end entry text –>
<!– acf –>
<div id=”clients” style=”width: 95%; background-color: #ffffff;margin: 0 auto;overflow: hidden;border: 10px solid #fff;”>
<?php $args = array( ‘post_type’ => ‘projects’, ‘posts_per_page’ => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
//the_title();
echo ‘<div class=”project-content”>’;
echo “<div class=’projects’>”;
echo “<div class=’client-name’><a href='”;
the_field(‘project_url’);
echo “‘>”;
the_field(‘project_client’);
echo “</div>”;
echo “<div class=’pdescription’>”;
echo ““;
the_field(‘project_type’);
echo ““;
the_field(‘project_description’);
echo “</div>”;
echo “<div style=’clear: both;’></div>”;
echo “</div><!– / end projects –>”;
echo ‘</div>’;
endwhile;?>
</div> <!– / end clients –>
</div><!– / end post –>
<?php endwhile; endif; ?>
</div><!– / end content –>
<?php get_sidebar(); ?>
</div><!– / end wrapper –>
<?php get_footer(); ?>
Hi @crlannen
The returned data from a relationship field is a list of posts. Please take a look at this page to learn more about how to use relationship field: http://www.advancedcustomfields.com/resources/relationship/.
I hope this helps.
Unfortunately none of them work. My code pull in the ID of the relationship field (from Clients CPT via the Projects). What I need it to display the meta_value.
So instead of post_id 312 it needs to display Small Business Administration.
The Projects client information is serialized data, e.g., meta_key is project_client with meta_value of a:1:{i:0;s:3:”312″;}
Hi @crlannen
I believe you can use this code:
<?php
$posts = get_field('project_client');
$projectUrl = get_field('project_url');
if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<div class="client-name"><a href="<?php echo $projectUrl; ?>"><?php the_field('client_name'); ?></a>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
If it isn’t working, could you please share the JSON export file of your field group?
Thanks!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.