带有自定义菜单的缩略图的WordPress自定义漫游

时间:2011-05-01 作者:javy

我还是PHP的新手,所以非常感谢您的帮助。一般来说,我发现抄本非常有用,但似乎定制步行器可能超出了它的范围。

我想有一个自定义导航菜单,我在一个主题显示缩略图。据我所知,我需要创建一个自定义助行器来实现这一点。

我把这个

wp_nav_menu( array( \'container_class\' => \'menu-stamp\', \'theme_location\' =>\'stamp-menu\' , \'walker\' => new Thumbnail_Walker) ); 
在“我的主题”菜单位置,并在下面的“项目输出”部分下方插入3个缩略图行

/*
 * 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
    );
   }
}
我在缩略图代码的末尾得到了一个意外的}错误,所以这是错误的,但我不知道为什么。

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

添加分号; 之后get_the_post_thumbnail( $id ):

get_the_post_thumbnail( $id );

SO网友:Andrew

对于未来的访客,您需要改变

  $thumbnail = \'\';
    if( $id = has_post_thumbnail( (int)$item->object_id ) ) {
  $thumbnail = get_the_post_thumbnail( $id );
  }

  $thumbnail = \'\';
    if ( has_post_thumbnail( $item->object_id ) ) {
  $thumbnail = get_the_post_thumbnail( $item->object_id );
  }
正确答案已发布here

结束

相关推荐

Menu API not switching menus?

我正在使用菜单API,我想切换到其他菜单,但出于某种原因,它保留了第一个菜单这是我的密码在函数中。php add_action( \'init\', \'register_my_menus\',10 ); function register_my_menus() { register_nav_menu(\'main-navigation\', \'Main Navigation\'); } 下面是我的主题文件(header.ph