获取菜单链接的上次修改日期

时间:2017-05-24 作者:Malik Drako

如何获取菜单链接目标页面的发布日期和/或上次修改日期?我想在数据属性中包含此值。

对于帖子和页面的链接,应该是帖子/页面的最后修改日期要获取帖子存档的链接,应该是该存档中最近帖子的日期我尝试使用$atts[\'data-modified\'] = $item->post_modified; 在自定义菜单漫游器中,但这只是获得菜单项本身的最后修改日期。

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

这个nav_menu_link_attributes 过滤器可以用来实现这一点,所以不需要步行器。下面是一个示例,其中包含一些注释来解释正在发生的事情。

add_filter( \'nav_menu_link_attributes\', \'wpse_nav_menu_link_attributes\', 10, 4 );
/**
 * Filters the HTML attributes applied to a menu item\'s anchor element.
 *
 * Adds data-modified attribute to links.
 * - For single post types, the data-modified attribute\'s value stores the
 *   modified date for that post.
 *
 * - For post type archives, the data-modified attribute\'s value stores the
 *   modified date for the most recently published post in the archive.
 *
 * - For taxonomies, the data-modified attribute\'s value stores the
 *   modified date for the most recently published post assigned to the taxonomy term.
 *
 * ----
 *
 * Docs below via WordPress core.
 *
 * @param array $atts {
 *     The HTML attributes applied to the menu item\'s `<a>` element, empty strings are ignored.
 *
 *     @type string $title  Title attribute.
 *     @type string $target Target attribute.
 *     @type string $rel    The rel attribute.
 *     @type string $href   The href attribute.
 * }
 * @param WP_Post  $item  The current menu item.
 * @param stdClass $args  An object of wp_nav_menu() arguments.
 * @param int      $depth Depth of menu item. Used for padding.
 */
function wpse_nav_menu_link_attributes( $atts, $item, $args, $depth ) {

    // Get all post meta for this menu item. Post, Page, and other info is
    // stored in the menu item post\'s meta data.
    $item_meta = get_post_meta( $item->ID );

    // Modify date format to suit your preferneces.
    // http://php.net/manual/en/function.date.php
    $date_format = \'l F j, Y\';

    // Bail if there is no meta.
    if ( ! $item_meta ) {
        return $atts;
    }

    $menu_item_type = false;
    // Bail if _menu_item_type is unavailable.
    if ( ! isset( $item_meta[\'_menu_item_type\'][0] ) ) {
        return $atts;
    } else {
        $menu_item_type = $item_meta[\'_menu_item_type\'][0]; // For readability.
    }

    // Handle post types and post type archives accordingly.
    switch ( $menu_item_type ) {

        /**
         * Handle single post type menu item type. E.g.: posts/pages/etc.
         */
        case \'post_type\' :

            // Bail if _menu_item_object_id is unavailable.
            if ( ! isset( $item_meta[\'_menu_item_object_id\'][0] ) ) {               
                return $atts;
            } else {
                $menu_item_object_id = $item_meta[\'_menu_item_object_id\'][0]; // For readability.
            }

            // Handle special case when this is the page for posts.
            $show_on_front  = get_option( \'show_on_front\' ); // \'page\' for static front page, \'posts\' for latest posts.
            $page_for_posts = get_option( \'page_for_posts\' ); 
            if ( \'page\' === $show_on_front && $page_for_posts === $menu_item_object_id ) {
                // Get the most recent post of the post post type. ...Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.
                $recent_post = get_posts( [
                    \'posts_per_page\' => 1,
                    \'post_type\'      => \'post\',
                    \'orderby\'        => \'date\',
                    \'order\'          => \'DESC\',                 
                    \'post_status\'    => \'publish\',
                ] );

                // Add the data-modified attribute and set the date via the most recent post.
                if ( is_array( $recent_post ) ) {
                    $atts[\'data-modified\'] = esc_attr( get_the_modified_date(
                            $date_format,
                            $recent_post[0]
                    ) );
                }

            // Handle typical case for single post/page/custom post type.
            } else { 
                // Add the data-modified attribute and set the value to post\'s modified date.
                $atts[\'data-modified\'] = esc_attr( get_the_modified_date(
                        $date_format, 
                        get_post( $menu_item_object_id )
                ) );
            }
        break;

        /**
         *  Handle post type archive menu item type.
         */
        case \'post_type_archive\' :

            // Bail if _menu_item_object is unavailable.
            if ( ! isset( $item_meta[\'_menu_item_object\'][0] ) ) {              
                return $atts;
            } else {
                $menu_item_object = $item_meta[\'_menu_item_object\'][0]; // For readability.
            }

            // Get the most recent post of this archive\'s type.
            $recent_post = get_posts( [
                \'posts_per_page\' => 1,
                \'post_type\'      => $menu_item_object,
                \'orderby\'        => \'date\',
                \'order\'          => \'DESC\',                 
                \'post_status\'    => \'publish\',
            ] );

            // Add the data-modified attribute and set the date via the most recent post.
            if ( is_array( $recent_post ) ) {
                $atts[\'data-modified\'] = esc_attr( get_the_modified_date(
                        $date_format,
                        $recent_post[0]
                ) );
            }
        break;

        /**
         *  Handle taxonomy menu item type.
         */
        case \'taxonomy\' :

            // Bail if _menu_item_object is unavailable.
            if ( ! isset( $item_meta[\'_menu_item_object\'][0] ) ) {              
                return $atts;
            } else {
                $menu_item_object = $item_meta[\'_menu_item_object\'][0];
            }

            // Bail if _menu_item_object_id is unavailable.
            if ( ! isset( $item_meta[\'_menu_item_object_id\'][0] ) ) {               
                return $atts;
            } else {
                $menu_item_object_id = $item_meta[\'_menu_item_object_id\'][0];
            }

            // Get the most recent post using this taxonomy and term.
            $recent_post = get_posts( [
                \'posts_per_page\' => 1,
                \'post_type\'      => \'any\',
                \'orderby\'        => \'date\',
                \'order\'          => \'DESC\',
                \'post_status\'    => \'publish\',
                \'tax_query\' => array (
                    [
                        \'taxonomy\' => $menu_item_object,
                        \'terms\'    => $menu_item_object_id,
                        \'field\'    => \'id\',
                    ],
                )       
            ] );            

            // Add the data-modified attribute and set the date via the most recent post using this taxonomy and term.
            if ( is_array( $recent_post ) ) {
                $atts[\'data-modified\'] = esc_attr( get_the_modified_date(
                        $date_format,
                        $recent_post[0]
                ) );
            }
        break;

    } // end switch

    return $atts;
}
注意:菜单中的每个链接都存储为名为nav_menu_item. 附加信息(如菜单项post指向的post ID)存储在post meta中。这个post_id 元的字段将指向nav_menu_item\'s post id。

结束

相关推荐

Problem with custom menus

在菜单上,我说的是自定义菜单,我有一个带有几列的页脚,每列都有一个项目列表,每列菜单都需要从wp admin上的菜单选项进行更新,所以我创建了关于函数的部分。调用菜单并在页脚上添加每个菜单的php。php文档,类似于:<?php wp_nav_menu( array(\'menu\' => \'f-1\', \'menu_class\' => \'\', \'container\' => \'nav\' )); ?> <?php wp_nav_menu( arr