【问题标题】:Browsing custom taxonomy terms on WordPress在 WordPress 上浏览自定义分类术语
【发布时间】:2013-10-31 11:19:22
【问题描述】:

我遇到了一个小问题,我可能需要你的帮助来解决它。

我正在构建一个使用自定义分类法并相应地标记帖子的网站。所以,有一个像http://example.com/custom-taxonomy/term 这样的页面,显示所有带有“term”标签的帖子。

访问者应该能够转到http://example.com/custom-taxonomy/ 并查看该自定义分类中使用的所有术语的列表,以便浏览它们。该列表也应该“分页”,因为某些分类法中可能包含相当多的术语。

关于我应该如何处理的任何想法?

非常感谢!

【问题讨论】:

    标签: wordpress taxonomy


    【解决方案1】:

    我会在这里回答我自己的问题,也许它会帮助别人。

    我创建了一个自定义页面模板,该模板使用 get_terms 来获取特定分类的术语并遍历它们以所需的方式显示它们。

    然后,我创建了一个页面,其 slug 与主要分类 slug 完全相同(在本例中为http://example.com/actors),因此在转到 /actors/ 时,您实际上会看到创建的页面充当分类法的索引页面。你可以在http://couch.ro/actori/看到它的效果

    在实际代码中,我还使用分类图像插件在实际标签上添加图像,因此 get_terms 通过 apply_filter() 函数执行。您有下面模板的完整代码。非常感谢任何反馈!

    <?php
    /*
    Template Name: Taxonomy Index
    */
    $slug_to_taxonomy=array('actori'=>'actor','regizori'=>'director');
    $terms_per_page=get_option( 'c2c_custom_post_limits' );
    
    if ($terms_per_page['archives_limit']==0)
    {
        $terms_per_page=get_options('posts_per_page');
    }
    else
    {
        $terms_per_page=$terms_per_page['archives_limit'];
    }
    
    $slug=$post->post_name;
    
    if (!isset($slug_to_taxonomy[$slug]))
    {
        header("Location: /");exit;
    }
    else
    {
        $taxonomy=$slug_to_taxonomy[$slug];
    }
    
    $terms_page=get_query_var('paged');
    if (empty($terms_page))
    {
        $terms_page=1;
    }
    
    $terms=apply_filters( 'taxonomy-images-get-terms', '',array('having_images'=>false,'taxonomy'=>$taxonomy, 'term_args'=>array('offset'=>($terms_page-1)*$terms_per_page,'number'=>$terms_per_page)) );
    
    if (empty($terms))
    {
        header("Location: /");exit;
    }
    
    $processed_terms=array();
    foreach ($terms as $term)
    {
        if (!empty($term->image_id))
        {
            $image_src=wp_get_attachment_image_src($term->image_id,'archive-thumbnail');
            $image_src=$image_src[0];
        }
        else
        {
            $image_src='http://couch.ro/wp-content/uploads/couchie_75.png';
        }
    
        $term_posts=get_posts(array('posts_per_page'=>3,'tax_query'=>array(array('taxonomy'=>$taxonomy,'field'=>'slug','terms'=>$term->slug))));
        $actual_term_posts=array();
        foreach ($term_posts as $post)
        {
            $actual_term_posts[$post->post_title]=get_permalink($post->id);
        }
    
        $processed_terms[]=array(
            'name'=>$term->name,
            'description'=>$term->description,
            'url'=>get_term_link($term),
            'image'=>$image_src,
            'posts'=>$actual_term_posts,
            'count'=>$term->count
        );
    }
    
    $has_next_page=(isset($processed_terms[$terms_page]));
    
    get_header();
    ?>
    <div class="posts-wrap"> 
        <div class="archive-title_wrap"><h1 class="archive-title"><?php the_title(); ?></h1></div>
        <div id="post_list_wrap">
    <?php
    foreach ($processed_terms as $term)
    {
        echo "<div class='post post-archive'>
            <a href='{$term['url']}' title='{$term['name']}'><img src='{$term['image']}' alt='{$term['name']}'></a>
            <div class='rating' style='text-align:right;'>{$term['count']} ".($term['count']==1?'review':'reviewuri')."</div>
            <h2 class='index-entry-title'>
                <a href='{$term['url']}' title='{$term['name']}'>{$term['name']}</a>
            </h2>
            <div class='archive-meta entry-meta-index'>
                <span>";
        $first_term_post=true;
        foreach ($term['posts'] as $title=>$link)
        {
            echo ($first_term_post?'':', ')."<a href='{$link}' title='{$title}'>{$title}</a>";
            $first_term_post=false;
        }
        echo "</span>
            </div>
        </div>";
    }
    ?>
        </div>
    <?php if ($terms_page>1 OR $has_next_page) { ?>
        <div class="navigation">
            <div class="nav-prev left"><?php if ($terms_page>1) echo "<a href='/{$slug}/".($terms_page>2?"page/".($terms_page-1)."/":'')."'>".__('&laquo; Previous Page', 'designcrumbs')."</a>"; ?></div>
            <div class="nav-next right"><?php if ($has_next_page) echo "<a href='/{$slug}/page/".($terms_page+1)."/'>".__('Next Page &raquo;', 'designcrumbs')."</a>" ?></div>
            <div class="clear"></div>
        </div>
    <?php } ?>
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    

    【讨论】:

      【解决方案2】:

      你可以像这样使用query_posts函数

      $termname = get_query_var('pagename'); //custom-taxonomy term
          query_posts(array(
              'post_type' => POST_TYPE,
              'showposts' => $limit,
              'custom-taxonomy' => $termname // use $term1.','.$term2.','.$term3 to get multiple terms posts
          ));
      

      在 Wordpress 上查看 Documentation

      要获得自定义分类下的所有术语,试试这个,您将拥有完全的控制权。

      global $wpdb;
              $taxonomy = CUSTOM_CAT_TYPE;
              $table_prefix = $wpdb->prefix;
              $wpcat_id = NULL;
              //Fetch category or Term as said                         
              $wpcategories = (array) $wpdb->get_results("
                      SELECT * FROM {$table_prefix}terms, {$table_prefix}term_taxonomy
                      WHERE {$table_prefix}terms.term_id = {$table_prefix}term_taxonomy.term_id
                      AND {$table_prefix}term_taxonomy.taxonomy ='" . $taxonomy . "' and  {$table_prefix}term_taxonomy.parent=0  ORDER BY {$table_prefix}terms.name ASC");
      
              $wpcategories = array_values($wpcategories);
      foreach ($wpcategories as $wpcat) {
      
                      $termid = $wpcat->term_id;
                      $name = $wpcat->name;
                      $termprice = $wpcat->term_price;
                      $tparent = $wpcat->parent;  
      }
      

      【讨论】:

      • 我不是在寻找一种方法来显示给定分类中的所有帖子,而是显示该分类中的术语,例如分类术语的“索引”。我已经设法通过创建自定义页面模板并使用 slug /custom-taxonomy/ 创建一个 PAGE 并处理该页面模板中的显示来做到这一点,但它似乎相当 hacky。
      猜你喜欢
      • 2020-01-04
      • 1970-01-01
      • 2013-11-17
      • 2014-08-16
      • 2012-07-11
      • 2012-08-09
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多