根据步行者内部WordPress中的页面ID更改CSS


Change the CSS depending on page id in wordpress inside of a walker

目前我的函数中有.php

class Menu_With_Description extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "'t", $depth ) : '';
        $class_names = $value = '';
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
        $class_names = ' class="' . esc_attr( $class_names ) . '"';
        $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
        $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
        $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
        $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        //$item_output .= '<span class="sub">' . $item->description . '</span>';
        $item_output .= '<img src="' . $item->description . '" />';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

目前在我的导航菜单上,它完美运行,因为我也希望它与我的主题一起使用,但是我想让它只检测页面"关于我们"并解析不同的代码集而不是语句。我尝试编写一个排除函数,而不是在我的标头上的 walker 语句之前解析我自己的代码.php但效果不佳。

您可以使用

if (is_page($page_id)) { //page specific code here. }

$page_id显然是您的"关于我们"页面。

WordPress Codex