首页 文章

自定义帖子类型,按元框值排序分类术语

提问于
浏览
1

我已经创建了一个带分类的自定义帖子类型 . 我需要在自定义帖子类型中按元值对分类法帖子进行排序 . 我正在使用插件高级自定义字段在我的自定义帖子类型中为我的帖子插入单独的元值字段 . 到目前为止,我已经能够根据其 Value 对帖子分类进行排序 . 我的问题是我在前端打印后将“所有”分类标记 . 我想获得与其相关的每个分类的帖子,而不是所有 . 我一直在测试通过在wp_query数组上实现它来获取术语id,但没有成功 . 我只是想单独显示我的分类法并 grab 相关的帖子,这是可能的,我做错了什么?

这是我的代码......

<?php 
get_header(); 

if( have_posts() ) { 
?>
    <section class="section container">
        <div class="row">
<?php       
            // gets our taxonomy title      
            global $wp_query; 
            $term = $wp_query->get_queried_object();
            $title = $term->name; 
?>
            <!-- show our taxonomy title on the front-end of the site -->
            <header id="<?php echo $term->slug;?>" class="col-md-12">   
                <h1><a href="<?php bloginfo( 'url' ); ?>/?p=493"><span class="glyphicon glyphicon-circle-arrow-left" aria-hidden="true"></span></a><?php echo $title; ?></h1>                            
            </header>`enter code here`
<?php              

        // wp_query-loop for our custom post typ "Personal", sort by meta_value 
        $wp_query = new WP_Query(array(
            'post_type'         => 'Personal',
            'posts_per_page'    => -1,
            'meta_key'          => 'sorterings_nummer',
            'orderby'        => '$term->term_id meta_value',
            'ordertax'       => 'DESC',
            'order'          => 'ASC'
        ));

        // gets our custom post type posts with a thumbnail, title and contact details  
        while( $wp_query->have_posts() ) {
            $wp_query->the_post(); 

            $class = get_field('sorterings_nummer') ? 'class="sorterings_nummer"' : '';

            $titel = get_post_meta(get_the_ID(), 'titel');
            $telefon = get_post_meta(get_the_ID(), 'telefon');
            $mobil = get_post_meta(get_the_ID(), 'mobil');
            $mail = get_post_meta(get_the_ID(), 'mail');
            add_filter('the_content', 'wpautop');
?>
            <article class="col-xs-6 col-sm-4 col-md-4 col-lg-3" >
                <div class="align-center">
                    <div class="content--thumbnail">
                        <?php the_post_thumbnail(); ?>
                    </div>

                    <header class="content--title">
                        <h2><?php the_title(); ?></h2>
                    </header>

                    <section class="content--contactDetails">
                        <h3 class="titel"><?php echo $titel[0] ? $titel[0]: ''; ?></h3>  
                        <p class="telefon"><strong><a href="tel:<?php echo $telefon[0] ?>"><?php echo $telefon[0] ? $telefon[0]: ''; ?></strong></a></p>
                        <p>
                            <a class="mail" href="mailto:<?php echo $mail[0] ?>"><?php echo $mail[0] ? $mail[0]: ''; ?></a>
                            <a class="mobil" href="tel:<?php echo $mobil[0] ?>"><?php echo $mobil[0] ? $mobil[0]: ''; ?></a>
                        </p>
                    </section>    
                </div>
            </article>
<?php 
        } // while content
?>
        </div> <!-- .row -->
    </section> <!-- .container -->
<?php 
} // if 
get_footer();

1 回答

  • 0

    请尝试使用tax_query()来检索分配给特定分类术语的帖子 .

    请在下面找到您更新的$ wp_query()代码:

    $wp_query = new WP_Query(array(
                'post_type'         => 'Personal',
                'posts_per_page'    => -1,
                'tax_query'         => array(array('taxonomy' => 'taxonomy_slug_name', 'field' => 'id', 'terms' => $term->term_id )),
                'meta_key'          => 'sorterings_nummer',
                'orderby'           => 'meta_value',
                'ordertax'          => 'DESC',
                'order'             => 'ASC'
            ));
    

    请在tax_query()的分类列中编写'taxonomy_slug_name' .

    希望,这可能对你有所帮助 .

相关问题