【问题标题】:Current Menu Item not working with $childpages当前菜单项不适用于 $childpages
【发布时间】:2016-03-22 11:47:01
【问题描述】:

我需要将“活动”或“当前菜单项”类添加到自定义子页面菜单。我已经尝试了下面的代码,但它似乎不起作用。

我浏览了谷歌,但找不到任何可行的方法!

<?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
    }
}
?>

提前致谢

【问题讨论】:

    标签: php wordpress class menu


    【解决方案1】:

    query_posts 更改为get_posts 你的&lt;a&gt; 标签的语法也无效。

    最后,不要使用不返回任何内容的$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
            }
        }
    

    【讨论】:

    • 非常感谢。最后一件事。你知道如何让它自动获取帖子父 id 而不必在每个模板中输入它吗?
    • 在自定义模板中,但我希望能够将其用作子页面的通用模板,如果有意义的话,不需要每次都输入 ID?
    • $post-&gt;post_parent 为您获取父帖子,以便您可以这样做 - $post_data = get_post($post-&gt;post_parent); $parent_ID = $post_data-&gt;ID;
    • 谢谢你,对不起,我应该把它放在代码的哪里?
    • 对不起,我的意思是我把你在上面那个 PHP 中所做的修改放在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多