【问题标题】:WordPress menu post featured imageWordPress 菜单帖子特色图片
【发布时间】:2015-07-16 10:21:04
【问题描述】:

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

【问题讨论】:

    标签: php jquery html css wordpress


    【解决方案1】:

    为特定菜单添加过滤器

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

    【讨论】:

    • 那么,我怎样才能在我的菜单代码中实现它呢?谢谢。
    • 这对我毫无帮助。我想知道怎么做,我不想买。
    • 我有 ubermenu 你喜欢吗?
    • 我不能说我没有。感谢您的帮助,但这不是我要找的。我只是想用我自己的菜单在我的 wrpdress 菜单中添加他们的特色图片。
    • 我已经在我的谷歌搜索中看到了这个。我不想显示图标。我想显示特色图片。
    【解决方案2】:

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

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

    不得不提的是,我在 walker 类中使用了这段代码。

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      相关资源
      最近更新 更多