使用Twig和WordPress查询类别帖子

时间:2018-03-05 18:30:11

标签: php wordpress twig

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

这些短代码显而易见:

[blog_list category_exclude="in-the-news"]用于" Blog"页面,不包括category_exclude的新闻类别。

[blog_list category="in-the-news"]用于具有category的该类别的新闻帖子页面。

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

[blog_list category="videos"]

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

[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;
}

1 个答案:

答案 0 :(得分: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',
        ),
    );
}