0

How do I change the function below so that it outputs a list if links rather than a string? The resulting html I need is <a href="#">term</a> <a href="#">term</a> <a href="#">term</a> etc.

function my_post_term_links() {

// Get an array of all taxonomies for this post
$taxonomies = get_taxonomies( '', 'names' );

// Are there any taxonomies to get terms from?
if ( $taxonomies ) {    

    // Call the wp_get_post_terms function to retrieve all terms. It accepts an array of taxonomies as argument. 
    $arr_terms = wp_get_post_terms( get_the_ID(), array_values( $taxonomies ) , array( "fields" => "names" ) );

    // Convert the terms array to a string
    $terms_links = implode( ' ',$arr_terms );

    if (!filter_var($terms_links , FILTER_VALIDATE_URL) === false) {
       $terms_links ="<a href='#'>".$terms_links."</a>";
    }  

    // Get out of here
    return $terms_links;
}
}

UPDATE//

I managed to get the function to display and list of terms with links as required by adding a for loop but it duplicates teh last terms and adds it on the end without a link...

    foreach ( $arr_terms  as $terms_links ) {
        echo '<a href="#">' . $terms_links . '</a>';
    }

So this is how the function looks now...

function my_post_term_links() {

// Get an array of all taxonomies for this post
$taxonomies = get_taxonomies( '', 'names' );

// Are there any taxonomies to get terms from?
if ( $taxonomies ) {    

    // Call the wp_get_post_terms function to retrieve all terms. It accepts an array of taxonomies as argument. 
    $arr_terms = wp_get_post_terms( get_the_ID(), array_values( $taxonomies ) , array( "fields" => "names" ) );

    // Convert the terms array to a string
    //$terms_links = implode( ' ',$arr_terms );

    foreach ( $arr_terms as $terms_links ) {
        if(!empty($terms_links)) {
        echo '<a href="#">' . $terms_links . '</a>';
        }
    }

    /*if (!filter_var($terms_links , FILTER_VALIDATE_URL) === false) {
       $terms_links ="<a href='#'>".$terms_links."</a>";
    }  */

    // Get out of here
    return $terms_links;
}
}
4
  • And what is the current output?
    – brance
    Commented May 13, 2015 at 11:07
  • the current output is a string e.g. term term term
    – Amesey
    Commented May 13, 2015 at 11:12
  • a string of terms without links that is, and i need the links to have hashtags in as above.
    – Amesey
    Commented May 13, 2015 at 11:13
  • I've nearly got it!! I updated the the post above please see...
    – Amesey
    Commented May 13, 2015 at 11:23

1 Answer 1

0

Then try to add

foreach ( $arr_terms  as $terms_links ) {
    if(!empty($terms_links)) {
    echo '<a href="#">' . $terms_links . '</a>';
    }
}

That checks if the $terms_links is empty, and if so it wont print it out.

5
  • That didn't work, same ouput.
    – Amesey
    Commented May 13, 2015 at 11:28
  • What exactly is your HTML output, can you post it here?
    – brance
    Commented May 13, 2015 at 11:32
  • <a href="#">term</a><a href="#">term</a><a href="#">term</a><a href="#">term</a><a href="#">term</a>term
    – Amesey
    Commented May 13, 2015 at 11:39
  • just amended the post showing how the function looks now
    – Amesey
    Commented May 13, 2015 at 11:43
  • Try to comment out the "return $terms_links;" maybe that is the problem.
    – brance
    Commented May 13, 2015 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.