• Hello, i have created a menu with the categories, The only think is that i want to change the css of the active category. Until now the code is like this:

    <div class="CategoryMenu">
    <?php
    $Awards_id= get_cat_ID('awards');
    $Press_id= get_cat_ID('press');
    $News_id= get_cat_ID('news');
    $Commercial_id= get_cat_ID('commercial');
    
    $args = array(
      'orderby' => 'name',
      'parent' => 0,
      'exclude' => array($Awards_id,$Press_id,$News_id )
    
      );
    
    $categories = get_categories( $args );
    foreach ( $categories as $category ) {
    
    	echo '<a class="CategoryArchive"  href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/>';
    }
    
    ?>
    </div> 

    Here for example the category is commercial, the foreach loop goes through all the categories and creates the link.I tried to make an if statement with is_category() function but it seems it wasnt working.

Viewing 6 replies - 1 through 6 (of 6 total)
  • is_category() function will work in that case, you need to pass id or name or slug in that function and conditionally you can apply css class on anchor element.

    Thread Starter phaidonas

    (@phaidonas)

    the code exists in a category-[myslug].php page, i tried it but either it does nothing either it gives the class in all the titles. i tried this kind of code:

    $Awards_id= get_cat_ID('awards');
    $Press_id= get_cat_ID('press');
    $News_id= get_cat_ID('news');
    $Commercial_id= get_cat_ID('commercial');
    
    $args = array(
      'orderby' => 'name',
      'parent' => 0,
      'exclude' => array($Awards_id,$Press_id,$News_id )
    
      );
    
    $categories = get_categories( $args );
    if(is_category('myslug')){
    foreach ( $categories as $category ) {
    
    	echo '<a>term_id ) . '">' . $category->name . '</a><br/>';
    }
    }else{
    foreach ( $categories as $category ) {
    
    	echo '<a>term_id ) . '">' . $category->name . '</a><br/>';
    }
    }
    
    ?>
    </div>
    • This reply was modified 6 years, 11 months ago by phaidonas.
    Moderator bcworkz

    (@bcworkz)

    If is_category() is not working, there is some sort of problem with what query is currently in the global $wp_query. But if this code is in category-[myslug].php, the only time this template is used is when is_category(‘myslug’) == true. All you need to do is output another class when $category->slug == ‘myslug’ in the foreach loop.

    Thread Starter phaidonas

    (@phaidonas)

    it works like a charm thank you, i have an other question somehow similar, the category page is a redirect of a page.I want to take the page by class (page-item-id) and give also an other class.
    The code that i used is a mixture of php and javascript but it doesnt work.Here:

    <?php
    $Archive_Id = get_page_by_path('southarchive');
    ?>
    
      <script> var y = document.getElementsByClassName("page-item-<?php$Archive_Id->ID;?>");
      y.className += " ArchiveActive";
      </script>
    • This reply was modified 6 years, 11 months ago by phaidonas.
    Moderator bcworkz

    (@bcworkz)

    I think y is still an array even if it only contains one element, I think you want y[0].className += " ArchiveActive";

    There may be a PHP only solution. Many themes output page container classes using post_class(). If yours does, use the “post_class” filter to add your class to the passed array if the passed ID matches that of southarchive. Or you could add code directly to the template that does the same thing.

    Thread Starter phaidonas

    (@phaidonas)

    I dont think so that is an array if you dont declare it so, I think it takes the class as an instance, Never less i did it with jquery which has better selector, but maybe also works with javascript.I just thought it after using the jquery

    Here the code:

    <?php
    $Archive_Id = get_page_by_path('southarchive');
    ?>
    
      <script type="text/javascript">
      $(  ".page-item-<?php echo $Archive_Id->ID?>" ).addClass( "ActiveArchive" );
      </script>
     

    The problem was the echo
    😛

    I will also try the post_class, i have an underscore theme.
    I dont think so putting the class inside the page file will work,because also then i will have to active it somehow from the category file because only then i want this class to exist.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Check if is category page’ is closed to new replies.