具有短代码属性的动态函数
Dynamic function with attributes in shortcode
我有以下功能,它计算具有特定元键(在本例中为保修字段)设置为 1 并且具有特定分类法(选择 Grimsby 的经销商税)的帖子数量 - 我想要的能够做的是通过短代码更改元键和分类术语,这样我就可以为不同的领域重用该功能。我查看了 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' );
您可以尝试这样的操作(查询未测试):
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;
}
如果像 [counttheposts terms="term1,term2" meta_key="my-meta-key"]
那样调用短代码,$args
变量的原始结果是:
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
)
)
)
)
我有以下功能,它计算具有特定元键(在本例中为保修字段)设置为 1 并且具有特定分类法(选择 Grimsby 的经销商税)的帖子数量 - 我想要的能够做的是通过短代码更改元键和分类术语,这样我就可以为不同的领域重用该功能。我查看了 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' );
您可以尝试这样的操作(查询未测试):
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;
}
如果像 [counttheposts terms="term1,term2" meta_key="my-meta-key"]
那样调用短代码,$args
变量的原始结果是:
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
)
)
)
)