Wordpress 如何在自定义walker中返回页面id
我正在尝试使用自定义walker返回带有自定义导航菜单项的页面缩略图。我的全步行器在下面,看起来这部分Wordpress 如何在自定义walker中返回页面id,wordpress,Wordpress,我正在尝试使用自定义walker返回带有自定义导航菜单项的页面缩略图。我的全步行器在下面,看起来这部分 $thumbnail = ''; if( $id = has_post_thumbnail( (int)$item->object_id ) ) { $thumbnail = get_the_post_thumbnail( $id ); } 没有拉页面id,所以我没有缩略图。谢谢你的帮助 /* * Create HTML list of nav menu items. * Replac
$thumbnail = '';
if( $id = has_post_thumbnail( (int)$item->object_id ) ) {
$thumbnail = get_the_post_thumbnail( $id );
}
没有拉页面id,所以我没有缩略图。谢谢你的帮助
/*
* Create HTML list of nav menu items.
* Replacement for the native Walker, using the description.
*
* @see http://wordpress.stackexchange.com/q/14037/
* @author toscho, http://toscho.de
*/
class Thumbnail_Walker extends Walker_Nav_Menu
{
/**
* Start the element output.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. May be used for padding.
* @param array $args Additional strings.
* @return void
*/
function start_el(&$output, $item, $depth, $args)
{
$classes = empty ( $item->classes ) ? array () : (array) $item->classes;
$class_names = join(
' '
, apply_filters(
'nav_menu_css_class'
, array_filter( $classes ), $item
)
);
! empty ( $class_names )
and $class_names = ' class="'. esc_attr( $class_names ) . '"';
$output .= "<li id='menu-item-$item->ID' $class_names>";
$attributes = '';
! empty( $item->attr_title )
and $attributes .= ' title="' . esc_attr( $item->attr_title ) .'"';
! empty( $item->target )
and $attributes .= ' target="' . esc_attr( $item->target ) .'"';
! empty( $item->xfn )
and $attributes .= ' rel="' . esc_attr( $item->xfn ) .'"';
! empty( $item->url )
and $attributes .= ' href="' . esc_attr( $item->url ) .'"';
// insert thumbnail
// you may change this
$thumbnail = '';
if( $id = has_post_thumbnail( (int)$item->object_id ) ) {
$thumbnail = get_the_post_thumbnail( $id );
}
$title = apply_filters( 'the_title', $item->title, $item->ID );
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. '</a> '
. $args->link_after
. $thumbnail
. $args->after;
// Since $output is called by reference we don't need to return anything.
$output .= apply_filters(
'walker_nav_menu_start_el'
, $item_output
, $item
, $depth
, $args
);
}
}
/*
*创建导航菜单项的HTML列表。
*使用说明替换本机Walker。
*
*@见http://wordpress.stackexchange.com/q/14037/
*@作者托肖,http://toscho.de
*/
类缩略图\u Walker扩展了Walker\u导航菜单
{
/**
*启动元素输出。
*
*@param string$通过引用传递的输出。用于附加其他内容。
*@param object$item菜单项数据对象。
*@param int$菜单项的深度。可用于填充。
*@param array$args附加字符串。
*@返回无效
*/
函数开始(&$output、$item、$depth、$args)
{
$classes=空($item->classes)?数组():(数组)$item->classes;
$class\u name=加入(
' '
,应用过滤器(
“导航菜单css类”
,数组过滤器($classes),$item
)
);
!空($class\u名称)
和$class_names='class=“”.esc_attr($class_names)。”;
$output.=“”;
$attributes='';
!空($item->attr\u title)
和$attributes.='title=“”.esc_attr($item->attr_title)。”;
!空($item->target)
和$attributes.='target=“”.esc_attr($item->target)。”;
!空($item->xfn)
和$attributes.='rel=“”.esc_attr($item->xfn)。”;
!空($item->url)
和$attributes.='href=“”.esc_attr($item->url)。”;
//插入缩略图
//你可以改变这个
$thumbnail='';
如果($id=has_post_缩略图((int)$item->object_id)){
$thumbnail=获取\u post\u缩略图($id);
}
$title=apply_过滤器('the_title',$item->title,$item->ID);
$item_output=$args->before
. ""
.$args->link_之前
$title
. ' '
.$args->link\u之后
.$缩略图
.$args->after;
//由于$output是通过引用调用的,所以我们不需要返回任何内容。
$output.=应用过滤器(
“步行者导航菜单开始”
,$item_输出
,$item
,$depth
,$args
);
}
}
根据
看起来您可以在$args
之后将$id
参数添加到start\el
函数中,如下所示:
function start_el(&$output, $item, $depth, $args, $id)