Make WordPress Core

Changeset 34367


Ignore:
Timestamp:
09/21/2015 04:31:19 PM (9 years ago)
Author:
obenland
Message:

Introduce template functions for comment navigation:

  • get_the_comments_navigation() and the_comments_navigation() for navigation to the next and previous page of comments.
  • get_the_comments_pagination() and the_comments_pagination() for paginated navigation between pages of comments. Uses paginate_comments_links().

This reduces the need for themes to define their own sets of comment navigation
functions, like Twenty Fifteen and others.
Completes the navigation template tag API started in #29808 for posts.

Fixes #30589.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r34363 r34367  
    27072707
    27082708/**
     2709 * Returns navigation to next/previous set of comments when applicable.
     2710 *
     2711 * @since 4.4.0
     2712 *
     2713 * @param array $args {
     2714 *     Optional. Default comments navigation arguments.
     2715 *
     2716 *     @type string $prev_text          Anchor text to display in the previous comments link. Default 'Older comments'.
     2717 *     @type string $next_text          Anchor text to display in the next comments link. Default 'Newer comments'.
     2718 *     @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
     2719 * }
     2720 * @return string Markup for comments links.
     2721 */
     2722function get_the_comments_navigation( $args = array() ) {
     2723    $navigation = '';
     2724
     2725    // Are there comments to navigate through?
     2726    if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) {
     2727        $args = wp_parse_args( $args, array(
     2728            'prev_text'          => __( 'Older comments' ),
     2729            'next_text'          => __( 'Newer comments' ),
     2730            'screen_reader_text' => __( 'Comments navigation' ),
     2731        ) );
     2732
     2733        $prev_link = get_previous_comments_link( $args['prev_text'] );
     2734        $next_link = get_next_comments_link( $args['next_text'] );
     2735
     2736        if ( $prev_link ) {
     2737            $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
     2738        }
     2739
     2740        if ( $next_link ) {
     2741            $navigation .= '<div class="nav-next">' . $next_link . '</div>';
     2742        }
     2743
     2744        $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'] );
     2745    }
     2746
     2747    return $navigation;
     2748}
     2749
     2750/**
     2751 * Displays navigation to next/previous set of comments when applicable.
     2752 *
     2753 * @since 4.4.0
     2754 *
     2755 * @param array $args See {@see get_the_comments_navigation()} for available arguments.
     2756 */
     2757function the_comments_navigation( $args = array() ) {
     2758    echo get_the_comments_navigation( $args );
     2759}
     2760
     2761/**
     2762 * Returns a paginated navigation to next/previous set of comments,
     2763 * when applicable.
     2764 *
     2765 * @since 4.4.0
     2766 *
     2767 * @see paginate_comments_links()
     2768 *
     2769 * @param array $args {
     2770 *     Optional. Default pagination arguments.
     2771 *
     2772 *     @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
     2773 * }
     2774 * @return string Markup for pagination links.
     2775 */
     2776function get_the_comments_pagination( $args = array() ) {
     2777    $navigation = '';
     2778    $args       = wp_parse_args( $args, array(
     2779        'screen_reader_text' => __( 'Comments navigation' ),
     2780    ) );
     2781    $args['echo'] = false;
     2782
     2783    // Make sure we get plain links, so we get a string we can work with.
     2784    $args['type'] = 'plain';
     2785
     2786    $links = paginate_comments_links( $args );
     2787
     2788    if ( $links ) {
     2789        $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] );
     2790    }
     2791
     2792    return $navigation;
     2793}
     2794
     2795/**
     2796 * Displays a paginated navigation to next/previous set of comments,
     2797 * when applicable.
     2798 *
     2799 * @since 4.4.0
     2800 *
     2801 * @param array $args See {@see get_the_comments_pagination()} for available arguments.
     2802 */
     2803function the_comments_pagination( $args = array() ) {
     2804    echo get_the_comments_pagination( $args );
     2805}
     2806
     2807/**
    27092808 * Retrieve the Press This bookmarklet link.
    27102809 *
Note: See TracChangeset for help on using the changeset viewer.