我正在为我的wordpress开发自己的插件。我使用自定义分类法和自定义单个CPT为插件和CPT创建了一个短代码。
当我尝试在短代码中使用函数get\\u template\\u part()时,结果是调用单个。主题中的php模板。如何限制get\\u template\\u part查找文件的位置?
这是我的短代码:
function shortcode_display_products ( $atts ){
global $wpdb;
$query = \'\';
$atts = shortcode_atts( array(
\'categories\' => \'\'
), $atts, \'shortcode_display_products\');
if( !empty($atts[\'categories\']) && !is_null($atts[\'categories\']) ){
$query = new WP_Query( array(
\'post_type\' => \'products\',
\'post_status\' => \'public\',
\'tax_query\' => array(
array(
\'taxonomy\'=> \'groups\',
\'field\' => \'slug\',
\'terms\' => \'"\'.$atts[\'categories\'].\'"\'
)
),
\'showposts\' => -1,
\'orderby\' => \'date\',
\'order\' => \'DESC\'
));
}
else{
$query = new WP_Query( array(
\'post_type\' => \'products\',
\'post_status\' => \'public\',
\'showposts\' => -1,
\'orderby\' => \'date\',
\'order\' => \'DESC\'
));
}
ob_start();
if( $query->have_posts() ){
$c = 0;
while ( $query -> have_posts() ){
$query -> the_post();
get_template_part(\'single\',\'products\');
$c++;
}
}
else{
return esc_html__(\'Not found any product register\',P_TEXTDOMAIN);
}
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode(\'display_products\',\'shortcode_display_products\');
这是我的单一产品:
if ( !is_single() ){
include \'/form/products-form.php\';
$idnumber = ( is_single() )? \'\': \'-\'.$c;
?>
<div class="card">
<div class="card-header" id="heading<?= $idnumber?>">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapse<?= $idnumber?>" aria-expanded="true" aria-controls="collapse<?= $idnumber?>">
<?= $post->title ?>
</button>
</h5>
</div>
<div id="collapse<?= $idnumber?>" class="collapse show" aria-labelledby="heading<?= $idnumber?>" data-parent="#accordion">
<div class="card-body">
<?= $post->content ?>
<?php create_product_form() ?>
</div>
</div>
</div>
<?php }
else{ _e(\'nothing else\',P_TEXTDOMAIN);}
}