Display URL from Page Attribute in Page List

Hi All,

I’m just wanting to have a link from a page attribute show up in a page list if one is set, and not be there if it is not.

This works but displays the URL text if an attribute isn’t set for the page. Can anyone help out? I’m not much with PHP. I’m assuming it’s some kind of If/Else?

<?php
	$url = $c->getAttribute('my_attribute');
	echo '<a href="' . $myattribute . '" target="_blank">' . 'View My Link' . '</a>';
?>

Not sure if there wass more code to this or not. You’re using two different variables there, $url and $myattribute. This should do it for you

<?php
	if ($c->getAttribute('my_attribute') != '') {
		$url = $c->getAttribute('my_attribute');
		echo '<a href="' . $url . '" target="_blank">' . 'View My Link' . '</a>';
	}
?>

Edit: If your $myattribuite variable was correct keep it as is…

Hrm - I added the attribute, in this case ‘mls_listing’. But nothing was displayed via this code.

<?php
    if ($c->getAttribute('mls_listing') != '') {
    $url = $c->getAttribute('mls_listing');
    echo '<a href="' . $url . '" target="_blank">' . 'View MLS Listing' . '</a>';
    }
?>

If you’re doing this in a page list, make sure you’re doing it inside the foreach ($pages as $page) loop. In this case your $c variables *should be $page. If you could post a larger chunk of the code or send me the files I could have a closer look at what’s going on also :slight_smile:

1 Like

Ah - Yeah, that got it - It was the $c not being $page. It totally works. Thank you for your help.

2 Likes