2013-04-30 102 views
1

我的工作离TwentyTwelve主题,我已经通过了环无法获得类别显示

get_header(); ?> 

<div id="primary" class="site-content"> 
    <div id="content" role="main" class="clearfix"> 
     <?php 
      $terms = get_the_category(); 
      $count = count($terms); 
      echo '<ul id="post-filter">'; 
       echo '<li><a href="#all" title="">All</a></li>'; 
       if ($count > 0){ 

        foreach ($terms as $term) { 

         $termname = strtolower($term->name); 
         $termname = str_replace(' ', '-', $termname); 
         echo '<li><a href="#'.$termname.'" title="" rel="'.$termname.'">'.$term->name.'</a></li>'; 
        } 
      } 
      echo "</ul>"; 
     ?> 
     <div id="mwrapper"> 

    <?php query_posts('cat=-6,-7'); ?> 
    <?php if (have_posts()) : ?> 

     <?php /* Start the Loop */ ?> 
     <?php while (have_posts()) : the_post(); ?> 
      <div class="box">.... 

我试图创建一个过滤器将过滤前加入这个片段修改索引文件通过博客文章。像演示here一样。目前我有五个类别:机构注释,设计笔记,精选,幽默,未分类。每个类别至少有一篇文章,但它似乎只是在设计笔记中。

我也试图改变get_the_category();wp_list_categories();但最终显示的所有类别。

Source我碰到的片段。

+0

首先,它是更好地使用'$条款而─> slug',而不是'用strtolower()'和'str_replace()函数'。 – doublesharp 2013-04-30 16:22:08

回答

2

get_the_category()抓住当前职位的类别/ IES信息,在充分WP安装类别不在名单。

我想你要找的是什么get_categories()功能(更多信息在这里的圣典:http://codex.wordpress.org/Function_Reference/get_categories

<?php 
    $categories=get_categories(array('order' => 'ASC', 'orderby' => 'name')); 
    $count = count($terms); 
    [...] 
+0

也可能需要'query_posts()'之前,而不是之后它,因为在OP的例子。 – doublesharp 2013-04-30 16:27:34

+0

工作。你太棒了,谢谢! – 2013-04-30 18:10:49

0

首先,你想要得到的所有类别。 get_the_category()不会这样做。您可能需要get_categories()。

$terms = get_categories(); 
$count = count($terms); 
echo '<ul id="post-filter">'; 
    echo '<li><a href="#all" title="">All</a></li>'; 
    if ($count > 0) { 
    foreach ($terms as $term) { 
     echo '<li><a href="#" data-slug="'.$term->slug.'">'.$term->name.'</a></li>'; 
    } 
    } 
echo "</ul>"; 

我也做了一些修改:删除了hash和rel属性。我们可以使用更具语义的数据属性。

下一部分取决于你的后置HTML,但我假设他们有一类post和类别他们是如果他们这样做,你可以做这样的事情用jQuery:

$('a', '#post-filter').on('click', function(e) { 
    e.preventDefault(); 

    $('.post').hide().filter('.' + $(this).attr('data-slug')).show(); 
}); 

这将隐藏所有帖子,并只显示所选类别的人。我会把它留给你来整理动画。