Making WordPress.org


Ignore:
Timestamp:
10/21/2014 04:53:25 PM (12 years ago)
Author:
coffee2code
Message:

Code Reference: make handbook page navigation links abide by menu ordering. See #631

Introduces and uses wporg_developer_post_nav_via_menu() to determine previous/next linking according to ordering defined in a specified menu (which is how Chapters are ordered).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php

    r875 r922  
    7373        endif;
    7474
     75        if ( ! function_exists( 'wporg_developer_post_nav_via_menu' ) ) :
     76                /**
     77                 * Outputs previous and/or next post navigation links using the
     78                 * specified menu to inform navigation ordering.
     79                 *
     80                 * @param  string $menu_name The name of the menu to use for nav ordering.
     81                 */
     82                function wporg_developer_post_nav_via_menu( $menu_name ) {
     83                        // Get the items for the specified menu
     84                        if ( ! $menu_items = wp_get_nav_menu_items( $menu_name ) ) {
     85                                return;
     86                        }
     87
     88                        // Get ids for all menu objects
     89                        $menu_ids = wp_list_pluck( $menu_items, 'object_id' );
     90
     91                        // Get current post
     92                        if ( ! $post = get_post() ) {
     93                                return;
     94                        }
     95
     96                        // Index of current post in menu. Return if not in menu.
     97                        $i = array_search( $post->ID, $menu_ids );
     98                        if ( false === $i ) {
     99                                return;
     100                        }
     101
     102                        // Find the previous post (note: preview menu item may not be a post)
     103                        $previous = null;
     104                        for ( $n = $i-1; $n >= 0; $n-- ) {
     105                                if ( isset( $menu_items[ $n ] ) && is_a( $menu_items[ $n ], 'WP_Post' ) ) {
     106                                        $previous = $menu_items[ $n ];
     107                                        break;
     108                                }
     109                        }
     110
     111                        // Find the next post (note: next menu item may not be a post)
     112                        $next = null;
     113                        $max = count( $menu_items );
     114                        for ( $n = $i+1; $n < $max; $n++ ) {
     115                                if ( isset( $menu_items[ $n ] ) && is_a( $menu_items[ $n ], 'WP_Post' ) ) {
     116                                        $next = $menu_items[ $n ];
     117                                        break;
     118                                }
     119                        }
     120
     121                        if ( ! $previous && ! $next ) {
     122                                return;
     123                        }
     124                        ?>
     125
     126                        <nav class="navigation post-navigation" role="navigation">
     127                                <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'wporg' ); ?></h1>
     128                                <div class="nav-links">
     129
     130                                <?php
     131                                if ( $previous ) {
     132                                        printf( '<a href="%s" rel="previous"><span class="meta-nav">&larr;</span> %s</a>',
     133                                                esc_url( $previous->url ),
     134                                                esc_html( $previous->title )
     135                                        );
     136                                }
     137
     138                                if ( $next ) {
     139                                        printf( '<a href="%s" rel="next">%s <span class="meta-nav">&rarr;</span></a>',
     140                                                esc_url( $next->url ),
     141                                                esc_html( $next->title )
     142                                        );
     143                                }
     144                                ?>
     145
     146                                </div>
     147                                <!-- .nav-links -->
     148                        </nav><!-- .navigation -->
     149                <?php
     150                }
     151        endif;
     152
    75153        if ( ! function_exists( 'wporg_developer_user_note' ) ) :
    76154                /**
     
    99177
    100178                                        <footer class="comment-meta">
     179                                        <?php DevHub_User_Submitted_Content_Voting::show_voting(); ?>
    101180                                                <div class="comment-author vcard">
    102181                                                        <span class="comment-author-attribution">
     
    125204                                                                </time>
    126205                                                        </a>
    127                                                         <?php edit_comment_link( __( 'Edit', 'wporg' ), '<span class="edit-link">', '</span>' ); ?>
     206                                                        <?php edit_comment_link( __( 'Edit', 'wporg' ), '<span class="edit-link">&mdash; ', '</span>' ); ?>
    128207                                                </div>
    129208                                                <!-- .comment-metadata -->
Note: See TracChangeset for help on using the changeset viewer.