我的脑袋在这段代码上打转。我正在尝试向现有插件的快捷码添加功能,以便仅显示包含特定类别名称的特定自定义帖子类型。
到目前为止,我有:
public function shortcode_litters( $args ) {
$output = \'\';
$atts = shortcode_atts( array(
\'category\' => \'\'
), $atts );
$terms = get_terms(\'product_category\');
wp_reset_query();
$query_args = array(
\'post_type\' => \'litter\',
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'asc\',
\'numberposts\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_category\',
\'field\' => \'slug\',
\'terms\' => $atts,
),
),
);
$litters = get_posts($query_args);
if($litters) {
$columns = empty($args["columns"]) ? 2 : $args["columns"];
$output .= \'<div class="breedr"><ul class="columns \'.number_to_words($columns).\'">\';
foreach ( $litters as $litter ) {
$output .= $this->breedr_template( plugin_dir_path( dirname( __FILE__ ) ) . \'public/partials/list-single-litter.php\', $litter );
}
$output .= \'</ul></div>\';
}
else {
$output .= "<span>No litter found.</span>";
}
return $output;
}
最初是这样的:
public function shortcode_litters( $args ) {
$output = \'\';
$query_from_date = strtotime(date("Y-m-d", strtotime("-3 months")));
$query_args = array(
\'post_type\' => \'litter\',
\'post_status\' => \'publish\',
\'orderby\' => \'title\',
\'order\' => \'asc\',
\'numberposts\' => -1
);
if(!empty($args[\'show\']) && in_array($args[\'show\'], array(\'current\', \'past\'))) {
if ($args[\'show\'] == "current") {
$meta_query = array(
\'meta_query\' => array(
array(
\'key\' => \'_litter_mating\',
\'value\' => $query_from_date,
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
)
)
);
}
if ($args[\'show\'] == "past") {
$meta_query = array(
\'meta_query\' => array(
array(
\'key\' => \'_litter_mating\',
\'value\' => $query_from_date,
\'compare\' => \'<\',
\'type\' => \'NUMERIC\'
)
)
);
}
}
else {
$meta_query = array(\'meta_query\' => \'\');
}
$litters = get_posts($query_args + $meta_query);
if($litters) {
$columns = empty($args["columns"]) ? 2 : $args["columns"];
$output .= \'<div class="breedr"><ul class="columns \'.number_to_words($columns).\'">\';
foreach ( $litters as $litter ) {
$output .= $this->breedr_template( plugin_dir_path( dirname( __FILE__ ) ) . \'public/partials/list-single-litter.php\', $litter );
}
$output .= \'</ul></div>\';
}
else {
$output .= "<span>No litter found.</span>";
}
return $output;
}
从本质上讲,我将短代码从
[litters show=""]
到
[litters category=""]
- 因为我希望它通过快捷码中的类别名称(而不是编号)显示。最初,它有
show=""
(当前或过去)并确定其基于日期。我想通过快捷码选择一个类别名称,手动确定它是当前的还是过去的。
我整天都在想这个问题,现在我已经脑死亡了。任何帮助都将不胜感激!
最合适的回答,由SO网友:nmr 整理而成
在您的代码中$atts
始终是$atts = [ \'category\' => \'\' ]
, 您不能使用\'terms\' => $atts,
在查询参数中$query_args
.
[litters category="first_slug,second_slug"]
您可以分配给
\'terms\'
在里面
\'tax_query\'
参数字符串(单个类别)或数组(多个类别)。
$args[\'category\']
传递给shortcode函数
as string, 因此,您需要检查它是否是一个类别,并且不需要执行任何操作,或者是一个由逗号分隔的段塞列表,并且它们必须转换为一个数组。
public function shortcode_litters( $args ) {
$output = \'\';
//
// filter shortcode attributes
$args = shortcode_atts(
array( \'category\' => \'\', \'columns\' => \'\' ),
$args
);
if ( strpos( $args[\'category\'], \',\' ) !== false ) {
$terms = explode( \',\', $args[\'category\'] );
} else {
$terms = $args[\'category\'];
}
$query_args = array(
\'post_type\' => \'litter\',
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'asc\',
\'numberposts\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_category\',
\'field\' => \'slug\',
\'terms\' => $terms,
),
),
);