-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-related-news.php
113 lines (95 loc) · 3.84 KB
/
wp-related-news.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
107
108
109
110
111
112
113
<?php
/**
* WP News Post Type & Related News Widget
*
* @package WPRelatedNews
* @author Joshua Jenks
* @copyright 2021 Joshua Jennks
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: wp-related-news
* Plugin URI: https://newfang.digital/wp-related-news
* Description: This plugin is an creates a news item post type and a shortcode to display 5 recent posts.
* Version: 1.0.0
**/
class WPRelatedNews {
public function __construct()
{
add_action( 'init', array( $this, 'create_news_post_type' ) );
add_shortcode('related_news', array($this, 'displayNewsItems'));
}
public function displayNewsItems()
{
// extract the attributes into variables
extract(shortcode_atts(array(
'num_items' => 5,
), $atts));
// Capturing current post data
$post_id = get_the_ID();
$cat_ids = array();
$categories = get_the_category( $post_id );
if(!empty($categories) && !is_wp_error($categories)):
foreach ($categories as $category):
array_push($cat_ids, $category->term_id);
endforeach;
endif;
$news_post_type = 'yoko_news';
// Custom WP query relatednews in category of current post
$query_args = array(
'category__in' => $cat_ids,
'post_type' => $news_post_type,
'post__not_in' => array($post_id),
'posts_per_page' => $atts['num_items'],
);
$related_news = new WP_Query( $query_args );
if($related_news->have_posts()):
while($related_news->have_posts()): $related_news->the_post(); ?>
<ul>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php the_content(); ?>
</li>
</ul>
<?php endwhile;
// Restore original Post Data
wp_reset_postdata();
endif;
}
public function create_news_post_type() {
$name = 'News';
$singular_name = 'News';
register_post_type(
'yoko_' . strtolower( $name ),
array(
'labels' => array(
'menu_name' => esc_html__('News Items', 'news-items'),
'name_admin_bar' => esc_html__('News Item', 'news-items'),
'add_new' => esc_html__('Add News Item', 'news-items'),
'add_new_item' => esc_html__('Add new News Item', 'news-items'),
'new_item' => esc_html__('New News Item', 'news-items'),
'edit_item' => esc_html__('Edit News Item', 'news-items'),
'view_item' => esc_html__('View News Item', 'news-items'),
'update_item' => esc_html__('View News Item', 'news-items'),
'all_items' => esc_html__('All News Items', 'news-items'),
'search_items' => esc_html__('Search News Items', 'news-items'),
'parent_item_colon' => esc_html__('Parent News Item', 'news-items'),
'not_found' => esc_html__('No News Items found', 'news-items'),
'not_found_in_trash' => esc_html__('No News Items found in Trash', 'news-items'),
'name' => esc_html__('News Items', 'news-items'),
'singular_name' => esc_html__('News Item', 'news-items'),
),
'public' => true,
'has_archive' => strtolower($taxonomy_name),
'hierarchical' => false,
'rewrite' => array( 'slug' => $name ),
'menu_icon' => 'dashicons-carrot',
'taxonomies' => array( 'category' ),
)
);
}
}
$wpRelatedNews = new WPRelatedNews();
?>