blackhill24
blackhill24

Reputation: 452

Wordpress loop using two variables in array with category_name

I need some help working out where my formatting is wrong.

I have this code at the start of my page template;

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
            $parent = get_term($term->parent, get_query_var('taxonomy') );?>

                <?php echo do_shortcode("[ecs-list-events message='' cat='{$term->slug}']"); ?>
                <?php echo do_shortcode("[ecs-list-events message='' cat='{$parent->slug}']"); ?> 

Im now trying to use these variables "$term->slug" and "$parent->slug" in a loop which is currently, im pretty sure everything in the loop is as it should be as the first variable works but the second is ignored.

<?php

// WP_Query arguments
$args = array (
    'category_name' => array ("$parent->slug", "$term->slug"),
);

// The Query
$welcome_text = new WP_Query( $args );

// The Loop
if ( $welcome_text->have_posts() ) {
    while ( $welcome_text->have_posts() ) {
        $welcome_text->the_post();

        the_content();
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();


?>

When i try to format using both i getthe below i get an error like;

Warning: urlencode() expects parameter 1 to be string, array given in /var/sites/s/silverfx.co.uk/public_html/wp-includes/formatting.php  

any ideas on where im going wrong?

Thanks

[EDIT]

I have just discovered that category_name only accepts strings so i guess the questions is now why does it ready the first variable and not the second?

it doesnt matter which way round they are ordered, the first always works and the second is ignored.

Current formatting is;

'category_name' => $parent->slug, $term->slug);

Upvotes: 0

Views: 913

Answers (1)

Stubbies
Stubbies

Reputation: 3124

It only accepts string separated by , or +.

Your args should be:

$cats = [];
$cats[] = $parent->slug;
$cats[] = $term->slug;

'category_name' => implode(",", $cats));

Or

'category_name' => $parent->slug. '+'. $term->slug);

You can read more about it here.

Upvotes: 1

Related Questions