• Resolved Sun

    (@sunrv)


    Hi,

    I need to filter on taxonomy parents in the following way:

    [pods name="taxonomy_name" template="Template name" limit="-1" where="taxonomy_name.parent.name='{@taxonomy_name.parent.name}'"]

    I want to show only taxonomies that fall under a specific taxonomy parent. Say I have the following; Parent A and Parent B.

    Parent A has child taxonomy Tax-1
    Parent B has child taxonomies Tax-2 and Tax-3

    On a custom pods post type that is connected to Tax-2, I want to show a global pod template – which can use all these taxonomies as navigation – to show all taxonomies under Parent B since Tax-2 falls under this parent, but not Parent A. So I want to use a single reusable shortcode to automatically filter out that if the page is under Tax-2, show all children of Parent B and leave out Tax-1.

    Is this possible, is it not, or am I missing anything?

    Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support pdclark

    (@pdclark)

    There are several logical steps in the request, so defining a new function is necessary.

    When activated, the shortcode plugin defined below will accept any of the following formats if the taxonomy is named taxonomy_name:

    • [terms-with-same-parent] to output a default template for taxonomy taxonomy_name
    • [terms-with-same-parent template="Taxonomy Terms"] if there is a Pods template of that name
    • [terms-with-same-parent taxonomy="taxonomy_name"] if there is a taxonomy of that name

    or…

    [terms-with-same-parent]<a href="{@permalink}">{@name}</a>[/terms-with-same-parent] 

    …to define a Pods template inline.

    The below shortcode can be added as a plugin to wp-content/plugins/terms-with-same-parent/terms-with-same-parent.php

    Please see comments in the code for options to add headers, restrict to one parent, or wrap in tags.

    <?php

    /**
    * Plugin Name: Shortcode - Terms with Same Parent
    * Description: <code>[terms-with-same-parent]</code>
    */
    add_shortcode(
    'terms-with-same-parent',
    function( $atts, $content, $tag ) {
    // Query taxonomy set as taxonomy="taxonomy_name"
    // or use default of 'taxonomy_name' if not specified.
    $taxonomy = 'taxonomy_name';
    if ( array_key_exists( 'taxonomy', $atts ) ) {
    $taxonomy = $atts['taxonomy'];
    }

    // Assigned terms.
    $terms = (array) get_the_terms( get_the_ID(), $taxonomy );
    if ( empty( $terms ) ) {
    return '';
    }

    ob_start();

    // Assigned term and parent IDs.
    $parent_ids = [];
    $assigned_child_ids = [];
    foreach ( $terms as $term ) {
    if ( 0 !== intval( $term->parent ) ) {
    $parent_ids[] = $term->parent;
    $assigned_child_ids[] = $term->term_id;
    }
    }
    $parent_ids = array_unique( $parent_ids );

    // Terms with the same parent.
    // See https://developer.ww.wp.xz.cn/reference/classes/wp_term_query/__construct/
    foreach( (array) $parent_ids as $parent_id ) {
    $terms_with_same_parent = get_terms(
    [
    'taxonomy' => $taxonomy,
    'parent' => $parent_id, // Must be one parent ID.
    'hide_empty' => true,
    'orderby' => 'name',
    'order' => 'ASC',
    'exclude' => $assigned_child_ids,
    'number' => 0,
    ]
    );

    if ( ! empty( $terms_with_same_parent ) ) {
    // Uncomment to show name of parent.
    // printf( '<h3>%s</h3>', get_term( $parent_id )->name );

    // Uncomment to wrap terms in a <p> tag.
    // echo '<p>';
    }

    foreach( (array) $terms_with_same_parent as $term ) {
    if ( array_key_exists( 'template', $atts ) ) {
    // If a Pods template is specified by template="Template Name".
    echo pods( $taxonomy, $term->term_id )->template( $atts['template'] );
    } else if ( ! empty( $content ) ) {
    // If a template is specified in shortcode content like:
    // [terms-with-same-parent]<a href="{@permalink}">{@name}</a>[/terms-with-same-parent]
    echo pods( $taxonomy, $term->term_id )->template( null, $content );
    } else {
    // If no template specified, default to links.
    printf(
    '<a href="%s">%s</a> ',
    get_term_link( $term ),
    $term->name
    );
    }
    }

    if ( ! empty( $terms_with_same_parent ) ) {
    // Uncomment if wrapped terms in a <p> tag.
    // echo '</p>';
    }

    // Uncomment to only show children of the first found parent.
    // break;
    }

    return ob_get_clean();
    }
    );

    Note the only thing in this code specific to Pods is the use of magic tag templates. An example of a generic template is used for the default template and parent term header.

    Thread Starter Sun

    (@sunrv)

    Hi Paul,

    Thanks for developing the custom plugin! I’ve added it to my wp-content map, edited the PHP template to fit my desired taxonomy names and activated the plugin. I edited the template at the following areas:

    • $taxonomy = ‘hoofdnavigatie’;
    • $parent_ids = [14];
    • $assigned_child_ids = [13];

    I’ve tried the following shortcode in my template, but it doesn’t seem to get any results. (Shortcodes are allowed in my Pods Templates through a line in the wp-config.php)

    [terms-with-same-parent taxonomy="{@hoofdnavigatie.parent.name}" template="Book tabs - List template - Depth version"]

    I’ve also tried the following way:

    [terms-with-same-parent]<a href="{@permalink}">{@name}</a>[/terms-with-same-parent] 

    But this didn’t work for me either.

    I’m inexperienced with PHP so I could be doing something wrong or missing something, can you please help me out?

    If you need more information, feel free to let me know!

    Plugin Support pdclark

    (@pdclark)

    I’m not sure what you mean by wp-content map.

    Is the plugin installed to wp-content/plugins/terms-with-same-parent/terms-with-same-parent.php ?

    Was the plugin activated under WP Admin > Plugins?

    Do other shortcodes run in the template?

    Does the shortcode output blank, or not get interpreted at all?

    If blank, consider whether the query in the shortcode is returning any posts — for example, is get_the_ID() returning the ID of the post that’s expected to have the terms associated? Are any terms being returned? Are posts assigned to those terms?

    Between ob_start() and ob_get_clean(), var_dump( $variable_name ); can be used to output the content of any of the variables, such as $terms or get_the_ID()

    Thread Starter Sun

    (@sunrv)

    Hi Paul,

    Apologies for the confusion, the plugin is installed to wp-content/plugins/terms-with-same-parent/terms-with-same-parent.php and has been activated in the WP Admin > Plugins.

    There are 4 other shortcodes running in the template under conditional logic [if][else][/if] as well as [each][/each].

    The shortcode output is blank. I’m not sure how to check if the get_the_ID() is returning the proper ID, but when uncommenting // printf( '<h3>%s</h3>', get_term( $parent_id )->name ); it does return the parent taxonomy name as intended, and there is a post assigned to the term.

    I’ve also looked into ob_start(), ob_get_clean() and var_dump( $variable_name );, but lack the PHP knowledge to properly apply them in the plugin file. Can you please help with that?

    Thanks in advance!

    Plugin Support pdclark

    (@pdclark)

    It’s difficult to say without seeing your complete template or how it is referenced.

    Please try breaking down your configuration into smaller steps:

    • Did you try using the shortcode on a post assigned to the hoofdnavigatie taxonomy, without any additional templates or page builders?
    • If using shortcodes in a Pods template, did you set define( 'PODS_SHORTCODE_ALLOW_SUB_SHORTCODES', true ); in wp-config.php as on https://docs.pods.io/displaying-pods/pods-templates/using-shortcodes-in-pods-templates/ ?
    • Did you try walking through the provided code and debug functions with any language models, which would be able to provide feedback on what var_dump() does, and how it relates to checking the values expected according to your configuration each step of the way?

    Beyond that, the key information for achieving the functionality described is that get_the_terms() will provide terms associated with any given ID, which contain their parent IDs, and get_terms() will provide terms within a given parent.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Shortcode where filter for taxonomy parent’ is closed to new replies.