在WordPress上浏览自定义分类术语

时间:2013-10-31 11:19:23

标签: wordpress taxonomy

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

我建立了一个网站,使用自定义分类法并相应地标记帖子。因此,有一个类似http://example.com/custom-taxonomy/term的页面会显示标有“term”的所有帖子。

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

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

非常感谢!

2 个答案:

答案 0 :(得分:2)

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

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

然后我创建了一个与主要分类slug(在这种情况下为http://example.com/actors)完全相同的slug的页面,因此当转到/ actors /时,您实际上看到创建的页面充当了分类。您可以在http://couch.ro/actori/

看到它生效

在实际代码中,我还使用Taxonomy Images插件在实际标签上放置图像,以便通过apply_filter()函数执行get_terms。您拥有以下模板的完整代码。任何反馈都非常感谢!

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

答案 1 :(得分:0)

您可以使用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;  
}