具有短代码

时间:2017-08-20 15:23:04

标签: php wordpress function shortcode

我有以下功能,它将具有特定元键的帖子数量(在此示例中为保修字段)设置为1并且具有特定分类(选择了格里姆斯比的经销商税) - 我想要的是什么能够做的是通过短代码更改元键和分类术语,以便我可以将该函数重用于不同的字段。我已经查看过codex并阅读了关于$ atts但是有一些测试没有成功。任何建议都将不胜感激,谢谢!

    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 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情(查询未经测试):

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那样调用短代码,[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
            )
        )
    )
)