0

I have two functions displaying a term list of the selected taxonomy:

First function:

$terms = get_the_terms(get_the_ID(), 'MY_TAXONOMY');
if (!is_wp_error($terms) && !empty($terms)) {
    foreach ($terms AS $term) {
        $name = $term->name;
        $link = add_query_arg('fwp_typ', FWP()->helper->safe_value($term->slug), 'https://www.MYWEBSITE.com/');
        echo "<a href='$link'>$name</a><br />";
    }
}       

Second function:

    global $post;
    $taxonomy = 'MY_TAXONOMY';
    $terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
        echo '<?ul>';
        $terms = trim( implode( ',', (array) $terms ), ' ,' );
        wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms );
        echo '<?/ul>';
    }

The first one ignores the hierarchy, but transforms the links as I need, i.e. so that they lead to queries of the WP's Facet Plugin. I understand that this line is key here:

$link = add_query_arg('fwp_typ', FWP()->helper->safe_value($term->slug), 'https://www.MYWEBSITE.com/');

The second one includes the hierarchy, but the links don't lead where I would like. How do I make this second function transform links like the first one?

2 Answers 2

1

This can be achieved multiple ways. One of them is to filter term link with your desired structure.

Following is a function that can convert a term link as per your structure. Ignore the first argument, it is there as we will be using this function as a filter callback.

/**
 * @param string  $termlink Term link.
 * @param WP_Term $term Term object.
 */
function wpse366737_pre_term_link( $termlink, $term ) {
    return add_query_arg(
        'fwp_typ', 
        FWP()->helper->safe_value( $term->slug ), 
       'https://www.MYWEBSITE.com/'
    );
}

Now, we have a function that we can use at appropriate place to modify term link. Once we are done, we will remove the filter so that other scripts using term link does not gets affected.

global $post;

$taxonomy = 'MY_TAXONOMY';

$terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );

if ( $terms ) {
    echo '<ul>';

    $terms = trim( implode( ',', (array) $terms ), ' ,' );

    // as we needed.
    add_filter( 'pre_term_link', 'wpse366737_pre_term_link', 10, 2 );

    // this list will display the filtered url for term.
    wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms );

    // remove the filter so other script doesn't get affected.
    remove_filter( 'pre_term_link', 'wpse366737_pre_term_link', 10 );

    echo '</ul>';
}
5
  • Thanks for your help. I'm very grateful. There is only a small problem with wpse366737_pre_term_link (). Look at the screenshot: srv19859.microhost.com.pl/2020-05-17_11h39_28.jpg The page address is duplicated. So I threw it out of the function. Everything is fine then, but there is a slash at the end of the URL that prevents a query. How to get rid of that slash? My page adress is: freuciv.com
    – reti
    May 17, 2020 at 10:05
  • And one more thing. Instead of 'fwp_type' there should be a variable. When I try to put it there, an error occurs.
    – reti
    May 17, 2020 at 11:01
  • Could you tell me what does prefix 'wpse366737_' mean?
    – reti
    May 17, 2020 at 20:15
  • If the address is coming wrong that indicates your url is missing protocol. Make sure it has http or https at the beginning. If using variable triggers an error, check that variable is available to use. wpse366737_ is just a unique prefix, don't mean anything specific.
    – Shazzad
    May 17, 2020 at 21:49
  • My page address has https and it looks like this variable can't be used. I have no idea how to fix it.
    – reti
    May 18, 2020 at 11:34
0

Solved ugly, but works. General form of the filter:

<?php
add_filter('term_link', function ($termlink, $term, $taxonomy) {
    if ('CPT-TAXONOMY-NAME' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . '?FACETWP-FACET-NAME=' . $term->slug;
    }
    return $termlink;
}, 10, 3);

The function in fuctions.php:

<?php
function list_hierarchical_terms($taxonomy) {
    global $post;
    $terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
        echo '<?ul>';
        $terms = trim( implode( ',', (array) $terms ), ' ,' );
        wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms );
        echo '<?/ul>';
    }
}

Now I don't know how to write it simpler. The functions for filter in functions.php:

<?php
function termlink_transformation_freuciv_post_type($termlink, $term, $taxonomy)
{
    if ('freuciv_post_type' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . '?fwp_typ=' . $term->slug;
    }
    return $termlink;
}

function termlink_transformation_category($termlink, $term, $taxonomy)
{
    if ('category' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . '?fwp_categories=' . $term->slug;
    }
    return $termlink;
}

function termlink_transformation_hierarchical_tags($termlink, $term, $taxonomy)
{
    if ('hierarchical_tags' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . '?fwp_tags=' . $term->slug;
    }
    return $termlink;
}

And in template-part:

<?php
add_filter('term_link', 'termlink_transformation_freuciv_post_type', 10, 3);
list_hierarchical_terms('freuciv_post_type', '<h5 style="margin-bottom: 5px">Typ: </h5>');
remove_filter( 'term_link', 'termlink_transformation_freuciv_post_type', 10 );

add_filter('term_link', 'termlink_transformation_category', 10, 3);
list_hierarchical_terms('category', '<h5 style="margin-bottom: 5px; margin-top: 10px;">Kategorien: </h5>');
remove_filter( 'term_link', 'termlink_transformation_category', 10 );

add_filter('term_link', 'termlink_transformation_hierarchical_tags', 10, 3);
list_hierarchical_terms('hierarchical_tags', '<h5 style="margin-bottom: 5px; margin-top: 10px;">Tags: </h5>');
remove_filter( 'term_link', 'termlink_transformation_hierarchical_tags', 10 );  

There's too much repetition here. I have no idea how to simplify it.

1
  • A colleague gave me this advice: "You can use a single term_link filter function and a switch php.net/manual/en/control-structures.switch.php or just a series of if else within the same function to check which tax is being used and adjust the output accordingly. Then he could have one add_filter, do all the list_hierarchical_terms functions, then remove_filter". When I have a free moment, I'll try to apply it.
    – reti
    May 21, 2020 at 8:11

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.