php - WordPress 菜单帖子特色图片

标签 php jquery html css wordpress

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

enter image description here

最佳答案

向特定菜单添加过滤器

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

关于php - WordPress 菜单帖子特色图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31451539/

相关文章:

php 使用参数或括号从 popen() 激发一个 php 文件

php - 为什么我的 MySQL BETWEEN 运算符不起作用?

javascript - 管理模板标记 block 的策略

javascript - 同时独立的 AJAX 请求

android - 在android webview中加载静态页面

html - Jquery Mobile 中复选框部分的背景颜色

css - 如何指定内联 img <header> 标签?

php - Laravel 与谷歌云数据存储

html - CSS:以均匀的空间垂直分布 <div> 或 <li>

php - 为什么不支持将 PHP 函数导入当前命名空间