1
|
1 |
<?php |
|
2 |
class feature_posts_widget extends WP_Widget { |
|
3 |
/** constructor */ |
|
4 |
function feature_posts_widget() { |
|
5 |
parent::WP_Widget(false, $name = "OCMX Featured Posts Widget", array("description" => "Features Posts widget for the Home Page. Place only in the 'Header Panel'.")); |
|
6 |
} |
|
7 |
|
|
8 |
/** @see WP_Widget::widget */ |
|
9 |
function widget($args, $instance) { |
|
10 |
extract( $args ); |
|
11 |
if(!($instance["post_category"])) : |
|
12 |
$use_catId = 0; |
|
13 |
else : |
|
14 |
$use_category = $instance["post_category"]; |
|
15 |
$use_catId = get_category_by_slug($use_category); |
|
16 |
endif; |
|
17 |
//Fetch the category for the widget |
|
18 |
$post_args = array('numberposts' => '5', 'offest' => 0, 'orderby' => 'post_date', 'order' => 'DESC','cat' => $use_catId->term_id); |
|
19 |
//Set the post Aguments and Query accordingly |
|
20 |
$count = 1; |
|
21 |
$ocmx_featured = get_posts($post_args); |
|
22 |
?> |
|
23 |
<div class="selected-feature"> |
|
24 |
<?php foreach($ocmx_featured as $post) : ?> |
|
25 |
<div class="feature-title<?php if($count !== 1): ?> no_display<?php endif; ?>" id="feature-post-header-<?php echo $post->ID; ?>"> |
|
26 |
<h4><?php echo date('d M Y', strtotime($post->post_date)); ?></h4> |
|
27 |
<h2> |
|
28 |
<?php $link = get_permalink($post->ID); ?> |
|
29 |
<a href="<?php echo $link ?>"><?php echo $post->post_title; ?></a> |
|
30 |
</h2> |
|
31 |
<?php if($count == 1): ?><div class="no_display" id="first_selected"><?php echo $post->ID;?></div><?php endif; ?> |
|
32 |
</div> |
|
33 |
<?php $count++; endforeach; ?> |
|
34 |
<div class="container-header-light-dark"><span></span></div> |
|
35 |
<div class="container-light" id="feature-media-container"> |
|
36 |
<?php $count = 1; foreach($ocmx_featured as $post) : |
|
37 |
// Fetch the PermaLink, Thumbnail and Video Metas |
|
38 |
$get_post_video = get_post_meta($post->ID, "main_video", true); |
|
39 |
$get_thumbnail = get_post_meta($post->ID, "other_media", true); |
|
40 |
// Set our category |
|
41 |
$category = get_the_category(); |
|
42 |
// If we haven't used our custom image input, search for the first image in the post |
|
43 |
$post_image = ""; |
|
44 |
if($get_thumbnail == "" && (get_option("ocmx_auto_home_images") && get_option("ocmx_auto_home_images") !== "no")) : |
|
45 |
$post_image = fetch_post_image($post->ID, "640", "2000"); |
|
46 |
endif; |
|
47 |
?> |
|
48 |
<div <?php if($count !== 1): ?>class="no_display"<?php endif; ?> id="feature-post-media-<?php echo $post->ID; ?>"> |
|
49 |
<?php if($get_post_video !== "") : |
|
50 |
$get_post_video = preg_replace("/(width\s*=\s*[\"\'])[0-9]+([\"\'])/i", "$1 640 $2 wmode=\"transparent\"", $get_post_video); |
|
51 |
$get_post_video = preg_replace("/(height\s*=\s*[\"\'])[0-9]+([\"\'])/i", "$1 360 $2", $get_post_video); |
|
52 |
/* Display Video */ |
|
53 |
echo $get_post_video; |
|
54 |
elseif ($post_image !== "") : |
|
55 |
echo $post_image; |
|
56 |
elseif ($get_thumbnail !== "") : ?> |
|
57 |
<img src="<?php bloginfo('template_directory'); ?>/functions/timthumb.php?src=<?php echo $get_thumbnail ?>&h=&w=640&zc=1" alt="<?php the_title(); ?>" /> |
|
58 |
<?php endif; ?> |
|
59 |
</div> |
|
60 |
<?php $count++; endforeach; ?> |
|
61 |
</div> |
|
62 |
<div class="container-footer-light-dark"><span></span></div> |
|
63 |
</div> |
|
64 |
|
|
65 |
<ul class="feature-list"> |
|
66 |
<?php $count = 1; ?> |
|
67 |
<?php foreach($ocmx_featured as $post) : |
|
68 |
setup_postdata($post); |
|
69 |
$link = get_permalink($post->ID); ?> |
|
70 |
<li> |
|
71 |
<span class="item"><?php echo $count; ?></span> |
|
72 |
<h4><?php echo date('d M Y', strtotime($post->post_date)); ?></h4> |
|
73 |
<a href="<?php echo $link; ?>" id="ocmx-featured-href-<?php echo $post->ID; ?>"><?php echo $post->post_title; ?></a> |
|
74 |
</li> |
|
75 |
<?php |
|
76 |
$count++; |
|
77 |
endforeach; ?> |
|
78 |
</ul> |
|
79 |
|
|
80 |
<?php |
|
81 |
rewind_posts(); |
|
82 |
} |
|
83 |
|
|
84 |
/** @see WP_Widget::update */ |
|
85 |
function update($new_instance, $old_instance) { |
|
86 |
return $new_instance; |
|
87 |
} |
|
88 |
|
|
89 |
/** @see WP_Widget::form */ |
|
90 |
function form($instance) { |
|
91 |
$post_category = esc_attr($instance["post_category"]); |
|
92 |
?> |
|
93 |
<p><label for="<?php echo $this->get_field_id('post_category'); ?>">Category |
|
94 |
<select size="1" class="widefat" id="<?php echo $this->get_field_id("post_category"); ?>" name="<?php echo $this->get_field_name("post_category"); ?>"> |
|
95 |
<option <?php if($post_count == 0){echo "selected=\"selected\"";} ?> value="0">All</option> |
|
96 |
<?php |
|
97 |
$category_args = array('hide_empty' => false); |
|
98 |
$option_loop = get_categories($category_args); |
|
99 |
foreach($option_loop as $option_label => $value) |
|
100 |
{ |
|
101 |
// Set the $value and $label for the options |
|
102 |
$use_value = $value->slug; |
|
103 |
$label = $value->cat_name; |
|
104 |
//If this option == the value we set above, select it |
|
105 |
if($use_value == $post_category) |
|
106 |
{$selected = " selected='selected' ";} |
|
107 |
else |
|
108 |
{$selected = " ";} |
|
109 |
?> |
|
110 |
<option <?php echo $selected; ?> value="<?php echo $use_value; ?>"><?php echo $label; ?></option> |
|
111 |
<?php |
|
112 |
} |
|
113 |
?> |
|
114 |
</select> |
|
115 |
</p> |
|
116 |
<?php |
|
117 |
} // form |
|
118 |
|
|
119 |
}// class |
|
120 |
|
|
121 |
//This sample widget can then be registered in the widgets_init hook: |
|
122 |
|
|
123 |
// register FooWidget widget |
|
124 |
add_action('widgets_init', create_function('', 'return register_widget("feature_posts_widget");')); |
|
125 |
|
|
126 |
?> |