Custom Taxonomy + JQuery Tabs

时间:2013-07-29 作者:jmysona

我在我的主页上创建了一个使用JQuery标签插件的小部件。因此,标签导航由自定义分类标题组成,下面的内容应该与这些标签相关。换句话说,每次用户点击标签时,我都想显示与分类法相关的帖子。

以下是我目前的代码:

//This part of my code creates tabs navigation

<ul id="tab-menu" class="menu_tabify">
<?php
$terms = get_terms(\'post_category\');
foreach ($terms as $term) {
    $wpq = array (\'taxonomy\'=>\'post_category\',\'term\'=>$term->slug, \'posts_per_page\' => -1);
    $myquery = new WP_Query ($wpq);
    $article_count = $myquery->post_count;
    echo "<li class=\\"term-heading\\">";
        echo "<a class=\\"term-heading\\" href=#".$term->slug.">";
            echo $term->name;
        echo "</a>";
    echo "</li>";
}
echo "</ul>";

// This part of my code creates tabs content where I would like to also include my loop
// which will display relevant posts depending on taxonomy
foreach ($terms as $term) {
    echo "<div class=\\"tab-content\\"  id=\\"".$term->slug."\\">";
        //loop to go here
    echo "</div>";
}

?>
有人能帮我创建那个循环吗?

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

这应该可以,但还没有测试

<div id="tabs">   

<ul id="tab-menu"> 
<?php
$terms = get_terms(\'category\');
global $wpdb;
$posts = array();
if ( is_array($terms) && ! empty($terms) ) {
$active = null;
foreach ( $terms as $i => $term ) {
  $ids = $wpdb->get_col( $wpdb->prepare("SELECT object_id FROM $wpdb->term_relationships
  WHERE term_taxonomy_id = %d",
  $term->term_taxonomy_id
  ));
  $articles_count = count($ids);
  if ( $articles_count > 0 ) { 
    if ( is_null($active) ) $active = $i;
    $posts[$term->term_id] = get_posts( array(\'post__in\' => $ids, \'posts_per_page\' => 5, \'orderby\' => \'post_date\', \'order\' => \'DESC\') );
  }
  $class = ($active == $i) ? \'term-heading active\' : \'term-heading\';
  ?>
  <li class="<?php echo $class; ?>">
    <a class="term-heading" href="#<?php echo $term->slug; ?>"><?php echo $term->name; ?></a>
  </li>
<?php
}
  echo \'</ul>  <!-- end #tab-menu -->\' . PHP_EOL;
}

//This part of code creates tabs content
if ( is_null($active) ) $active = 0;
foreach ( $terms as $i => $term ) {
  $class = ($active == $i) ? \'tab-content active\' : \'tab-content\';
  ?>
  <div class="<?php echo $class; ?>"  id="<?php echo $term->slug; ?>">
  <?php
  if ( ! empty($posts[$term->term_id]) ) {
    echo \'<ul>\' . PHP_EOL;
    global $post;

    foreach ($posts[$term->term_id] as $post) {
    setup_postdata($post); 
  ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
  <?php
  }
    echo \'</ul>\' . PHP_EOL;
  } else {
    printf(\'<p>%s</p>\', __(\'No posts in this category.\'));
  }
  wp_reset_postdata();
  ?>
  </div>
<?php } ?>

</div> <!-- end #tabs -->

结束

相关推荐

Pagination and multiple loops

对,所以我现在有一个使用css网格将页面分成三部分的页面。第三列是侧栏,前两列各有一个要显示的帖子查询,并创建一个漂亮的帖子网格(虚拟内容):http://puu.sh/2Xh9o.jpg每个循环如下所示: <?php query_posts(\'showposts=5\'); ?> <?php $posts = get_posts(\'numberposts=5&offset=0\'); foreach ($posts as $post) : start_wp()