WordPress菜单发布精选图片

时间:2015-07-16 10:21:05

标签: php jquery html css wordpress

我有一个包含多个下拉列表的菜单。 我想在菜单下拉列表中显示下方或旁边的帖子或帖子的特色图片。可能吗? 我已将图像附加到此消息中。 我不想知道如何设计它或类似的东西。 所以,假设我有“Siguranta”,我想在下面显示该帖子的特色图片,并在图片下方显示“阅读更多”链接。非常感谢提前。

enter image description here

2 个答案:

答案 0 :(得分:3)

将过滤器添加到特定菜单

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;
    }

答案 1 :(得分:0)

所以,我会回答我自己的问题。我终于用这段代码做了:

  // get featured image
  $thumbnail = get_the_post_thumbnail( $item->object_id );

  //display featured image
  $item_output .= $thumbnail;

我必须提到我在walker类中使用了这段代码。

相关问题