-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsa_related_pages.php
148 lines (115 loc) · 3.91 KB
/
sa_related_pages.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
Plugin Name: St Andrews Related Pages
Description: A custom widget for St Andrews website to show related page from the page tree in a sidebar widget.
Author: David Bick, following a plugin by Sean Barton
*/
function sa_related_pages_render_entry($title, $permalink, $post_class) {
$return = '<li class="' . $post_class . '"><a href="' . $permalink . '">' . $title . '</a></li>';
return $return;
}
function sa_related_pages_render_list($related_posts, $post_class = '', $this_page_id=false) {
$return = false;
if ($related_posts->have_posts()) {
$return .= $template_start;
while ($related_posts->have_posts()) {
$related_posts->the_post();
global $post;
$p = $post;
if($p->ID != $this_page_id) {
$return .= sa_related_pages_render_entry(
$p->post_title,
get_permalink($p->ID),
$post_class
);
}
}
}
return $return;
}
function sa_related_pages_find_and_render_list() {
global $wpdb;
global $wp_query;
global $post;
$post_id = get_the_ID();
$parent_id = $post->post_parent;
$template_start = '<ul>';
$template_end = '</ul>';
$parent_list = $sibling_list = $child_list = false;
if($parent_id) {
// Parent
$args = array(
'post_type'=>'page'
, 'post_status'=>'publish'
, 'page_id'=>$parent_id
);
$related_posts = new WP_Query($args);
$parent_list .= sa_related_pages_render_list($related_posts, 'sa_related_pages_parent');
// Siblings
$args = array(
'post_type'=>'page'
, 'post_status'=>'publish'
, 'post_parent'=>$parent_id
, 'orderby'=>'menu_order'
, 'order'=>'ASC' );
$related_posts = new WP_Query($args);
$sibling_list .= sa_related_pages_render_list($related_posts, 'sa_related_pages_sibling', $post_id);
}
// Children
$args = array(
'post_type'=>'page'
, 'post_status'=>'publish'
, 'post_parent'=>$post_id
, 'orderby'=>'menu_order'
, 'order'=>'ASC' );
$related_posts = new WP_Query($args);
$child_list .= sa_related_pages_render_list($related_posts, 'sa_related_pages_child');
wp_reset_postdata();
wp_reset_query();
// Choose something suitable... children only, if we have them. Otherwise siblings and parent.
$return = $child_list ? $child_list : $parent_list . $sibling_list;
if($return) {
$return = $template_start . $return . $template_end;
}
return $return;
}
function sa_related_pages_loaded() {
add_action('widgets_init', create_function('', 'return register_widget("sa_related_pages_pages_widget");'));
}
class sa_related_pages_pages_widget extends WP_Widget {
function sa_related_pages_pages_widget() {
parent::WP_Widget(false, 'St Andrews Related Pages');
}
function widget($args, $instance) {
global $sbu;
extract($args);
$title = apply_filters('widget_title', $instance['title']);
$text = apply_filters('widget_text', $instance['text']);
$list = sa_related_pages_find_and_render_list();
if ($list) {
echo $before_widget;
if ($title) {
echo $before_title . $title . $after_title;
}
if ($text) {
echo $text;
}
echo $list;
echo $after_widget;
}
}
function update($new_instance, $old_instance) {
return $new_instance;
}
function form($instance) {
global $sbu;
$title = esc_attr($instance['title']);
$text = trim(esc_attr($instance['text']));
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Intro Text (optional):'); ?> <textarea class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea></label></p>
<?php
}
}
add_action('plugins_loaded', 'sa_related_pages_loaded');
?>