首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态tax_query术语

动态tax_query术语
EN

Stack Overflow用户
提问于 2016-03-06 12:00:58
回答 1查看 1.3K关注 0票数 0

要显示来自名为ct_category的自定义分类法的所有类别名称和复选框,我有以下代码:

代码语言:javascript
复制
$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>';          
}                                             

我想只显示选中类别的内容。我试着追随,但没有起作用:

代码语言:javascript
复制
$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' =>中显示为数组,则该问题将得到解决。我花了很多时间在谷歌上搜索,但找不到任何解决方案。

下面是完整的代码

代码语言:javascript
复制
<?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 );
?>
EN

回答 1

Stack Overflow用户

发布于 2016-03-06 16:12:47

按如下方式打印复选框

代码语言:javascript
复制
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category[]" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';    

这将收集数组$_POST['taxonomy_category']中的所有in

然后编写wp_query,如下所示:

从分类名称中删除$,并传递术语的值,如下所示

代码语言:javascript
复制
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
            array(
               'taxonomy' => 'ct_category',
               'field' => 'term_id',
               'terms' => $_POST['taxonomy_category'],
               'operator'=> 'IN'
                )
              )
           ); 
$loop = new WP_Query( $args );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35822950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档