wordpress - 如何在自定义walker中返回页面id

标签 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.
* 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
);
}
}

最佳答案

根据http://phpdoc.wordpress.org 看起来您可以将 $id 参数添加到 $args 之后的 start_el 函数中,如下所示:

function start_el(&$output, $item, $depth, $args, $id)

关于wordpress - 如何在自定义walker中返回页面id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5893036/

相关文章:

php - 如何在 WooCommerce 结帐页面上重新排序其他字段

javascript - 使用自定义 WordPress 主题配置 keywords.js

javascript - 单击按钮时的 update_order_review( )

html - 为单个页面覆盖主题的 CSS

php - 在 wordpress 中返回 true 和 false 的 Ajax 函数问题

javascript - 仅当向左滑动时才转换 Logo ,而不是返回到原始位置时

javascript - jQuery scroll() 事件不会传播

mysql - 如何在 bash 脚本中将变量传递给 SSH 命令

php - 博客文章 WordPress 定制

javascript - 在 WordPress 后端的 TinyMCE 模态窗口内调用 PHP 函数