WordPress.org

Ready to get started?Download WordPress

Forums

query_posts - add class current Page - Child (5 posts)

  1. Omegakenshin
    Member
    Posted 3 years ago #

    I have a "navigation" with query_post and I want to add the class "current" when the child is the current page.

    query_posts('post_type=page&post_parent=37&order=asc'); while (have_posts()) : the_post (); ?>
    <li ADD CLASS HERE id="post-<?php the_ID(); ?>" > <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <img src="IMAGE HERE" alt="" />
    </li>
    <?php endwhile; wp_reset_query();

    I didn't do it with register_nav_menus because I need to custom it a little with images.
    Thanks

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    Try this:

    $post_id = $post->ID;
    query_posts('post_type=page&post_parent=37&order=asc'); while (have_posts()) : the_post (); ?>
    <?php
    $class = '';
    if ($post_id == $post->ID){ $class = 'class="current" '; }
    ?>
    <li id="post-<?php the_ID(); ?>" <?php echo $class ?>> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <img src="IMAGE HERE" alt="" />
    </li>
    <?php endwhile; wp_reset_query();
  3. Omegakenshin
    Member
    Posted 3 years ago #

    Excelent, that's it ^^, thanks a lot n.n

  4. ryansebiz
    Member
    Posted 3 years ago #

    Thanks kees - been banging my head against the wall for a couple hours trying to find this solution.

  5. plantprojects
    Member
    Posted 3 years ago #

    Where do you put this code, in the template file or functions.php?

Topic Closed

This topic has been closed to new replies.

About this Topic