2014-10-16 96 views
0

我有一个自定义查询,我试图运行。我多么希望它的工作是,查询自定义分类wordpress错误

  • 首先,检查页面标题

  • 如果网页标题是一样的,从一个自定义分类猫的名字,然后显示所有可在帖子在那个自定义分类中 - 发布类型。

唯一的麻烦是,它不返回任何东西,我确信我针对正确的“柱式” &正确的“分类”的名字。它将返回猫ID改为:

$cat->cat_ID; 

,但不会返回任何帖子,这里是我的代码:

<?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; 

这是一个page.php文件模板

任何建议?

+0

这个页面/模板是这个 – 2014-10-16 14:52:02

+0

它运行在page.php – 2014-10-16 15:07:05

回答

1

这里有几个问题。让我们开始这条线

$args = query_posts(array('post_type' => 'team', 'cat'=> $catID, 'orderby' => 'title', 'showposts' => -1)); 
  1. query_posts不应该被使用,也不应该使用两个查询的一个

注:此功能并不意味着被插件或主题使用。正如后面所解释的,改变主查询有更好,更高性能的选项。 query_posts()过于简单并且存在问题,通过用查询的新实例替换页面的主查询来修改主查询。它效率低下(重新运行SQL查询),并在某些情况下彻底失败(特别是在处理帖子分页时)。

  • 其次showposts,取而代之posts_per_page

  • 您的术语已经贬值是不正确的,因此你正在使用错误的参数的查询。您不是在此处使用类别,而是使用自定义分类和术语。要获得的类别,术语和自定义分类一个很好的视角,看到this post我已经WPSE做

  • 你应该使用一个tax_query,而不是category parametersWP_Query

  • 回到你如何得到你的条件。你用get_categories()这样做的方式并没有错,但它可能会让你感到困惑,因为你实际上在使用自定义分类法而不是内置的分类法。我会建议使用get_terms()代替

  • 其实我觉得你并不需要使用get_termsget_categoriesforeach循环。我检查了你的代码,似乎唯一一次显示的是术语名称等于页面名称。

  • 您已经有了分类名称和术语的名字,你也许可以做的唯一一件事就是检查term exists,然后养活,为您的自定义查询

    这是一个修改版本你的代码,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; 
    ?> 
    

    编辑

    现在已经测试了代码并做了一些小的调整。现在我的本地安装正在运行100%。

    +0

    真棒,感谢所有关于它的信息。我刚刚测试过它,它工作完美。我没有意识到自己造成的一些问题,这使得他们完美无缺。我没有意识到我正在执行循环两次,我还会检查折旧项目的其他代码。再次感谢!! – 2014-10-17 08:54:57

    +0

    我的荣幸,很高兴我的帖子很有用。您应该查看一下名为Frank Bueltge的调试对象插件的wordpress plugin respitory。这是开发人员必备的工具。下载并安装在您的测试环境中。这也将帮助你在发展中的大好时机 – 2014-10-17 09:12:26

    +0

    太好了,我现在肯定会去做,非常感谢。 – 2014-10-17 09:18:27