当前菜单项与$childpages不起作用


Current Menu Item not working with $childpages

我需要将"active"或"current-menu-item"类添加到自定义子页面菜单中。我已经尝试了下面的代码,但它似乎不起作用。

我已经浏览了谷歌,但找不到任何有用的东西!

<?php
$childpages = query_posts('orderby=menu_order&order=asc&post_type=page&post_parent=35&posts_per_page=300');
if ($childpages)
{
    // Display the children content
    foreach ($childpages as $post)
    {
        setup_postdata($post)
        ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="mezzanine-sub-title <?php echo!empty($_GET['page_id']) && $_GET['page_id'] == $post->ID ? "active" : NULL ?>">
            <span><?php the_title(); ?></span>
            <a>
                <?php
                global $post;
                $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(5600, 1000), false, '');
                ?>
                <div class="mezzanine-sub-image" style="background: url(<?php echo $src[0]; ?> );border:<?php the_field('border'); ?>;">
                </div>
            </a>
        </a>
        <?php
    }
}
?>

提前致谢

query_posts更改为get_posts您的<a>标签的语法也无效。

最后,不使用不返回任何内容的$get使用wp_query

所以你的代码应该是:

$childpages = get_posts('orderby=menu_order&order=asc&post_type=page&post_parent=35&posts_per_page=300');
    if ($childpages)
    {
        // Display the children content
        foreach ($childpages as $post)
        {
            setup_postdata($post)
            ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="mezzanine-sub-title<?php if ( $post->ID == $wp_query->post->ID ) { echo ' active'; }?>">
                <span><?php the_title(); ?></span>
                    <?php
                    $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(5600, 1000), false, '');
                    ?>
                    <div class="mezzanine-sub-image" style="background: url(<?php echo $src[0]; ?> );border:<?php the_field('border'); ?>;">
                    </div>
            </a>
            <?php
        }
    }