/
custom-taxonomies.php
173 lines (149 loc) · 4.64 KB
/
custom-taxonomies.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
Plugin Name: WPAdverts Snippets - Custom Taxonomies
Version: 1.0
Author: Greg Winiarski
Description: Adds custom taxonomy to [adverts_add] and displays values (as links) in Ad details page.
*/
add_action( "init", "custom_taxonomies_init", 50 );
add_filter( "adverts_form_load", "custom_taxonomies_form_load" );
add_filter( "adverts_list_query", "custom_taxonomies_query" );
add_action( "adverts_tpl_single_details", "custom_taxonomies_tpl_single_details" );
/**
* Custom Taxonomy Init
*
* Registers new custom taxonomy
*
* @since 1.0
* @return void
*/
function custom_taxonomies_init() {
$args = array(
'label' => __( "Taxonomy Example" ),
'hierarchical' => true,
'query_var' => true,
'rewrite' => array('slug' => 'advert-example'),
);
register_taxonomy( 'advert_example', 'advert', $args );
}
/**
* Returns options for advert_example field
*
* This function is being used when generating category field in the (for example
* "post ad" form).
*
* @uses adverts_walk_category_dropdown_tree()
* @since 1.0
* @return array
*/
function custom_taxonomies_options() {
$args = array(
'taxonomy' => 'advert_example',
'hierarchical' => true,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'depth' => 0,
'selected' => 0,
'show_count' => 0,
);
include_once ADVERTS_PATH . '/includes/class-walker-category-options.php';
$walker = new Adverts_Walker_Category_Options;
$params = array(
get_terms( 'advert_example', $args ),
0,
$args
);
return call_user_func_array(array( &$walker, 'walk' ), $params );
}
/**
* Adds "Taxonomy Example" field into [adverts_add] form.
*
* @since 1.0
* @access public
* @param array $form Adverts Form structure
* @return array Customized form structure
*/
function custom_taxonomies_form_load( $form ) {
global $pagenow;
if( $form['name'] == "advert" && $pagenow != "post.php" ) {
$form["field"][] = array(
"name" => "advert_example_tax",
"type" => "adverts_field_select",
"order" => 20,
"label" => __( "Taxonomy Example", "adverts" ),
"max_choices" => 10,
"options" => array(),
"options_callback" => "custom_taxonomies_options",
"save" => array(
"method" => "taxonomy",
"taxonomy" => "advert_example"
)
);
}
if( $form['name'] == "search" ) {
$form['field'][] = array(
"name" => "advert_example",
"type" => "adverts_field_select",
"order" => 20,
"label" => "",
"max_choices" => 10,
"options" => array(),
"options_callback" => "custom_taxonomies_options",
"meta" => array(
"search_group" => "visible",
"search_type" => "full"
)
);
}
return $form;
}
/**
* Displays selected terms on Ad details page.
*
* This functions is executed by adverts_tpl_single_details filter
*
* @since 1.0
* @access public
* @param int $post_id Currently displayed post ID
*/
function custom_taxonomies_tpl_single_details( $post_id ) {
$terms = get_the_terms( $post_id, 'advert_example' );
?>
<?php if(! empty( $terms ) ): ?>
<div class="adverts-grid-row">
<div class="adverts-grid-col adverts-col-30">
<span class="adverts-round-icon adverts-icon-wordpress"></span>
<span class="adverts-row-title">Taxonomy Example</span>
</div>
<div class="adverts-grid-col adverts-col-65">
<?php foreach( $terms as $term ): ?>
<a href="<?php echo esc_attr( get_term_link( $term ) ) ?>"><?php echo join( " / ", advert_term_path( $term, 'advert_example' ) ) ?></a><br/>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php
}
/**
* Adds tax_query param to WP_Query
*
* The tax_query is added only if it is in $_GET['advert_example']
*
* @param array $args WP_Query args
* @return array Modified WP_Query args
*/
function custom_taxonomies_query( $args ) {
if( ! adverts_request( "advert_example" ) ) {
return $args;
}
if( ! isset( $args["tax_query"] ) || ! is_array( $args["tax_query"] ) ) {
$args["tax_query"] = array();
}
$args["tax_query"][] = array(
'taxonomy' => 'advert_example',
'field' => 'term_id',
'terms' => adverts_request( "advert_example" )
);
return $args;
}