Make WordPress Core

Changeset 25327


Ignore:
Timestamp:
09/10/2013 10:11:53 PM (11 years ago)
Author:
wonderboymusic
Message:

Introduce get_previous_post_link(), get_next_post_link(), and get_adjacent_post_link(). Allows developers to retrieve the value without echoing.

Props yoavf, markjaquith, SergeyBiryukov.
Fixes #17302.

File:
1 edited

Legend:

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

    r25319 r25327  
    13381338}
    13391339
     1340/*
     1341 * Get previous post link that is adjacent to the current post.
     1342 *
     1343 * @since 3.7.0
     1344 *
     1345 * @param string $format Optional. Link anchor format.
     1346 * @param string $link Optional. Link permalink format.
     1347 * @param bool $in_same_cat Optional. Whether link should be in same category.
     1348 * @param string $excluded_categories Optional. Excluded categories IDs.
     1349 * @return string
     1350 */
     1351function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
     1352    return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, true );
     1353}
     1354
    13401355/**
    13411356 * Display previous post link that is adjacent to the current post.
    13421357 *
    13431358 * @since 1.5.0
     1359 * @uses get_previous_post_link()
    13441360 *
    13451361 * @param string $format Optional. Link anchor format.
     
    13481364 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    13491365 */
    1350 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    1351     adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
     1366function previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
     1367    echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_categories );
     1368}
     1369
     1370/**
     1371 * Get previous post link that is adjacent to the current post.
     1372 *
     1373 * @since 3.7.0
     1374 * @uses get_next_post_link()
     1375 *
     1376 * @param string $format Optional. Link anchor format.
     1377 * @param string $link Optional. Link permalink format.
     1378 * @param bool $in_same_cat Optional. Whether link should be in same category.
     1379 * @param string $excluded_categories Optional. Excluded categories IDs.
     1380 * @return string
     1381 */
     1382function get_next_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
     1383    return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, false );
    13521384}
    13531385
     
    13621394 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    13631395 */
    1364 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    1365     adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
    1366 }
    1367 
    1368 /**
    1369  * Display adjacent post link.
     1396function next_post_link( $format = '%link »', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
     1397     echo get_next_post_link( $format, $link, $in_same_cat, $excluded_categories );
     1398}
     1399
     1400/**
     1401 * Get adjacent post link.
    13701402 *
    13711403 * Can be either next post link or previous.
    13721404 *
    1373  * @since 2.5.0
     1405 * @since 3.7.0
    13741406 *
    13751407 * @param string $format Link anchor format.
     
    13781410 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    13791411 * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
    1380  */
    1381 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
     1412 * @return string
     1413 */
     1414function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
    13821415    if ( $previous && is_attachment() )
    13831416        $post = get_post( get_post()->post_parent );
     
    14071440    $adjacent = $previous ? 'previous' : 'next';
    14081441
    1409     echo apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
     1442    return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
     1443}
     1444
     1445/**
     1446 * Display adjacent post link.
     1447 *
     1448 * Can be either next post link or previous.
     1449 *
     1450 * @since 2.5.0
     1451 * @uses get_adjacent_post_link()
     1452 *
     1453 * @param string $format Link anchor format.
     1454 * @param string $link Link permalink format.
     1455 * @param bool $in_same_cat Optional. Whether link should be in a same category.
     1456 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1457 * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
     1458 * @return string
     1459 */
     1460function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
     1461    echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, $previous );
    14101462}
    14111463
Note: See TracChangeset for help on using the changeset viewer.