【问题标题】:Filters post by subcategory slug using buttons使用按钮按子类别 slug 过滤帖子
【发布时间】:2020-11-10 03:33:59
【问题描述】:

我现在加载一个类别的子页面帖子,现在我尝试按他的子类别过滤此帖子,并在单击按钮后获取指向 mypage.com/subpage/?slug=fish 之类的页面的链接,仅查看鱼帖子。

我应该使用 $_GET 吗?

有人可以帮忙吗?

有我的代码从动物类别中加载我的所有帖子

  <?php

        $args = array(
            'cat' => 50,
            'posts_per_page' => '999',
            'order' => 'ASC',
            'orderby' => 'menu_order',
        );
        $the_query = new WP_Query($args);
        // The Loop
        if ($the_query->have_posts()) {
            ?>

            <?php

            while ($the_query->have_posts()) {
                $the_query->the_post();

                get_template_part( 'templates/content/content', 'animals' ); 

            }
            /* Restore original Post Data */
            wp_reset_postdata();
        } else {
            // no posts found
            echo '';
        }
    ?>
    

【问题讨论】:

    标签: php html jquery wordpress filter


    【解决方案1】:

    你可以这样使用:

    if(isset($_GET['slug']))
    {
    $args = array(
        'post_type' => 'post_type',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $_GET['slug']
            )
         )
    );
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-23
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2014-12-12
      • 2019-11-24
      相关资源
      最近更新 更多