如何检查某个对象ID是否为页面或帖子(从页面/帖子本身外部)


How to check if a certain object ID is a page or post (from outside of the page/post itself)

我有一个导航菜单循环,它使用一个自定义walker输出:

<ul>
<li id="menu-item-2" class="menu-item-object-category">stuff</li>
<li id="menu-item-3" class="menu-item-object-category">stuff</li>
...
etc
And if a page is in the menu, it outputs:
<li id="menu-item-4" class="menu-item-object-page">stuff</li>
</ul>

显然,wordpress在某个地方检测到菜单中的条目是一个类别或一个页面,并分配适当的类。我怎么能在我的助行器上做同样的检查呢?

我只是想做一个

If ($item->object_id = page) { // special code}

我尝试了is_page(),但意识到这是一个布尔函数,用于检测当前页面是否为页面。

是否有简单的方法来检查步行者的每个输出页面?

这是我的walker代码:

class Custom_Walker 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 .= $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 );
    }
}   

谢谢

可以使用get_post函数检查类型,例如:

<?php
$current_post = get_post($current_post_id);
if ($current_post->type == 'post') {
  //Do the stuff with post
}
elseif ($current_post->type == 'page') {
  //Do the stuff with page
}
else {
  //Do the stuff with other post types
}