-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvadimages_drowdown_posts.php
53 lines (48 loc) · 1.38 KB
/
vadimages_drowdown_posts.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
<?php
/**
* Plugin Name: Vadimages Dropdown Posts
* Description: Provide function to display dropdown with posts.
* Version: 0.2.1
* Author: Vadimages team
* Author URI: http://vadimages.com
*/
function v_dropdown_posts($args = '')
{
$defaults = array(
"orderby" => "title",
"order" => "asc",
"selected" => 0,
"echo" => 1,
"select_name" => "post_id",
"select_id" => "",
"post_type" => "post",
"posts_per_page" => -1
);
$r = wp_parse_args($args, $defaults);
extract($r, EXTR_SKIP);
if (empty($select_id)) {
$select_id = $select_name;
}
$output = '';
$the_query = new WP_Query($r);
if ($the_query->have_posts()) {
$output = "<select name='" . esc_attr($select_name) . "' id='" . esc_attr($select_id) . "'>";
while ($the_query->have_posts()) {
$the_query->the_post();
$output .= '<option';
if ($selected) {
if ($selected == get_the_ID()) {
$output .= " selected='selected' ";
}
}
$output .= ' value="' . get_the_ID() . '">' . get_the_title() . '</option>';
}
$output .= "</select>";
}
wp_reset_postdata();
if ($echo) {
echo $output;
} else {
return $output;
}
}