Support » Themes and Templates » in_category outside the loop

  • Hi guys,
    I’m setting up a second sidebar where I put the “submenus” of my site. I’m trying to use the in_category tag in this sidebar template, however it’s not working. A friend of mine told me the problem was because it was outside the loop, but in the codex it says it may be used outside the loop. So could someone take a look at it, please?

    My code:

    <ul class="submenu">
        <?php if ( is_category(10) || is_single() && in_category('10')): ?>
        <li><a class="icone-temas" href="<?php echo get_category_link(11);?> ">Temas</a></li>
        <li><a class="icone-plugins" href="<?php echo get_category_link(12);?> ">Plugins</a></li>
    	<?php elseif ( is_category(13) || is_single() && in_category('13')): ?>
        <li><a class="icone-dicas" href="<?php echo get_category_link(14);?> ">Dicas</a></li>
        <li><a class="icone-tutoriais" href="<?php echo get_category_link(15);?> ">Tutoriais</a></li>
    	<?php elseif ( is_category(1) || is_single() && in_category('1')): ?>
        <?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=1&title_li='); ?>
    	<?php elseif ( is_category(16) || is_single() && in_category('16')): ?>
        <?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=16&title_li='); ?>
    	<?php endif; ?>


Viewing 4 replies - 1 through 4 (of 4 total)
  • do you have WP in version 2.7 or above?

    here’s info that you can use it outside the loop in 2.7

    I’m setting up a second sidebar where I put the “submenus” of my site.

    Could you expand on what you are trying to achieve.




    Hi guys, thanks for your time,

    Yes, I’m using WP 2.7 and I have already read the in_category codex page. I understood from there that I can use it outside the loop, is that correct?
    So, what I’m trying to achieve is the following: I have a theme with 3 columns: main, sidebar, sidebar-slim. In the sidebar-slim I want to show a “submenu”, that is a menu listing the child categories when on a parent category page and a post belonging to its child categories, or the subpages when on a page and its subpages. Hope I was clear enough this time. 🙂
    I know I could achieve that using conditional statements on the single template, but I’d like to try it on the sidebar-slim template instead.


    For use in sidebar, if category archive show chidren

    if (is_category( )) {
      $cat = get_query_var('cat');
      $parent = get_category ($cat);
      if ($parent->parent) {
        wp_list_categories ('child_of=' . $parent->parent);
        } else {
        wp_list_categories ('child_of=' . $cat);

    For use in sidebar, display only top level Pages, but when viewing a Page that has children (or is a child) show only children of that parent
    When visiting main page, all top level pages are listed in the sidebar.
    When click on a top level page with no children, all top level pages are listed.
    When click on a top level page with children, just the children pages are listed.
    When click on a children page, just the children of that parent are listed.

    $output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
    if (is_page( )) {
      $page = get_query_var('page_id');
      if ($post->post_parent) {
        $page = $post->post_parent;
      $children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
      if ($children) {
        $output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>');
    echo $output;

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘in_category outside the loop’ is closed to new replies.