Home › Forums › General Issues › Using the URL of a relationship field item
Hi Guys – please help – been working on this for hours with no success: I have the following page:
http://flock.simonpointer.com/our-clients/ which shows simply featured images from a custom post type called clients running on a custom template as follows:
(I should mention that I inherited this template code from someone else and it’s a little old and delicate, so I nervous sbout re making the template even if the code in it is not ideal).
<?php
/**
* Template name: Our clients
*
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div class="col-1-1 intro-blurb">
<?php echo '<h1>'.get_the_title().'</h1>'; ?>
<?php echo get_the_content(); ?>
</div>
<div style="clear:both"></div>
<div id="client-logos">
<?php
$select_html = "";
//get the list of offices
$args = array( 'post_type' => 'clients', 'order' => 'ASC', 'posts_per_page' => '-1');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
if ( has_post_thumbnail() ){
//the_post_thumbnail('large');
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );
$url = $thumb['0'];
?>
<div class="client-logo-outer">
<div class="client-logo-border">
<div class="client-logo-inner">
<a href="">
<img src="<?php echo $url; ?>" alt="<?php echo get_the_content(); ?>" title="<?php echo get_the_content(); ?>"/>
</a>
</div>
</div>
</div>
<?php
}
endwhile;
?>
</div>
<?php endwhile; // End of the loop. ?>
<div class="no-comment" style="clear:both"></div>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
This works in that it displays a list of featured images on the page.
I have added an ACF relationship field to the clients posts type that allows the editor to specify a related case study from another custom post type called “case-history”.
All I want to do is adapt the above template to retrieve the url of the related case-history post and use it for a link from the clients featured image – which is a logo.
I have followed the tutorial on the ACF site for this and adapted the code as follows:
<?php
/**
* Template name: Our clients
*
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div class="col-1-1 intro-blurb">
<?php echo '<h1>'.get_the_title().'</h1>'; ?>
<?php echo get_the_content(); ?>
</div>
<div style="clear:both"></div>
<div id="client-logos">
<?php
$select_html = "";
//get the list of offices
$args = array( 'post_type' => 'clients', 'order' => 'ASC', 'posts_per_page' => '-1');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
if ( has_post_thumbnail() ){
//the_post_thumbnail('large');
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );
$url = $thumb['0'];
?>
<?php
/*
* Query posts for a relationship value.
* This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
*/
$case_link = get_posts(array(
'post_type' => 'clients',
'meta_query' => array(
array(
'key' => 'related_case', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
?>
<?php foreach( $case_link as $case_link ): ?>
<?php
$link = get_field('related_case', $cases->ID);
?>
<div class="client-logo-outer">
<div class="client-logo-border">
<div class="client-logo-inner">
<a href="<?php echo get_permalink( $case_link->ID ); ?>">
<img src="<?php echo $url; ?>" alt="<?php echo get_the_content(); ?>" title="<?php echo get_the_content(); ?>"/>
</a>
</div>
</div>
</div>
<?php
}
endwhile;
?>
</div>
<?php endwhile; // End of the loop. ?>
<div class="no-comment" style="clear:both"></div>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
I can’ seem to get this working at all – this template now causes a 500 server error. I suspect it’s because I have not got the multiple loops and endif’s working properly.
Be much appreciated if someone could help out?
Thanks
Simon
@simonpointer try setting WP_DEBUG
to 1
or true
in your wp-config.php file. That will show you where errors are happening.
Just from a quick look, towards the end of your code you have a }
right before your endwhile;
. If you look above, you have foreach():
, so either change that to foreach(){
or change that }
to endforeach
. I hope this helps.
Phil
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!
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.