Ask your WordPress questions! Pay money and get answers fast! (more info)

Need to know how to include permalink

  • SOLVED

I have code pulling in information through a relational Advanced Custom Fields setting.

The code looks like this:

<code
<?php
//Loops through the repeater
while (have_rows('favorite_oils')) {
the_row();

//fetches the post object field. Assumes this is a single value, if they can select more than one you have to do a foreach loop here like the one in the example above
$oil_object = get_sub_field('select_oil');
if($oil_object){
//Fetch the image field from the oils post
$image = get_field('main_image_link', $oil_object->ID);
}

//Echo out the image with the medium size. Change this as you like!
echo ' <div class="team-oils-small"><img src="' . $image . '" /></div>';
}
?>
</code>

That works perfectly, and pulls an image from a custom post type that is selected. What I need to know how to do is link to the permalink for that post. How do I call in the related permalink and then include it? Thanks!

Answers (3)

2015-08-22

Andrea P answers:

can't you do simply like this?


<?php
//Loops through the repeater
while (have_rows('favorite_oils')) {
the_row();

//fetches the post object field. Assumes this is a single value, if they can select more than one you have to do a foreach loop here like the one in the example above
$oil_object = get_sub_field('select_oil');
if($oil_object){
//Fetch the image field from the oils post
$image = get_field('main_image_link', $oil_object->ID);
$post_url = get_permalink($oil_object->ID);
}

//Echo out the image with the medium size. Change this as you like!
echo ' <div class="team-oils-small"><a href="'.$post_url.'" ><img src="' . $image . '" /></a></div>';
}
?>


Kyler Boudreau comments:

Andrea - your code worked beautifully. Thanks!!!

2015-08-22

Jayaram Y answers:

can you try this once.. (untested)


<?php
//Loops through the repeater
while (have_rows('favorite_oils')) {
the_row();

//fetches the post object field. Assumes this is a single value, if they can select more than one you have to do a foreach loop here like the one in the example above
$oil_object = get_sub_field('select_oil');
if($oil_object){
//Fetch the image field from the oils post
$image = get_field('main_image_link', $oil_object->ID);
}

//Echo out the image with the medium size. Change this as you like!
echo '<div class="team-oils-small"><a href="'. the_permalink() .'"><img src="' . $image . '" /></a></div>';
}
?>

2015-08-22

Bob answers:

try this function [[LINK href="https://codex.wordpress.org/Function_Reference/get_post_permalink"]]https://codex.wordpress.org/Function_Reference/get_post_permalink[[/LINK]]

is your code in loop?
is it archive page or single custom post type page or code somewhere in page template?