最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Hierarchical taxonomy list with modificated term links

programmeradmin1浏览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), '/');
        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), '/');

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?

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/');
        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/');

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?

Share Improve this question edited May 20, 2020 at 9:12 reti asked May 15, 2020 at 21:22 retireti 276 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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/'
    );
}

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>';
}

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.

发布评论

评论列表(0)

  1. 暂无评论