Wordpress seo silo category term-tree for sidebar

0 votes

I created a sidebar for the dynamic WordPress category pages.

Example Url: https://example.com/category/firstcategory/secondcategory/othercategories/

SEO Silo:

Info about SEO Silos: What is a content silo and how does it benefit for SEO?

An imaginary company offers books, films and toys. And so the following categories are usually formed:

Company category:

* company
 ** books
 ** movies
 ** games

They have subcategories.

* company
 ** books
   *** drama
   *** comedy
 ** movies
   *** drama
   *** comedy
   *** romance
 ** games
   *** actions
   *** for kids
   *** click and point

The subcategories in turn have entries.

If I now come to the books category, only everything about books may appear in the category tree and not movies and games as well. Ideally not even the upper category, like that:

* company
 ** books
   *** drama
   ---- drama Book1
   ---- drama Book2

If I'm in the drama category for books, I dont want to see the others like comedy in the books category too.

If I'm in the Company category, its okay to see the first subcategories like:

* company
 ** books
 ** movies
 ** games

If I'm in the books category:

* company
 ** books
   *** drama
   *** comedy

Problem:

The code isn't optimized yet, because I'm just trying out a lot.

Active Category level 2 already works (apart from the indentation).

  1. When calling up the second category, sometimes not all (existing) sub-categories appear.
  2. Probably the smallest problem is that the categories are not indented as a list.
// Manual: https://stackoverflow.com/questions/9225920/how-to-check-which-level-category-it-is-for-wordpress
function get_the_level($id, $type = 'category') {
    return count(get_ancestors($id, $type));
}

function children_sidebar_shortcode($atts) {

    global $post;

    if (!is_category()) {
        return false;
    }

    $returnval = "";
    $category = get_queried_object();
    $category_id = $category->term_id;

    $n = 0;
    $item_cat_level = 0;

    // Active Category is level 2 and more
    if (get_the_level($category_id) >= 2) {

        $returnval .= "<h2>Active Category is level 2 and more</h2>";

        $args = array(
            'type'                     => 'post',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'term_group', //'name',
            'order'                    => 'ASC',
            'hide_empty'               => 0,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'category',
            'pad_counts'               => false
        );
    } else {

        $returnval .= "<h2>Active Category is level 1 or less</h2>";

        $args = array(

            'type'                     => 'post',
            'child_of'                 => $category_id,
            'parent'                   => '', // set here same category as you want to fetch only their 1st level category  on in depth child
            'orderby'                  => 'term_group', //'name',
            'order'                    => 'ASC',
            'hide_empty'               => 0,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'category',
            'pad_counts'               => false
        );
    }

    $categories = get_categories($args);

    if (count($categories) > 0) {

        // show the category description
        //$returnval .= category_description();

        $returnval .= "<ul>";
        foreach ($categories as $category) {
            $item_cat_level = get_the_level($category->term_id);

            // Active Category is level 2 and more
            if (get_the_level($category_id) >= 2) {

                if (
                    // Category is part of the parent Categories/Terms
                    cat_is_ancestor_of($category->term_id, $category_id)

                    // SubLevel Category 2
                    || $item_cat_level >= 2

                    // Active Category (same)
                    || $category->term_id === $category_id
                ) {
                    $category_link = sprintf(
                        '<a href="%1$s" alt="%2$s">%3$s</a>',
                        esc_url(get_category_link($category->term_id)),
                        esc_attr($category->name),
                        esc_html($category->name)
                    );

                    $returnval .= sprintf("<li class='cat-item cat-item-%d %s'>%s", $category_id, ($category->term_id === $category_id) ? 'current-cat-ancestor' : '', $category_link);
                }
            } else {

                if (
                    // Category is part of the parent Categories/Terms
                    //cat_is_ancestor_of($category->term_id, $category_id)

                    // SubLevel Category 2
                    //|| 
                    $item_cat_level <= 1

                    // Active Category (same)
                    //|| $category->term_id === $category_id
                ) {
                    $category_link = sprintf(
                        '<a href="%1$s" alt="%2$s">%3$s</a>',
                        esc_url(get_category_link($category->term_id)),
                        esc_attr($category->name),
                        esc_html($category->name)
                    );

                    $returnval .= sprintf("<li class='cat-item cat-item-%d %s'>%s", $category_id, ($category->term_id === $category_id) ? 'current-cat-ancestor' : '', $category_link);
                }
            }
        }
        $returnval .= "</ul>";
    }

    // Attributes
    $atts = shortcode_atts(
        array(
            'name' => '',
        ),
        $atts,
        ''
    );

    return $returnval;
}
add_shortcode('children_sidebar', 'children_sidebar_shortcode');
Feb 25, 2022 in Others by Kichu
• 19,050 points
622 views

1 answer to this question.

0 votes
function children_sidebar_shortcode( $atts ) {
    // Do nothing if we're not on a category archive.
    if ( ! is_category() ) {
        return '';
    }

    // Parse the shortcode's parameters.
    $atts = shortcode_atts( array(
        'show_parent'  => 1,
    ), $atts );

    // Get the queried category object and ID.
    $current_cat    = get_queried_object();
    $current_cat_id = $current_cat->term_id;

    // Get the category's immediate children, if any.
    $parent_cat_id  = $current_cat_id;
    $categories     = get_categories( array(
        'parent'     => $parent_cat_id,
        'hide_empty' => 1,
    ) );

    // If none, get its siblings.
    // .. which means it's the last level.
    if ( empty( $categories ) ) {
        $parent_cat_id = $current_cat->parent;
        $categories    = get_categories( array(
            'parent'     => $parent_cat_id,
            'hide_empty' => 1,
        ) );
    }

    $heading = '';
    $list    = '<ul class="cat-list">';

    foreach ( $categories as $term ) {
        $class = 'cat-item';

        // If it's the current category, show the name only.
        if ( $current_cat_id === $term->term_id ) {
            $class .= ' current-cat';

            $cat_name = '<span>' . esc_html( $term->name ) . '</span>';
        // If it's not the current category, add a link to view the category
        // archive.
        } else {
            $cat_name = '<a href="' . esc_url( get_category_link( $term ) ) . '">' .
                esc_html( $term->name ) . '</a>';
        }

        $list .= "<li class='$class'>$cat_name</li>";
    }

    $list .= '</ul>';

    if ( $parent_cat_id && $atts['show_parent'] ) {
        $term = get_category( $parent_cat_id );

        $heading = '<h3>' . esc_html( $term->name ) . '</h3>';
    }

    return "$heading $list";
}


if you are looking for a category tree with only siblings then tis is the answer 
and if you want to have full control over html markup replace the entire forarch with this code 
$list .= wp_list_categories( array(
    'current_category' => $current_cat_id,
    'parent'           => $parent_cat_id,
    'hide_empty'       => 1,
    'title_li'         => '',
    'echo'             => 0,
) );
answered Feb 26, 2022 by narikkadan
• 63,420 points

Related Questions In Others

0 votes
0 answers

Wafcoin: Legalization is the cornerstone for long-term development

Wafcoin token trading platform has always insisted ...READ MORE

Jan 29, 2019 in Others by anonymous
384 views
0 votes
1 answer

Yoast SEO (WordPress Plugin) - Get plugin generated data manually

get_post_meta(), use this to get the meta ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,420 points
1,245 views
0 votes
1 answer

How to add meta keyword in wordpress using yoast seo plugin.?

if you dont want to use code ...READ MORE

answered Feb 12, 2022 in Others by narikkadan
• 63,420 points
749 views
0 votes
2 answers

What is most SEO optimized image HTML code for?

What is most SEO optimized image HTML ...READ MORE

answered Feb 12, 2022 in Others by gdxxfgoidt
1,509 views
0 votes
1 answer

Simple question regarding PHP, Wordpress & Yoast SEO

the way the search engine displays your ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,420 points
317 views
0 votes
1 answer

How to Remove canonical tag added by Yoast SEO plugin

first  Log in to your WordPress website ...READ MORE

answered Feb 28, 2022 in Others by narikkadan
• 63,420 points
781 views
0 votes
1 answer

adding meta keyword in wordpress using yoast seo plugin.

You can use Advance Custom Fields plugin ...READ MORE

answered Feb 28, 2022 in Others by narikkadan
• 63,420 points
1,379 views
0 votes
1 answer

Yoast SEO remove og description and twitter description on certain page

try this out it wil help ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,420 points
403 views
0 votes
1 answer

Limit the size of the existing and new permalink slugs in Wordpress for SEO

?php   /**  * Trim native slugs  */ function pm_trim_native_slug($slug, $post_ID, ...READ MORE

answered Feb 25, 2022 in Others by narikkadan
• 63,420 points
1,561 views
0 votes
1 answer

How to Access Yoast SEO tags in Wordpress API Callback

use rest api to request for the ...READ MORE

answered Feb 12, 2022 in Others by narikkadan
• 63,420 points
1,543 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP