Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using custom taxonomies and custom post type in my blog and everything is running fine so far.

The problem is, I've made a small query to list all my child categories, but now I can't get the name of each parent because of the condition on line 10.

Here's my code:

<?php
$args = array(
        'orderby' => 'id',
        'order' => 'ASC',
        'taxonomy' => 'album'
);             

$categories=get_categories($args);
foreach($categories as $category) {
if($category->parent!=0) {
?>
<li class="span3">
<div class="thumbnail">
        <a href="<? bloginfo('url'); ?>/album/<?php echo $category->category_nicename; ?>" rel="nofollow">
                        <img src="<?php bloginfo('url'); ?>/covers/<?php echo $category->category_nicename; ?>.jpg" alt="">
        </a>

        <div class="caption">
                <a href="<? bloginfo('url'); ?>/album/<? echo $category->category_nicename; ?>">
                        <? echo $category->parent . ' - ' . $category->name; ?>
                </a>
        </div>
</div>
</li>
    <?
    }
}
    ?>

Any workaround?

share|improve this question
    
Please edit your questions to include your code here. A question should not depend on an external site. –  s_ha_dum Jul 31 '13 at 22:48

1 Answer 1

up vote 0 down vote accepted

Using two nested foreach you prevent n+1 db queries, where n is the number of child terms:

<?php
$_cats = array();
$args = array(
  'orderby' => 'id', 'order' => 'ASC', 'taxonomy' => 'album'
);             
$categories = get_categories($args);
if ( ! empty($categories) ) { foreach( $categories as $category ) {
    if ( $category->parent != 0 ) {
        if ( ! isset($_cats[$category->parent]) ) {
            $_cats[$category->parent] = array( 'terms' => array() );
        }
        $_cats[$category->parent]['terms'][] = $category;
    } else {
        if ( ! isset($_cats[$category->term_id]) ) $_cats[$category->term_id] = array();
        $_cats[$category->term_id]['parent_name'] = $category->name;
        $_cats[$category->term_id]['terms'] = array();
    }
} }
if ( ! empty($_cats) ) { foreach( $_cats as $parentid => $children ) {
  if ( ! empty($children['terms']) ) { foreach( $children['terms'] as $term ) {
    $album_url = site_url() . '/album/' . $term->category_nicename;
    $cover =  site_url() . '/covers/' . $term->category_nicename . '.jpg';
?>
<li class="span3">
<div class="thumbnail">
  <a href="<?php echo $album_url; ?>" rel="nofollow">
    <img src="<?php echo $cover; ?>" alt="<?php echo $term->name; ?>">
  </a>
  <div class="caption">
  <a href="<?php echo $album_url; ?>">
    <?php echo $children['parent_name'] . ' - ' . $term->name; ?>
  </a>
  </div>
</div>
</li>
<?php } } } } ?>

... and please, the right PHP open tag is <?php not <?.

share|improve this answer
    
AFAIK there's nothing wrong with using <? instead of <?php, it's just shorthand. Can you expalin why people shouldn't use that? Thanks for this answer. –  Rev Jun 27 at 16:16
    
From official PHP docs (bold mine): "PHP also allows for short open tags <? and ?> which are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option." @Rev –  G. M. Jun 27 at 16:32
    
Ah. So it's not that it's necessarily bad, it's just on some servers it might not work or could stop working. Makes sense, thanks. –  Rev Jun 27 at 20:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.