美文网首页
开发主题时写的一些代码

开发主题时写的一些代码

作者: 小星star | 来源:发表于2020-05-24 12:15 被阅读0次

    Portfolio Filter

    <!-- Portfolio Filter
    ============================================= -->
    <ul class="portfolio-filter clearfix" data-container="#portfolio">
    
        <li class="activeFilter"><a href="#" data-filter="*">Show All</a></li>
    
        <?php
    
        // wp_list_categories( array(
        //  'show_option_all'     => 'Show All',
        //  'taxonomy'            => 'product_category',
        //  'hide_empty'          => 0,
        //  'title_li'            => '',
        //  'walker'              => '',
        // ) );
    
        $args=array(
            'type'                  => 'product',
            'taxonomy'              => 'product_category',
            'hide_empty'            => 0,
        );
        $categories = get_categories($args);
        foreach($categories as $category) {
            echo '<li>';
            echo '<a href="' . get_category_link( $category->term_id ) . '" data-filter="' . sprintf( __( ".%s" ), $category->slug ) . '" ' . '>' . $category->name.' </a>';
            echo '</li>';
        }
        ?>
    </ul><!-- #portfolio-filter end -->
    
    <?php
    $args = array(
        'post_type' => 'product',                       //自定义文章类型名称
        'showposts'=> 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_category',
                'field' => 'slug',                      //(string) - Select taxonomy term by ('id' or 'slug')
                'terms' => array( $cat_slug ),          //(int/string/array) - Taxonomy term(s).
            )
        )
        );
    $my_query = new WP_Query($args);
    if ( $my_query->have_posts() ) {
    
        while ( $my_query->have_posts() ) {
            $my_query->the_post();
    
            get_template_part( 'template-parts/content', 'portfoliobrif' );
        }
    }
    wp_reset_query(); //重置 query 查询
    ?>
    
        <?php
    
        $terms = wp_get_object_terms( $post->ID, 'product_category' );
        foreach( $terms as $term )
            $term_slugs[] = $term->slug;
    
        // echo $custom_cat =  implode( '', $term_slugs );
        // var_dump($term_slugs);
        $args = array(
            'post_type' => 'product', //自定义文章类型名称
            'showposts' => 10, //输出的文章数量,这个可以是缺省值,不用设置
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_category',//自定义分类法名称
                    'field'    => 'slug',
                    'terms' => $term_slugs, //id 为 64 的分类。也可是多个分类 array(12,64)
                    ),
                )
            );
        $my_query = new WP_Query($args);
    

    相关文章

      网友评论

          本文标题:开发主题时写的一些代码

          本文链接:https://www.haomeiwen.com/subject/jqpdahtx.html