简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM