0
要顯示所有類別與自定義分類複選框名字叫ct_category
我有以下代碼:動態tax_query方面
$terms = get_terms('ct_category');
foreach ($terms as $term) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
}
我想從檢查類別只顯示內容。我試過以下沒有工作:
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => $ct_category,
'field' => 'term_id',
'terms' => $_POST['taxonomy_category']
)
)
);
$loop = new WP_Query($args);
我猜可能是問題在'terms' => $_POST['taxonomy_category']
。如果名稱屬性taxonomy_category['.$term->term_id.']
可以顯示爲'terms' =>
中的數組,則問題將被修復。花了大量的時間搜索谷歌,但無法找到任何解決方案。
下面是完整的代碼
<?php
function add_meta_box() {
add_meta_box('ct_metabox', 'CT', 'meta_box_content_output', 'cpt', 'normal');
}
add_action('add_meta_boxes', 'add_meta_box');
function meta_box_content_output ($post) {
wp_nonce_field('save_meta_box_data', 'meta_box_nonce');
$taxonomy_category = get_post_meta($post->ID, 'taxonomy_category', true);
function categories_checkbox(){
$terms = get_terms('ct_category');
foreach ($terms as $term) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
}
}
<?
<ul>
<li>
<?php categories_checkbox() ?>
<li>
</ul>
<?php
}
function save_meta_box_data($post_id) {
// Check if our nonce is set.
if (! isset($_POST['meta_box_nonce'])) {
return;
}
// Verify that the nonce is valid.
if (! wp_verify_nonce($_POST['meta_box_nonce'], 'save_meta_box_data')) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check the user's permissions.
if (isset($_POST['post_type']) && 'page' == $_POST['post_type']) {
if (! current_user_can('edit_page', $post_id)) {
return;
}
} else {
if (! current_user_can('edit_post', $post_id)) {
return;
}
}
$taxonomy_category_value = "";
if(isset($_POST["logo_taxonomy_category"])) {
$taxonomy_category_value = sanitize_text_field($_POST["logo_taxonomy_category"]);
}
update_post_meta($post_id, "logo_taxonomy_category", $taxonomy_category_value);
}
add_action('save_post', 'save_meta_box_data');
?>
<?php
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => $ct_category,
'field' => 'term_id',
'terms' => $taxonomy_category
)
)
);
$loop = new WP_Query($args);
?>
感謝您的回覆普拉卡什。不幸的是你的解決方案沒有奏效我剛剛添加了完整的代碼,可以幫助您更好地理解。 – Babu