Skip to content

Instantly share code, notes, and snippets.

@nicolasramy
Last active December 22, 2015 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolasramy/6429896 to your computer and use it in GitHub Desktop.
Save nicolasramy/6429896 to your computer and use it in GitHub Desktop.
Simple Wordpress hacks

Simple Wordpress Hacks

Categories

Add class on current Category item

Base

<div id="sidebar">

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

$categories=get_categories($args);
  foreach($categories as $category) {
  
   echo '<div class="bloc-cat" >';
    echo '<div id="titre-cat"><h2><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "Voir toutes les entrees %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> <h2></div> ';
    echo '<div id="description-cat">'. $category->description . '</div>'; 
    
    echo '</div>';
    
    } 
    
?>

</div>

Add condition to display class

<div id="sidebar">
   <?php
      $args = array(
         'orderby' => 'name',
         'order' => 'ASC'
      );

      $categories = get_categories($args);
      $current_category = is_single() ? get_the_category() : false;
      foreach ($categories as $category) {
         echo '<div class="bloc-cat' . (($current_category && $category->cat_ID == $current_category[0]->cat_ID) ? ' current-cat' : '') .'" >';
            echo '<div id="titre-cat">';
            echo '<h2><a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("Voir toutes les entrees %s"), $category->name) . '" >' . $category->name . '</a><h2>';
            echo '</div>';
            echo '<div id="description-cat">'. $category->description . '</div>'; 
         echo '</div>';
    } 
   ?>
</div>

Fix HTML id error

<div id="sidebar">
   <?php
      $args = array(
         'orderby' => 'name',
         'order' => 'ASC'
      );

      $categories = get_categories($args);
      $current_category = is_single() ? get_the_category() : false;
      foreach ($categories as $category) {
         echo '<div class="bloc-cat' . (($current_category && $category->cat_ID == $current_category[0]->cat_ID) ? ' current-cat' : '') .'" >';
            echo '<div id="titre-cat-' . $category->term_id . '">';
            echo '<h2><a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("Voir toutes les entrees %s"), $category->name) . '" >' . $category->name . '</a><h2>';
            echo '</div>';
            echo '<div id="description-cat">'. $category->description . '</div>'; 
         echo '</div>';
    } 
   ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment