0

I am editing the archive template so that when an archive is viewed from a custom taxonomy called "organisation" the template takes a url from the description field of that organisation and converts it into a link. i.e. if the organisation viewed is RandomCo then at the head of the archive page there would be a link to their website.

The only data in RandomCo's description is the url. I intend to add a custom field to the taxonomy, but wanted to get the template working using the description field first.

The variable picks up the url and echos it perfectly, but the link returns nonsense, beginning with the address to the organisation's archive page on my site.

I've tried a range of echo/ href combinations (see the code) but all return the same result.

    <?php

    $orgdesc = get_the_archive_description();

            // I've tried these 4 approaches

    echo "<a href='".$orgdesc."'>Link</a>";

    echo "<a href='$orgdesc'>Link</a>";

    $url = $orgdesc;
    $link = '<a href="'.$url.'" target="_blank">Link</a>';
    echo $link;

?>

    <a href="<?php echo $orgdesc;?>"> Link </a>

The links from all the efforts above return the same value: http://example.com/organisation/randomco/%3Cp%3Ehttps://randomco.com/%3C/p%3E

1 Answers1

0

After decoding your example link, I've noticed that your organization url is wrapped in the html <p></p> tag. You have to unwrap that link. After that it should work correctly. You have to also remeber to prefix all of your urls with "http://" or "https://" to make it always absolute.

Maciej Król
  • 382
  • 1
  • 8
  • Thank you for the reply. How do I unwrap the

    ? There is no other data in the description field and $orgdesc echos just the url.

    – user12033678 Sep 17 '19 at 23:20
  • You should check your get_the_archive_description() function for mistakes, but if you need fast solution, just use this: preg_replace('/

    (.*)<\/p>/', '$1', $url)

    – Maciej Król Sep 17 '19 at 23:30
  • Found it, https://stackoverflow.com/questions/12130966/how-do-i-remove-p-tag-and-its-content-from-html-using-php - all 4 approaches work perfectly now. Thank you. – user12033678 Sep 17 '19 at 23:33