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

Add class to parent terms. WordPress

  • SOLVED

I need to modify the foreach section of both the 'resourcemenu' and 'resourcearchive' so that the parent terms get a 'parent-term' class added to them (so I can style them differently).

Thanks,
CW



div class="span3 resourcemenu">

<?php
$args = array(
'taxonomy' => 'seo_resource_topics',
'hierarchical' => true
);
$terms = get_terms('seo_resource_topics', $args);
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li><a class=\"scroll\" href=#" . $term->slug . ">" . $term->name . "</a></li>";
}
echo "</ul>";
}
?>


</div>

<div class="span9">
<div class="resourcearchive padded">
<?php

$taxonomy = 'seo_resource_topics';

// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );

foreach( $terms as $term ) :
echo '<h3 id="' . $term->slug . '">'. $term->name. '</h3>';
if ( $term->parent == 0 ) continue;
$resourcequery = new WP_Query(array(
'post_type' => 'seo_resource',
'taxonomy' => $taxonomy,
'term' => $term->slug,
'posts_per_page' => -1
));

if( $resourcequery->have_posts() ): while( $resourcequery->have_posts() ) : $resourcequery->the_post(); ?>

<article <?php post_class(); ?>>
<header>
<a href="<?php the_permalink(); ?>"><strong><?php the_title(); ?></strong></a>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<?php endif;

endforeach;

?>
</div>

</div>

Answers (4)

2013-10-16

S├ębastien | French WordpressDesigner answers:


div class="span3 resourcemenu">

<?php
$args = array(
'taxonomy' => 'seo_resource_topics',
'hierarchical' => true
);
$terms = get_terms('seo_resource_topics', $args);
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
$class= ($term->parent == 0) ? " parent-term" : "";
echo "<li><a class=\"scroll".$class."\" href=#" . $term->slug . ">" . $term->name . "</a></li>";

}
echo "</ul>";
}
?>
</div>
<div class="span9">
<div class="resourcearchive padded">
<?php
$taxonomy = 'seo_resource_topics';
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$class= ($term->parent == 0) ? " class=\"parent-term\"" : "";
echo '<h3 '.$class.'id="' . $term->slug . '">'. $term->name. '</h3>';
if ( $term->parent == 0 ) continue;
$resourcequery = new WP_Query(array(
'post_type' => 'seo_resource',
'taxonomy' => $taxonomy,
'term' => $term->slug,
'posts_per_page' => -1
));
if( $resourcequery->have_posts() ): while( $resourcequery->have_posts() ) : $resourcequery->the_post(); ?>

<article <?php post_class(); ?>>
<header>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<?php endif;
endforeach;
?>
</div>
</div>


you can add
<style>
.parent-term {
background:gold
}</style>

to see where is the class added in the both menu


Christopher comments:

That worked perfectly. Many thanks.

2013-10-16

Remy answers:

You can use [[LINK href="http://codex.wordpress.org/Function_Reference/get_term_children"]]get_term_children()[[/LINK]] to check if a term has children, and add a class to it. Here is an example on how to use it


foreach ( $terms as $term ) {
$children = get_term_children ( $term, 'seo_resource_topics' );
if ( !empty( $children ) && !is_wp_error( $children ) ) {
$parent_class = 'class="parent-term"';
}
echo "<li $parent_class><a class=\"scroll\" href=#" . $term->slug . ">" . $term->name . "</a></li>";

}


Here I applied the class to the li element, but you can add it wherever you want.

2013-10-16

Arnav Joy answers:

W




div class= ("span3 resourcemenu">



<?php

$args = array(

'taxonomy' => 'seo_resource_topics',

'hierarchical' => true

);

$terms = get_terms('seo_resource_topics', $args);

$count = count($terms);

if ( $count > 0 ){

echo "<ul>";

foreach ( $terms as $term ) {

If($term->parent == 0 )

cho "<li class='parent_term'><a class=\"scroll\" href=#" . $term->slug . ">" . $term->name . "</a></li>";
else
echo "<li><a class=\"scroll\" href=#" . $term->slug . ">" . $term->name . "</a></li>";

}

echo "</ul>";

}

?>





</div>



<div class="span9">

<div class="resourcearchive padded">

<?php



$taxonomy = 'seo_resource_topics';



// Gets every "category" (term) in this taxonomy to get the respective posts

$terms = get_terms( $taxonomy );



foreach( $terms as $term ) :

echo '<h3 id="' . $term->slug . '">'. $term->name. '</h3>';

if ( $term->parent == 0 ) continue;

$resourcequery = new WP_Query(array(

'post_type' => 'seo_resource',

'taxonomy' => $taxonomy,

'term' => $term->slug,

'posts_per_page' => -1

));



if( $resourcequery->have_posts() ): while( $resourcequery->have_posts() ) : $resourcequery->the_post(); ?>



<article <?php post_class(); ?>>

<header>

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

</header>

<div class="entry-summary">

<?php the_excerpt(); ?>

</div>

</article>

<?php endwhile; ?>

<?php endif;



endforeach;



?>

</div>



</div>

2013-10-16

Fahad Murtaza answers:

Why not use wp_list_categories() where the hierarchical argument is already set (by default) ,


<?php wp_list_categories('taxonomy' => 'your_taxonomy_name'); ?>


Define $args array and use as follow if you want, but above should do the trick for you.


<?php wp_list_categories($args);?>


Fahad Murtaza comments:

My code sample using the technique discussed above.



<div class="span3 resourcemenu">

<?php

$args = array(

'taxonomy' => 'seo_resource_topics',
'hierarchical' => true

);

wp_list_categories($args);
?>
</div>


<div class="span9">
<div class="resourcearchive padded">

<?php
$taxonomy = 'seo_resource_topics';

// Gets every "category" (term) in this taxonomy to get the respective posts

$terms = get_terms($taxonomy);

foreach ($terms as $term):
echo '<h3 id="' . $term->slug . '">' . $term->name . '</h3>';

if ($term->parent == 0)
continue;

$resourcequery = new WP_Query(array(

'post_type' => 'seo_resource',

'taxonomy' => $taxonomy,

'term' => $term->slug,

'posts_per_page' => -1

));



if ($resourcequery->have_posts()):
while ($resourcequery->have_posts()):
$resourcequery->the_post();
?>
<article <?php
post_class();
?>>

<header>

<a href="<?php
the_permalink();
?>"><?php
the_title();
?></a>

</header>

<div class="entry-summary">

<?php
the_excerpt();
?>

</div>

</article>

<?php
endwhile;
endif;
endforeach;

?>
</div>
</div>