logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$30
Custom Taxonomy Listings

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

?>


This question has been answered.

attachment image View Attachment

Daniel Q | 07/05/12 at 10:15am Edit

Previous versions of this question: 07/07/12 at 5:56pm | 07/08/12 at 5:18am

(7) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    07/05/12
    10:59am
    Navjot Singh says:

    I am assuming brand is a custom post type? If that is the case, you can check this post by Kathy for the solution. Hope that helps.

    Since my answer does not show, am editing this.

    Check this page for another solution based on taxonomy based sorting.

    Previous versions of this answer: 07/05/12 at 10:59am

    • 07/05/12 10:31am

      Daniel Q says:

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

    • 07/08/12 5:21am

      Daniel Q says:

      Hi Navjot,

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

  • avatar
    Last edited:
    07/05/12
    10:25am
    spencert says:

    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'


    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?

    • 07/05/12 11:03am

      Daniel Q says:

      Hi Spencert, im using code from this article but i dont know if taxonomy is best use here? im beginning to think this should be custom post type...

  • avatar
    Last edited:
    07/05/12
    10:32am
    AdamGold says:

    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 />';
    }
    }
    }

    • 07/05/12 10:57am

      Daniel Q says:

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

      Thank you for being patient

    • 07/05/12 4:24pm

      AdamGold says:

  • avatar
    Last edited:
    07/05/12
    10:38am
    John Cotton says:

    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...

    • 07/05/12 11:00am

      Daniel Q says:

      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?

    • 07/05/12 11:08am

      Daniel Q says:

      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

  • avatar
    Last edited:
    07/07/12
    3:40pm
    nleavinsbarr says:

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

  • avatar
    Last edited:
    07/08/12
    6:23pm
    isimpkinreita says:

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

  • avatar
    Last edited:
    07/09/12
    1:46am
    Arnav Joy says:

    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



    ?>

    Previous versions of this answer: 07/09/12 at 1:46am

This question has expired.



Daniel Q had additional discourse to offer.

Gabriel Reguly, Daniel Q voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.