Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBart van Pinxteren
4,161 Pointslinking from within one custom post to another
Hello, I am enjoying WP courses a lot and learning fast. Now I am kind of stuck and would appreciate some help.
I am using the recommended Plugins ACF and CPT and want to link from within one custom post to another using the field type 'relationship', that should return the ID of a selected field.
I am using this code:
<p class="concert-details";>Location: <a href="<?php echo get_permalink( <?php the_field( 'location_concert' ); ?> ); ?>">Stadium</a></p>
But where
<?php echo get_permalink( 'location_concert' ); ?>
returns 90 (the ID of the post referred to), the above mentioned code doesn't link to that page. But if I replace the get_permalink with 90:
<p class="concert-details";>Location: <a href="<?php echo get_permalink( 90 ); ?>">Stadium</a></p>
It does the trick!
I hope I am making myself clear and I hope somebody can help me.
Regards, Bart
5 Answers
Kevin Korte
28,149 PointsWithout running your code on my computer to be sure, you could load your permalink in variable, and than use that variable, so something like.
<?php $location = get_permalink( 'location_concert' );?>
<p class="concert-details";>Location: <a href="<?php echo get_permalink( $location ); ?>">Stadium</a></p>
Bart van Pinxteren
4,161 PointsKevin, you just made my day! Thank you so much. This is all I needed. Best wishes to you from The Netherlands!
Bart van Pinxteren
4,161 PointsHi Kevin, I am afraid I have to get back to you. I was a bit overenthusiastic... With your code the link directs to a post, but not the proper one. It directs me from the list to the single post (concert), but not to the post (location) that it should link to. If I put 90 between the brackets in de href it works. But if I put the string there ($location) that should contain the integer 90 (that is the ID of the post that location-concert refers to), it doesn't work. Hope you can give it another try...
Kevin Korte
28,149 PointsI have a wordpress testbed I'll plug this code into and see what happens. I wasn't sure if it would work or not. I'm in the US and today is Thanksgiving, so I'm not sure if I'll have the time today, but should be get back to you by tomorrow if you haven't worked it out yet. Cheers!
Kevin Korte
28,149 PointsBummer!
Bart van Pinxteren
4,161 PointsI would appreciate it very much if you could have a second look one of these days. Happy Thanksgiving!