代码之家  ›  专栏  ›  技术社区  ›  Curtis W

具有短码属性的动态函数

  •  0
  • Curtis W  · 技术社区  · 7 年前

    我有一个下面的函数,它统计特定元键(在本例中是保修字段)设置为1并且具有特定分类法(选择Grimsby的经销商税)的帖子数量-我希望能够通过快捷码更改元键和分类法术语,以便我可以将该函数用于不同的字段。我查阅了法典并阅读了有关$ATT的内容,但有几次测试没有成功。任何建议都将不胜感激,谢谢!

        function counttheposts_func( $atts ) {
            $args = array(
                'posts_per_page'    => -1,
                'post_type'         => 'deal',
                'post_status'       => 'publish',
                'meta_key'          => 'wpcf-warranty',
                'meta_value'        => '1',
              'tax_query' => array(
                  'relation' => 'AND',
                  array(
                      'taxonomy' => 'dealership-tax',
                      'field' => 'slug',
                      'terms' => array('Grimsby'),
                  ),
              )
            );
    
            $posts_query = new WP_Query($args);
            $the_count = $posts_query->post_count;
    
            echo $the_count;
    
    
        }
    
        add_shortcode( 'counttheposts', 'counttheposts_func' );
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Mithc    7 年前

    您可以尝试以下操作(未测试的查询):

    function counttheposts_func( $atts ) {
        /**
         * Defaults
         *
         * @var array
         */
        $args = shortcode_atts( array(
            'posts_per_page' => -1,
            'post_type'      => 'deal',
            'post_status'    => 'publish',
            'meta_key'       => 'wpcf-warranty',
            'meta_value'     => '1',
            'tax_query'      => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'dealership-tax',
                    'field'    => 'slug',
                    'terms'    => array( 'grimsby' ), // The slug should be something like the result of sanitize_title().
                ),
            ),
        ), $atts );
    
        /**
         * If the shortcode contains the 'terms' attribute, then we should override
         * the 'tax_query' since we have to treat this differently.
         */
        if ( isset( $atts['terms'] ) && '' !== $atts['terms'] ) {
            /**
             * You can even try multiple term slugs separating them with ",". You'll
             * probably want to escape each term with sanitize_title() or something to
             * prevent empty results, since expects a slug, not a title.
             *
             * @var array
             */
            $terms = explode( ',', $atts['terms'] );
    
            $args['tax_query'] = array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'dealership-tax',
                    'field'    => 'slug',
                    'terms'    => $terms,
                ),
            );
        }
    
        $posts_query = new WP_Query( $args );
    
        return $posts_query->post_count;
    }
    

    测试的原始结果 $args 如果简称为like,则为变量 [counttheposts terms="term1,term2" meta_key="my-meta-key"]

    Array
    (
        [posts_per_page] => -1
        [post_type] => deal
        [post_status] => publish
        [meta_key] => my-meta-key
        [meta_value] => 1
        [tax_query] => Array
        (
            [relation] => AND
            [0] => Array
            (
                [taxonomy] => dealership-tax
                [field] => slug
                [terms] => Array
                (
                    [0] => term1
                    [1] => term2
                )
            )
        )
    )