1

我正在尝试将 display:none 添加到功能中的步行者菜单中,但它不起作用。

基本上,如果帖子状态是“草稿”,那么......

有什么想法我哪里出错了吗?

            if ( isset( $item->classes[0] ) && ! empty( $item->classes[0] ) ) {
                $custom_class_data = ' data-classes="' . $item->classes[0] . '"';
            }

            $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . $class_columns . '"' : '';

            $style = $style ? ' style="' . esc_attr( $style ) . '"' : '';

            $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
            $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
            global $post;
            $page_id = $post->ID;; // example id of your page 
            $page = get_page( $page_id );
            if( $page->post_status == 'draft' ) : 
            $output .= '<li role="menuitem" style="display: none"' . $id . ' ' . $class_names . ' ' . $column_width . $custom_class_data . $style . ' >';
            else:
            $output .= '<li role="menuitem" ' . $id . ' ' . $class_names . ' ' . $column_width . $custom_class_data . $style . ' >';
            endif;
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

        } // End if().
4

2 回答 2

1

global $post用于检查状态,但这是指当前页面,因此无法正常工作。

在导航中隐藏菜单项

我假设这是在start_el函数中,并且$item传入的变量具有要添加到菜单的页面/帖子的详细信息,因此您可以像这样直接检查状态:

if( $item->post_status == 'draft' )

您更新的代码将是:

if ( isset( $item->classes[0] ) && ! empty( $item->classes[0] ) ) {
    $custom_class_data = ' data-classes="' . $item->classes[0] . '"';
}

$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . $class_columns . '"' : '';

$style = $style ? ' style="' . esc_attr( $style ) . '"' : '';

$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

if( $item->post_status == 'draft' ) : 
    $output .= '<li role="menuitem" style="display: none"' . $id . ' ' . $class_names . ' ' . $column_width . $custom_class_data . $style . ' >';
else:
    $output .= '<li role="menuitem" ' . $id . ' ' . $class_names . ' ' . $column_width . $custom_class_data . $style . ' >';
endif;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );


替代方案:不要在导航中包含链接

由于它们只是草稿页面,因此您根本不应该包含它们 -display:none只是将它们从显示中隐藏起来,它们仍然包含在 HTML 中,因此通过检查 HTML 可以很容易地看到这些 url,Google 仍然会尝试跟踪链接.

我建议将状态作为您函数中的第一件事进行检查,如果是草稿,则返回不做任何事情,否则正常处理该项目。

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    // don't add page/post if it is draft
    if( $item->post_status == 'draft' ) return;

    // if its not draft, process as normal
    [...]

}


注:其他状态的帖子

草稿帖子通常不包括在内,因此我假设您正在使用自定义代码来显示帖子/页面。future在这种情况下,不要忘记状态为private或的帖子trash

于 2017-09-19T11:58:53.947 回答
1

您已经有$style存储样式的变量,我会这样做:

// code...
if( $page->post_status == 'draft' )  {
    $style = 'display: none;';
    // if $style was previously initialized
    // $style += 'display: none;';
}
$style = $style ? ' style="' . esc_attr( $style ) . '"' : '';
$output .= '<li role="menuitem"' . $id . ' ' . $class_names . ' ' . $column_width . $custom_class_data . $style . ' >';
// rest of the code
于 2017-09-18T23:14:46.150 回答