繁体   English   中英

使用Twig和WordPress查询类别帖子

[英]Query category posts with Twig and WordPress

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

这些短代码显而易见:

[blog_list category_exclude="in-the-news"]用于“博客”页面,但不包括“新闻类别”中的category_exclude

[blog_list category="in-the-news"]在“新闻发布”页面上用于类别为的一个category

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

[blog_list category="videos"]

但是我需要做的是使用category_exclude在Blog页面上排除多个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;
}

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

[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',
        ),
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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