最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to get child pages and add unique class only to active page?

programmeradmin3浏览0评论

I have a parent page with child pages. How to get child pages and add unique class only to active page ? I am making a sidebar with children's menu parent page one level up.

I can list all child pages but i have problem to add an unique class to active child page to css differently.

<?php
global $post;
$direct_parent = $post->post_parent;
$args = array(
'post_type'      => 'page',
'posts_per_page' => -1,
'post_parent'    => $direct_parent, 
'order'          => 'ASC',
'orderby'        => 'menu_order'
);


$parent = new WP_Query( $args );

if ( $parent->have_posts() ) : ?>

<ul class='sidebar-list'>

<?php while ( $parent->have_posts() ) : $parent->the_post();
?>
<li>
<a href="<?php the_permalink()?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>

</ul>

<?php endif; wp_reset_query(); ?>

I have a parent page with child pages. How to get child pages and add unique class only to active page ? I am making a sidebar with children's menu parent page one level up.

I can list all child pages but i have problem to add an unique class to active child page to css differently.

<?php
global $post;
$direct_parent = $post->post_parent;
$args = array(
'post_type'      => 'page',
'posts_per_page' => -1,
'post_parent'    => $direct_parent, 
'order'          => 'ASC',
'orderby'        => 'menu_order'
);


$parent = new WP_Query( $args );

if ( $parent->have_posts() ) : ?>

<ul class='sidebar-list'>

<?php while ( $parent->have_posts() ) : $parent->the_post();
?>
<li>
<a href="<?php the_permalink()?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>

</ul>

<?php endif; wp_reset_query(); ?>
Share Improve this question asked Jul 11, 2019 at 7:23 matmat 1
Add a comment  | 

1 Answer 1

Reset to default -2

I made this by jQuery:

$('.sidebar-list li a').filter(function(){
  return this.href === location.href;
}).addClass('active');
发布评论

评论列表(0)

  1. 暂无评论