• Hello,

    I’ve got a page that determines the name of the child category that the current post belongs to:

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(3, $childcat)) {
    echo $childcat->cat_name ;
    }}
    ?>

    Below this I’d like to show all other posts that also belong to this current child category.

    <?php global $post;
    $args = array('category_name' => $childcat, 'posts_per_page' => -1, 'category__not_in' => 3,);
    $custom_posts = get_posts($args);
    foreach($custom_posts as $post) : setup_postdata($post); ?>

    However, the category_name' => $childcat bit fails. If I type in the name or ID of the child category directly it works, but it needs to be a variable. What am I missing? Been Googling for about two hours trying to fix this. Not that well versed with WP it must be said.

Viewing 8 replies - 1 through 8 (of 8 total)
  • If you have multiple loops (make sure you reset the query)

    https://codex.www.ads-software.com/Function_Reference/wp_reset_query

    Thread Starter Rimfya

    (@rimfya)

    Yep I’ve got a reset directly above this.

    The first one echo’s the child-cat.
    What does the second one output?

    Thread Starter Rimfya

    (@rimfya)

    It *should* output a bunch of posts belonging to that same child category. Instead it outputs every single post ever.

    But if I replace $childcat in the second one with the name or ID of the child category that is being echo’d above, it works perfectly. Just can’t get that variable to work in the second query.

    example:

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(3, $childcat)) {
    echo $childcat->cat_name ;
    $child_cat = $childcat->slug;
    }}
    ?>
    <?php global $post;
    $args = array('category_name' => $child_cat, 'posts_per_page' => -1, 'category__not_in' => 3,);
    $custom_posts = get_posts($args);
    foreach($custom_posts as $post) : setup_postdata($post); ?>
    Thread Starter Rimfya

    (@rimfya)

    Genius it works!

    So … what’s different? We’re making the variable in the array the Category Slug, not the Category Name?

    what’s different?

    the suggested code sets a variable $child_cat with the category slug; before, $childcat was only a loop variable, and not accessible outside of the ‘foreach’ loop.

    also, $cat->name is the category title (example: This Category), while $cat->slug is the category slug (example: this-category); (see for instance here https://codex.www.ads-software.com/Function_Reference/get_category )

    the query parameter needs the category slug; https://codex.www.ads-software.com/Class_Reference/WP_Query#Category_Parameters

    I’m using the above code. Made some changes in order to have a separator.

    Would like to insert the child categories into the text of a post by using a shortcode [region]; such as

    This activity is possible in [region].

    This is the function I tried to make. It works but it shows the childcategories above the post and not inside the text of the post.

    function region($atts) {
    $sep = '';
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(27, $childcat)) {
    echo $sep.'',$childcat->cat_name,'';
    $sep = ' / ';
    }}}
    
    add_shortcode('region', 'region');

    Hope to find some help here

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Use Variable for category_name’ is closed to new replies.