使用Twig和WordPress查询类别帖子

问题描述 投票:0回答:1

我继承了一个WordPress Twig网站,我正在尝试修改页面中使用的现有短代码,以输出博客页面和单个类别帖子页面的博客帖子。

这些短代码显而易见:

[blog_list category_exclude="in-the-news"]用于“博客”页面,不包括使用category_exclude的In The News类别。

[blog_list category="in-the-news"]用于category的那个类别的In The News帖子页面。

我添加了另一个类别,即视频,这适用于视频帖子类别页面:

[blog_list category="videos"]

但我需要做的是使用category_exclude在Blog页面上排除多个类别,如下所示:

[blog_list category_exclude="in-the-news videos"]

这不起作用,所以我知道我需要为下面的if循环修改query_posts,以确定要排除的类别。如何让category_exclude参数使用多个参数?

这是完整的短代码功能:

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;
}
php wordpress twig
1个回答
2
投票

根据上面的代码,您只需要指定要排除的逗号分隔的类别列表:

[blog_list category_exclude="in-the-news videos"]

应该改为

[blog_list category_exclude="in-the-news,videos"]

这是因为在上面的代码中,它将category_exclude字符串分解为数组,在找到任何逗号的地方进行拆分:

if (!empty($params['category_exclude'])) {  // Exclude categories
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'terms' => explode(',', $params['category_exclude']), // "cat1,cat2" would become ["cat1","cat2"] here
            'field' => 'slug',
            'operator' => 'NOT IN',
        ),
    );
}
© www.soinside.com 2019 - 2024. All rights reserved.