【发布时间】:2015-07-16 10:21:04
【问题描述】:
我有一个包含多个下拉菜单的菜单。 我想在菜单下拉菜单中的一个或多个帖子的链接下方或旁边显示特色图片。可能吗? 我已在此消息中附加了一张图片。 我不想知道如何设计它或类似的东西。 假设我有 “Siguranta”,我想在下方显示该帖子的特色图片,并在图片下方显示“阅读更多”链接。非常感谢。
【问题讨论】:
标签: php jquery html css wordpress
我有一个包含多个下拉菜单的菜单。 我想在菜单下拉菜单中的一个或多个帖子的链接下方或旁边显示特色图片。可能吗? 我已在此消息中附加了一张图片。 我不想知道如何设计它或类似的东西。 假设我有 “Siguranta”,我想在下方显示该帖子的特色图片,并在图片下方显示“阅读更多”链接。非常感谢。
【问题讨论】:
标签: php jquery html css wordpress
为特定菜单添加过滤器
add_filter('wp_nav_menu_args', 'add_filter_to_menus');
function add_filter_to_menus($args) {
// You can test agasint things like $args['menu'], $args['menu_id'] or $args['theme_location']
if( $args['theme_location'] == 'header_menu') {
add_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
}
}
过滤器菜单
function filter_menu_items($item)
{
if ($item->type == 'taxonomy') {
// For category menu items
$cat_base = get_option('category_base');
if (empty($cat_base)) {
$cat_base = 'category';
}
// Get the path to the category (excluding the home and category base parts of the URL)
$cat_path = str_replace(home_url() . '/' . $cat_base, '', $item->url);
// Get category and image ID
$cat = get_category_by_path($cat_path, true);
$thumb_id = get_term_meta($cat->term_id, '_term_image_id', true); // I'm using the 'Simple Term Meta' plugin to store an attachment ID as the featured image
} else {
// Get post and image ID
$post_id = url_to_postid($item->url);
$thumb_id = get_post_thumbnail_id($post_id);
}
if (!empty($thumb_id)) {
// Make the title just be the featured image.
$item->title = wp_get_attachment_image($thumb_id, 'poster');
}
return $item;
}
然后您想删除您在开始时应用的过滤器,以便处理的下一个菜单不使用上面在 filter_menu_items() 中定义的相同 HTML。
移除过滤器
add_filter('wp_nav_menu_items','remove_filter_from_menus', 10, 2);
function remove_filter_from_menus( $nav, $args ) {
remove_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
return $nav;
}
【讨论】:
所以,我会回答我自己的问题。我终于用这段代码做到了:
// get featured image
$thumbnail = get_the_post_thumbnail( $item->object_id );
//display featured image
$item_output .= $thumbnail;
不得不提的是,我在 walker 类中使用了这段代码。
【讨论】: