示例#1
0
文件: Post.php 项目: laraish/laraish
 /**
  * Get the related posts of this post by using YARPP
  *
  * @param array $args
  *
  * @return mixed
  */
 public function relatedPosts($args = [])
 {
     $args = array_merge(['limit' => 6], $args);
     return array_map(function ($post) {
         return new static($post);
     }, yarpp_get_related($args, $this->pid));
 }
示例#2
0
 /**
  * Use a plugin to get related posts, or fall back to
  * simply fetching some posts from the same category.
  */
 public static function get_related_posts()
 {
     $post = get_post();
     $posts_per_page = apply_filters('semicolon_related_posts_per_page', 4);
     // Support for the Yet Another Related Posts Plugin
     if (function_exists('yarpp_get_related')) {
         $related = yarpp_get_related(array('limit' => $posts_per_page), $post->ID);
         return new WP_Query(array('post__in' => wp_list_pluck($related, 'ID'), 'posts_per_page' => $posts_per_page, 'ignore_sticky_posts' => true, 'post__not_in' => array($post->ID)));
     }
     $args = array('posts_per_page' => $posts_per_page, 'ignore_sticky_posts' => true, 'post__not_in' => array($post->ID));
     // Get posts from the same category.
     $categories = get_the_category();
     if (!empty($categories)) {
         $category = array_shift($categories);
         $args['tax_query'] = array(array('taxonomy' => 'category', 'field' => 'id', 'terms' => $category->term_id));
     }
     return new WP_Query($args);
 }
示例#3
0
<?php

$post = new TimberPost();
$data = Timber::get_context();
$related_posts = yarpp_get_related();
if (!empty($related_posts)) {
    foreach ($related_posts as $related_post) {
        $timber_related_posts[] = Timber::get_post($related_post->ID);
    }
    $data['related_posts'] = $timber_related_posts;
}
$data['post'] = $post;
$data['author_image'] = get_lazyloadxt_html(get_wp_user_avatar($post->author->ID, 150));
Timber::render(array('single.twig'), $data);
 /**
  * Get related posts
  * 
  * @param integer $count number of posts to return
  * @param integer|null $post_id
  */
 public function get_related($count = 5, $post_id = null)
 {
     if (!$post_id) {
         global $post;
         $post_id = $post->ID;
     }
     if (Bunyad::options()->related_posts_yarpp && function_exists('yarpp_get_related')) {
         return yarpp_get_related(array('limit' => $count), $post_id);
     }
     $args = array('numberposts' => $count, 'post__not_in' => array($post_id));
     // get related posts using tags or categories?
     if (Bunyad::options()->related_posts_by == 'tags') {
         $args['tag__in'] = wp_get_post_tags($post_id, array('fields' => 'ids'));
     } else {
         $args['category__in'] = wp_get_post_categories($post_id);
     }
     $related = get_posts(apply_filters('bunyad_get_related_query', $args));
     return (array) $related;
 }
示例#5
0
 /**
  * Use a plugin to get related posts, or fall back to
  * simply fetching some posts from the same category.
  */
 public static function get_related_posts()
 {
     $post = get_post();
     $posts_per_page = apply_filters('semicolon_related_posts_per_page', 4);
     // Support for the Zone Manager (Zoninator) plugin
     if (function_exists('z_get_zoninator')) {
         // Allow plugins and child themes to define their own zones or use defaults.
         $zones = apply_filters('semicolon_related_posts_zones', false, $post->ID);
         if (!is_array($zones)) {
             $zones = array('related-posts');
             $categories = wp_get_object_terms($post->ID, array('category'), array('fields' => 'slugs'));
             foreach ($categories as $slug) {
                 $zones[] = 'related-posts-' . $slug;
             }
             $zones = array_reverse($zones);
         }
         foreach ($zones as $zone) {
             $query = z_get_zoninator()->get_zone_query($zone, apply_filters('semicolon_related_posts_query_args', array('posts_per_page' => $posts_per_page, 'post__not_in' => array($post->ID), 'ignore_sticky_posts' => true)));
             if ($query->have_posts()) {
                 return $query;
             }
         }
     }
     // Support for the Yet Another Related Posts Plugin
     if (function_exists('yarpp_get_related')) {
         $related = yarpp_get_related(array('limit' => $posts_per_page), $post->ID);
         $args = array('post__in' => wp_list_pluck($related, 'ID'), 'posts_per_page' => $posts_per_page, 'ignore_sticky_posts' => true, 'post__not_in' => array($post->ID));
         $args = apply_filters('semicolon_related_posts_query_args', $args);
         return new WP_Query($args);
     }
     $args = array('posts_per_page' => $posts_per_page, 'ignore_sticky_posts' => true, 'post__not_in' => array($post->ID));
     // Get posts from the same category.
     $categories = get_the_category();
     if (!empty($categories)) {
         $category = array_shift($categories);
         $args['tax_query'] = array(array('taxonomy' => 'category', 'field' => 'id', 'terms' => $category->term_id));
     }
     $args = apply_filters('semicolon_related_posts_query_args', $args);
     return new WP_Query($args);
 }