Php 如何检查某个对象ID是否为页面或帖子(从页面/帖子本身外部)
我有一个导航菜单循环,它使用一个自定义walker,输出:Php 如何检查某个对象ID是否为页面或帖子(从页面/帖子本身外部),php,wordpress,Php,Wordpress,我有一个导航菜单循环,它使用一个自定义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: <
<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>
我尝试了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 );
}
}
class Custom\u Walker扩展了Walker\u导航菜单
{
函数开始(&$output、$item、$depth、$args){
全局$wp_查询;
$indent=($depth)?str_repeat(“\t”,$depth):”;
$class_name=$value='';
$classes=空($item->classes)?array():(array)$item->classes;
$class_name=join(“”,应用_过滤器('nav_menu_css_class',数组_过滤器($classes),$item));
$class_names='class=“”.esc_attr($class_names)。”;
$output.=$indent.'';
$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_输出='';
$item\u output.=$args->link\u before。应用\u过滤器('the\u title',$item->title,$item->ID)。$args->link\u before;
$item_输出='';
$item_output.=$args->after;
$output.=apply_filters('walker_nav_menu_start_el'、$item_output、$item、$depth、$args);
}
}
谢谢您可以使用函数并检查类型,例如:
<?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
}
必须是if($current\u post->post\u type=='page'),但除此之外,这正是我需要的,谢谢!
<?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
}