Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Taxonomy Listings WordPress

  • SOLVED

I am trying to create taxonomy for a brand directory.

I want brands to be listed alphabetically. A brand will exist by name as well as 4 other parameters.

I am having a hard time with search a-z and also with listing all brands under a certain letter, so if i select a, i want to show a list of all brands under 'a'

I hope the attachment helps visualise what i am trying to do

**EDIT**

I have created a page with the following


<?php

/*Template Name: brandsPagea*/

?>

<?php get_header(); ?>

<?php get_sidebar(); ?>
<div id="container">


<div id="content_listing">




<?php $recent = new WP_Query('post_type=brands&posts_per_page=100'); while($recent->have_posts()) : $recent->the_post();?>
<div class="listing_thumb"><?php the_post_thumbnail( $size, $attr ); ?></div>
<?php the_title( '<h2><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>

<?php echo get_the_term_list( $post->ID, 'Departments', 'Departments: ', ', ', '' ); ?><br>
<?php echo get_the_term_list( $post->ID, 'Store', 'Store: ', ', ', '' ); ?><br>
<?php echo get_the_term_list( $post->ID, 'Location', 'Location: ', ', ', '' ); ?><br>
<?php echo get_the_term_list( $post->ID, 'Categories', 'Categories: ', ', ', '' ); ?>


<?php the_content(); ?>

<?php wp_link_pages( array( 'before' => '<div>' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>


<?php endwhile; ?>



</div>

</div>



<?php get_footer(); ?>


With this i am displaying all posts (from my custom posts type brands)

I have added another taxonomy 'Alphabetical' and hope to be able to sort and search based on this taxonomy if possible?

I need to be able to produce an alphabetical list 'A-Z' and when a user clicks on a letter i want to show a list of brands which start with the letter.

I also want to be able (on a custom page) to build a query which will list all associated brands under a specific letter. Can someone please help?


****EDIT****

My theme is based on default twentyten

I have tested a page with the following code, the output however lists alphabetical terms, but includes all posts, regardless of whether they have a letter in 'taxonomy' associated or not


<?php
//Get terms for this taxonomy - orders by name ASC by default
$terms = get_terms('Alphabetical');

//Loop through each term
foreach($terms as $term):

//Query posts by term.
$args = array(
'orderby' => 'title', //As requested in comments
'tax_query' => array(
array(
'taxonomy' => 'Alphabetical',
'field' => 'slug',
'terms' => array($term->slug)
)
));
$tag_query = new WP_Query( $args );

//Does tag have posts?
if($tag_query->have_posts()):

//Display tag title
echo '<h2> Tag :'.esc_html($term->name).'</h2>';

//Loop through posts and display
while($tag_query->have_posts()):$tag_query->the_post();
//Display post info here
endwhile;

endif; //End if $tag_query->have_posts
wp_reset_postdata();
endforeach;//Endforeach $term

?>


Answers (7)

2012-07-05

John Cotton answers:

Brand should be a custom post type, then you can add taxonomies for styles of clothing sold or whatever, use the post content or excerpt for description, post thumbnail for logo and add post meta for other info you want.

Then, when you come to query the posts to list alphabetically, just user order-by name.

In the loop, test for the initial letter and then change the output as it changes....

So, for example (some pseudo code):


// query posts, ordering by name
$initials = array();

foreach( $posts as $post ) {
$letter = substr( $post->post_title, 1 );
if( !isset($initials[$letter]) ) {
$initials[$letter] = array();
}
$initials[$letter][] = array( 'title' => $post->post_title, 'id' => $post->ID );
}

// now we have an array of arrays, with initial letter as the key which we can loop through and output
foreach( $initials as $letter => $posts ) {
echo '<h2>'.$letter.'</h2>';
echo '<ul>';
foreach( $posts as $post ) {
echo '<li>'.$post['title'].'</li>';
}
echo '</ul>';
}


Clearly that could be refined to improve output, but that should give you the idea...


Daniel Q comments:

Hi John Cotton,

Your explanation spunds like exactly what i am trying to do! I think there may be a knowledge gap here in implementationbut i am reading more into it. Navjpt had suggested similar but my experience with custom ppost type to date did not handle alphabetizing well. Can i ask, based on the images i attached, can i achieve both layout types with your suggestion?


Daniel Q comments:

Hi again John Cotton,

Can i ask you to explain a little more? Im sorry but i think im in over my head and my stress ball is broken*

*out the window

2012-07-05

Navjot Singh answers:

I am assuming brand is a custom post type? If that is the case, you can check [[LINK href="http://www.kathyisawesome.com/424/alphabetical-posts-glossary/"]]this post by Kathy[[/LINK]] for the solution. Hope that helps.

Since my answer does not show, am editing this.

Check [[LINK href="http://wordpress.stackexchange.com/questions/46830/loop-through-all-tags-output-posts-in-alphabetical-list"]]this page for another solution[[/LINK]] based on taxonomy based sorting.


Daniel Q comments:

I have tried this, i want to be able to sort based on taxonomy but am having issues when using a custom post type...


Daniel Q comments:

Hi Navjot,

Kathy's solution (after further testing) produces an alphabetical list but no letters are clickable :(

2012-07-05

Spencer Tomosvary answers:

<blockquote>I am having a hard time with search a-z and also with listing all brands under a certain letter, so if i select a, i want to show a list of all brands under 'a'</blockquote>

Can you post the code you've for for your search function or is it a plugin?
Why not tag each brand with the appropriate letter?


Daniel Q comments:

Hi Spencert, im using code from this article [[LINK href="http://www.aroundwp.com/search-form-with-custom-taxonomy-terms-and-categories/"]][[/LINK]] but i dont know if taxonomy is best use here? im beginning to think this should be custom post type...

2012-07-05

AdamGold answers:

Try:
$args=array(
'public' => true,
'_builtin' => false

);
$output = 'objects';
$operator = 'and';
$taxonomies=get_taxonomies($args,$output,$operator);
$groups = array();
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
$first_letter = strtoupper( $taxonomy->name );
$groups[ $first_letter ][] = $taxonomy;
}
}
ksort($groups);
if( !empty( $groups ) ) {
foreach ($groups as $letter => $tags) {
echo $letter . '-';
foreach( $tags as $taxonomy ) {
echo $taxonomy->name . '<br />';
}
}
}


Daniel Q comments:

Thanks AdamGold, Im sorry but i dont understand this, where does this fit?

Thank you for being patient


AdamGold comments:

2012-07-07

nleavinsbarr answers:

The new personalized taste experience cowboy Slim fold lanterns - $55.00 :

2012-07-08

isimpkinreita answers:

Kasman / CASIMA classic watches three business strip male table - $123.00 :

2012-07-09

Arnav Joy answers:

please try this
<?php

//Get terms for this taxonomy - orders by name ASC by default

$terms = get_terms('Alphabetical');



//Loop through each term

foreach($terms as $term):



//Query posts by term.

$args = array(

'orderby' => 'title', //As requested in comments
'post_type' => 'brands',

'tax_query' => array(

array(

'taxonomy' => 'Alphabetical',

'field' => 'slug',

'terms' => array($term->slug)

)

));

$tag_query = new WP_Query( $args );



//Does tag have posts?

if($tag_query->have_posts()):



//Display tag title

echo '<h2> Tag :'.esc_html($term->name).'</h2>';



//Loop through posts and display

while($tag_query->have_posts()):$tag_query->the_post();

//Display post info here

endwhile;



endif; //End if $tag_query->have_posts

wp_reset_postdata();

endforeach;//Endforeach $term



?>