I've inherited a WordPress Twig site and am trying to modify an existing shortcode that is used in pages to output blog posts for a blog page and for single category post pages.
These shortcodes do the obvious:
[blog_list category_exclude="in-the-news"] is used on the "Blog" page, excluding the In The News category with category_exclude.
[blog_list category="in-the-news"] is used on the In The News posts page for that one category with category.
I have added another category, i.e. videos, and this works for a video posts category page:
[blog_list category="videos"]
But what I need to do is exclude more than one category on the Blog page using category_exclude, like this:
[blog_list category_exclude="in-the-news videos"]
That doesn't work, and so I know I need to modify query_posts for the if loop below that determines the category to exclude. How to I make the category_exclude parameter use more than one argument?
This is the complete shortcode function:
add_shortcode('blog_list', 'blog_get_list');
function blog_get_list($params) {
global $paged;
$blog_posts = [];
if (!isset($paged) || !$paged){
$paged = 1;
}
$page_size = 20;
$context = Timber::get_context();
if (!empty($params['page_size'])) {
$page_size = $params['page_size'];
}
$args = array(
'post_type' => 'post',
'posts_per_page' => $page_size,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
if (!empty($params['category'])) {
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'terms' => explode(',', $params['category']),
'field' => 'slug',
'operator' => 'IN',
),
);
}
if (!empty($params['category_exclude'])) { // Exclude categories
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'terms' => explode(',', $params['category_exclude']),
'field' => 'slug',
'operator' => 'NOT IN',
),
);
}
query_posts($args);
$posts = Timber::get_posts();
foreach ($posts as $p) {
$blog_posts[] = acco_blog_get_single($p);
}
$context['blog_posts'] = $blog_posts;
$context['pagination'] = Timber::get_pagination();
return Timber::compile('blog/index.twig', $context);
}
function acco_blog_get_single($post) {
$blog_post = [
'id' => $post->ID,
'link' => $post->link,
'title' => $post->title(),
'author_name' => $post->author_name,
'date' => $post->post_date,
'summary' => $post->get_field('summary'),
'body' => $post->get_field('body')
];
$feature_image = $post->get_image('feature_image');
if ($feature_image->ID){
$blog_post['feature_image'] = $feature_image;
}
return $blog_post;
}