【问题标题】:adding display none based on post status in walker menu根据 walker 菜单中的帖子状态添加不显示
【发布时间】:2018-02-27 14:01:12
【问题描述】:

我正在尝试将 display:none 添加到函数中的walker 菜单,但它不起作用。

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

任何想法我哪里出错了?

            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().

【问题讨论】:

  • 如果以下答案对您的问题有所帮助,请考虑为有帮助的答案点赞,以便其他用户知道这可能对他们有所帮助。如果它对您有用,您也应该接受它,因此它在网站上被标记为已解决。见What should I do when someone answers my question?

标签: php wordpress wp-nav-walker


【解决方案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

【讨论】:

  • 这似乎不起作用,也许您需要查看整个文件:这是整个文件的 URL:codeshare.io/anPrzV
【解决方案2】:

您使用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
    [...]

}


注意:其他状态的帖子

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

【讨论】:

  • 这对我不起作用,所以我已经发布了整个文件。它似乎没有获取页面 id。
  • @Jane 您的代码共享适用于 Avada 主题中的 NavWalker。您自己的包含问题中代码的函数在哪里?如果您不向我们展示,我们将无法帮助您修复损坏的代码!
  • 它没有损坏,我正在尝试调整它以不显示设置草稿的链接。该文件位于我的子主题中。
  • @Jane 我很困惑......你在问题中发布了代码并说你说它不起作用,所以我认为这是我们试图修复的代码。那么代码在哪里?
猜你喜欢
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多