dongxuan1314 2014-10-16 14:02
浏览 26
已采纳

查询自定义分类词wordpress错误

I have a custom query which I'm trying to run. How I want it to work is,

  • Firstly, check the page title

  • if the page title is the same as the name of the cat from a custom taxonomy then show all the posts that are in that custom taxonomy - post type.

The only trouble is, it isn't returning anything, I've made sure I'm targeting the correct 'post type' & correct 'taxonomy' name. It will return the cat id with the following:

$cat->cat_ID;

But won't return any posts, here is my code:

<?php 

    // Get the name of the page
    $theTitle = get_the_title();
    //Get the taxonomy for the custom post type 
    $categoryselect = array( 'taxonomy' => 'team-members' );

    $categories = get_categories($categoryselect);

    // loop through each category as cat
    foreach ($categories as $cat):

        //If cat is the same as title *name* then lets do something     
        if($theTitle == $cat->cat_name):?>
            <h3>We’re here to help.</h3>            
        <?php   

            $catID = $cat->cat_ID;
            //echo $catID;

            //query the posts but, use the cat ID so the page relates to it.
            $args = query_posts(array( 'post_type' => 'team', 'cat'=> $catID, 'orderby' => 'title', 'showposts' => -1 ));
            $loop = new WP_Query( $args );

            // run the loop for posts
            while ( $loop->have_posts() ) : $loop->the_post();?>
            <div class="person">
                <h5><?php the_title(); ?></h5>
            </div>
            <?php endwhile; 

        endif; 

    endforeach;

This is on a page.php template

Any suggestions?

  • 写回答

1条回答 默认 最新

  • douyue2313 2014-10-16 17:24
    关注

    You have a couple of issues here. Lets start with this line

    $args = query_posts(array( 'post_type' => 'team', 'cat'=> $catID, 'orderby' => 'title', 'showposts' => -1 ));
    
    1. query_posts should never be used, and nor should you make use of two queries in one

    Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

    1. Secondly showposts has been depreciated in favor of posts_per_page

    2. You terminology is incorrect, and therefore you are making use of wrong parameters in your query. You are not working with categories here, but with a custom taxonomy and terms. To get a good perspective of categories, terms and custom taxonomies, see this post I have done on WPSE

    You should make use of a tax_query and not the category parameters in WP_Query

    1. To come back to how you get your terms. The way you are doing it with get_categories() is not wrong, but it can become confusing as you are actually working with a custom taxonomy and not the build-in category taxonomy. I would suggest to use get_terms() instead

    2. I actually feel that you don't need to use get_terms, get_categories or the foreach loop. I have checked your code and it seems the only time that something will show is when the term name is equal to the page name.

    You already have the taxonomy name and the term name, the only thing that you can maybe do is to check if the term exists, and then feed that to your custom query

    This is a modified version of your code, UNTESTED

    <?php 
        // Get the name of the page
        $theTitle = get_the_title();
        //Get the taxonomy for the custom post type 
        $taxonomy = 'team-members';
        //Set the page title as term name 
        $term = $theTitle;
    
            if( term_exists( $term, $taxonomy ) ) : // Check if there is a term that match the page title ?>
    
                <h3>We’re here to help.</h3>            
            <?php   
    
                //query the posts but, use the cat ID so the page relates to it.
                $args = array( 
                    'post_type' => 'team', 
                    'orderby' => 'title', 
                    'posts_per_page' => -1,
                    'tax_query' => array(
                        array(
                           'taxonomy'           => $taxonomy,
                           'field'              => 'name',
                           'terms'              => $term,
                           'include_children'   => false
                        ),
                    ),  
                );
    
                $loop = new WP_Query( $args );
    
                // run the loop for posts
                while ( $loop->have_posts() ) : $loop->the_post();?>
                <div class="person">
                    <h5><?php the_title(); ?></h5>
                </div>
                <?php endwhile; 
    
            endif; 
    ?>
    

    EDIT

    Have now tested the code and made a few minor adjustments. It is working 100% now on my local install.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题