문제

I ran into a little problem and I might need your help to sort it out.

I an building a website that uses custom taxonomies and tags posts accordingly. So, there is a page like http://example.com/custom-taxonomy/term that displays all the posts tagged with "term".

The visitor should be able to go to http://example.com/custom-taxonomy/ and see a lists of all the terms that are used inside that custom taxonomy in order to browse them. This list should also be "paginated" since some taxonomies could have quite a lot of terms in them.

Any ideas on how I should handle this?

Thanks a bunch!

도움이 되었습니까?

해결책

I'll answer my own question here, maybe it will help others.

I have created a custom page template that uses get_terms to get the terms for a specific taxonomy and iterates through them displaying them in the desired manner.

I then created a page with the exact same slug as the main taxonomy slug (http://example.com/actors in this case) and thus when going to /actors/ you actually see the page created that acts as an index page for the taxonomy. You can see it in effect at http://couch.ro/actori/

In the actual code I am also using the Taxonomy Images plugin for having images on the actual tags so the get_terms is executed through the apply_filter() function. You have the full code for the template below. Any feedback is highly appreciated!

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

다른 팁

You can use query_posts function like this

$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
    ));

see Documentation at Wordpress

To get all Terms under Custom Taxonomy try this you will have full control.

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;  
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top