WordPress.org

Ready to get started?Download WordPress

Forums

Add page Class has_children (3 posts)

  1. malcalevak
    Member
    Posted 5 years ago #

    Having started to delve into some WordPress plugin modification I've recently become aware of the various page classes page_item page-item-24 current_page_item cat-item cat-item-17 current_page_ancestor current_page_parent etc.

    It's come to my attention, though, that while these are all extremely helpful, there's one that's missing, and that's the "has_children" class. Basically if a post has children it would get this tag.

    If I'm missing something that already does this, please do clue me in, but I've been looking around and haven't seen it. I did manage to write a parsing script that sort of let's me get the functionality such a class would give me as well, but it's a little messy.

    Thanks!

  2. visaap
    Member
    Posted 5 years ago #

    That would be very nice to have; a class="hasChild" for parent-pages/-posts. Even 5 months later there's no-one who has encountered the same needs for such a class?

  3. visaap
    Member
    Posted 5 years ago #

    Using jQuery will allow you to have hasChild in your navigation on the right places!

    To accomplish this feature:

    • Open header.php from your theme.
    • Paste the following code just above <?php wp_head(); ?>:
      <?php wp_enqueue_script('jquery'); ?>
    • Paste just below <?php wp_head(); ?>:
      <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/behaviors.js"></script>
    • Save and close header.php
    • Create a new folder in your theme named 'js'
    • Create a file inside this new folder named 'behaviors.js'
    • Paste the following code inside this new file:
      jQuery(document).ready(function(){
      	jQuery("#pages li:has(ul)").addClass("hasChild");
      });
    • Save and close behaviors.js

    Read more about wp_enqueue_script

Topic Closed

This topic has been closed to new replies.

About this Topic