Описание
Получает данные смежных (следующий, предыдущий) постов.
Использование
<?php get_adjacent_post( $in_same_cat, $excluded_categories, $previous ) ?>
Параметры
По умолчанию: false
По умолчанию: ''
По умолчанию: true
Возвращаемые данные
(объект) Пример элементов объекта, которые возвращает функция:
[ID] => 25
[post_author] => 1
[post_date] => 2010-04-05 04:24:04
[post_date_gmt] => 2010-04-05 00:24:04
[post_content] => Если у вас внезапно есть, что обсудить, пройдитесь клавиатурой по форме ниже:
[post_title] => Обратная связь
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => contacts
[to_ping] =>
[pinged] =>
[post_modified] => 2010-08-16 17:41:20
[post_modified_gmt] => 2010-08-16 13:41:20
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://wp-kama.ru/contacts
[menu_order] => 0
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
Примеры
Выведем на экран ссылку на следующий пост:
$next_post = get_adjacent_post(0,'',0); echo '<a href="'. get_permalink($next_post->ID) .'">'. $next_post->post_title .'</a>';
Выведем на экран ссылку на предыдущий пост:
$next_post = get_adjacent_post(); echo '<a href="'. get_permalink($next_post->ID) .'">'. $next_post->post_title .'</a>';
Фильтры
На заметку
- Использует глобальную переменную: $post
- Использует глобальную переменную: (объект) $wpdb
get_adjacent_post()
из файла: /wp-includes/link-template.php WP 3.3.2function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) {
global $post, $wpdb;
if ( empty( $post ) )
return null;
$current_post_date = $post->post_date;
$join = '';
$posts_in_ex_cats_sql = '';
if ( $in_same_cat || ! empty( $excluded_categories ) ) {
$join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
if ( $in_same_cat ) {
$cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
$join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
}
$posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
if ( ! empty( $excluded_categories ) ) {
if ( ! is_array( $excluded_categories ) ) {
// back-compat, $excluded_categories used to be IDs separated by " and "
if ( strpos( $excluded_categories, ' and ' ) !== false ) {
_deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
$excluded_categories = explode( ' and ', $excluded_categories );
} else {
$excluded_categories = explode( ',', $excluded_categories );
}
}
$excluded_categories = array_map( 'intval', $excluded_categories );
if ( ! empty( $cat_array ) ) {
$excluded_categories = array_diff($excluded_categories, $cat_array);
$posts_in_ex_cats_sql = '';
}
if ( !empty($excluded_categories) ) {
$posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
}
}
}
$adjacent = $previous ? 'previous' : 'next';
$op = $previous ? '<' : '>';
$order = $previous ? 'DESC' : 'ASC';
$join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
$query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
$query_key = 'adjacent_post_' . md5($query);
$result = wp_cache_get($query_key, 'counts');
if ( false !== $result )
return $result;
$result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
if ( null === $result )
$result = '';
wp_cache_set($query_key, $result, 'counts');
return $result;
}Связанные Функции
Ещё из раздела
Смотрите также: Функции WordPress и Теги Шаблона.
