-
Notifications
You must be signed in to change notification settings - Fork 1
/
content-related-articles.php
106 lines (89 loc) · 2.3 KB
/
content-related-articles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Display related posts.
*
* First display tag related posts. If there are not enough of them, add some
* more posts from the same category. If that is still not enough, add some
* random posts.
*
* @author Kim-Christian Meyer <kim.meyer@palasthotel.de>
* @package digitale-pracht
*/
// Array with different methods of retrieving related articles. Returns a
// partial WP Query args array each.
$relateds_args = array(
/**
* WP Query args for tag related posts in chronological order.
*
* @return array
*/
'tags' => function() {
$tags = get_the_tags( get_the_id() );
if ( empty( $tags ) || ! is_array( $tags ) ) {
return array();
}
$tags_slugs = array_map( function( $tag_obj ) {
return $tag_obj->slug;
}, $tags );
return array(
'orderby' => 'post_date',
'tag_slug__in' => $tags_slugs,
);
},
/**
* WP Query args for posts from the same category in random order.
*
* @return array
*/
'categories' => function() {
$categories = wp_get_post_categories( get_the_id() );
if ( empty( $categories ) || ! is_array( $categories ) ) {
return array();
}
return array(
'category__in' => $categories,
'orderby' => 'rand',
);
},
/**
* WP Query args for random posts.
*/
'random' => function() {
return array(
'orderby' => 'rand',
);
},
);
// how many?
$relateds_max = get_theme_mod( 'digitalepracht_relateds_number', 4 );
// counter
$relateds_count = 0;
// exclude already processed posts
$relateds__not_in = array( get_the_ID() );
foreach ( $relateds_args as $slug => $get_args ) {
// Continue until there are `$relateds_max` posts.
if ( $relateds_count >= $relateds_max ) {
break;
}
$args = $get_args();
if ( empty( $args ) ) {
continue;
}
$default_args = array(
'posts_per_page' => $relateds_max - $relateds_count,
'post_type' => 'post', // posts only
'meta_key' => '_thumbnail_id', // with thumbnail
'post__not_in' => $relateds__not_in, // exclude current post
);
$args = wp_parse_args( $args, $default_args );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'teaser', 'illustrated' );
$relateds_count ++;
$relateds__not_in[] = get_the_id();
}
}
wp_reset_postdata();
}