所以,我目前正在建立一个基于模板事件v2主题的网站。我已经做了一些工作正常的定制,但这个有点超出我的范围。
问题如下。
我需要构建一个小部件,它只输出标记为要素的事件(自定义帖子)。现在我有一些不同的方式来显示事件,但它们几乎只是按类别或按日期排序。
示例网站:http://www.blaest.no/jonnysnorkel
以下是我正在处理的小部件的小部件的当前代码:
class blaestfeatured extends WP_Widget {
function blaestfeatured() {
//Constructor
$widget_ops = array('classname' => 'widget category List View', 'description' => 'Custom Featured event widget by Christoffer Sandstrøm' );
$this->WP_Widget('blaestfeatured', 'blaestfeatured', $widget_ops);
}
function widget($args, $instance) {
// prints the widget
extract($args, EXTR_SKIP);
echo $before_widget;
$widget_id= $args['widget_id'];
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
$category = empty($instance['category']) ? '' : apply_filters('widget_category', $instance['category']);
$post_number = empty($instance['post_number']) ? '5' : apply_filters('widget_post_number', $instance['post_number']);
$post_link = empty($instance['post_link']) ? '' : apply_filters('widget_post_link', $instance['post_link']);
$more_link = empty($instance['more_link']) ? '' : apply_filters('widget_more_link', $instance['more_link']);
$character_cout = empty($instance['character_cout']) ? '15' : apply_filters('widget_character_cout', $instance['character_cout']);
$sorting = empty($instance['event_sorting']) ? 'Latest Published' : apply_filters('widget_event_sorting', $instance['event_sorting']);
?>
<h3 class=""><?php echo $title; ?> </h3>
<ul>
<?php //$type = get_option('ptthemes_event_sorting');
if ( $sorting != '' )
{
global $wpdb;
if ( $sorting == 'Random' )
{
$orderby = "(select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id = p.ID and $wpdb->postmeta.meta_key like \"st_date\") ASC, rand()";
}
elseif ( $sorting == 'Alphabetical' )
{
$orderby = "p.post_title ASC";
}elseif($sorting =='s_date'){
$today = date('Y-m-d');
$orderby = "(select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id = p.ID and $wpdb->postmeta.meta_key like \"st_date\") ASC";
}
else
{
$orderby = "(select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id=p.ID and $wpdb->postmeta.meta_key = 'featured_h') ASC, p.post_date DESC";
}
}
?>
<?php
global $post,$wpdb;
if($category)
{
$category = "'".str_replace(",","','",$category)."'";
$sqlsql = " and p.ID in (select tr.object_id from $wpdb->term_relationships tr join $wpdb->term_taxonomy t on t.term_taxonomy_id=tr.term_taxonomy_id where t.term_id in ($category) )";
}
$today = date('Y-m-d');
if ( false === ( $latest_menus = get_transient( 'onecolumnslist'.$widget_id ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$where = "AND p.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='end_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d')>='".$today."') ";
@$sql = "select p.* from $wpdb->posts p where p.post_type='".CUSTOM_POST_TYPE1."' and (p.post_status='publish' or p.post_status='recurring' ) AND (p.ID in ( select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='event_type' and $wpdb->postmeta.meta_value ='Regular event')) $sqlsql $where order by $orderby limit $post_number";
$latest_menus = $wpdb->get_results($sql);
set_transient( 'onecolumnslist'.$widget_id, $latest_menus, 60*60*12 );
}
$pcount=0;
if($latest_menus)
{
foreach($latest_menus as $post) :
setup_postdata($post);
$pcount++; ?>
<?php
$is_parent = $post->post_parent;
if($is_parent)
$post_id = $post->post_parent;
else
$post_id = $post->ID;
$post_images = bdw_get_images_with_info($post_id,'thumb');
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ) , 'single-post-thumbnail' );
if($image[0] != '')
$thumb = $image[0];
elseif($post_images[0]['file'] != '')
$thumb = $post_images[0]['file']; ?>
<?php
$terms = get_the_terms( $post->ID, 'eventcategory' ); // Definerer måten å hente cssclassen for terms.
if ( $terms && ! is_wp_error( $terms ) ) :
$classcategory = array();
foreach ( $terms as $term ) {
$classcategory[] = $term->name;
}
$postclasscategory = join( ", ", $classcategory );
?>
<?php
$args = array(
'post_type' => 'event',
'meta_query' => array(
array(
'key' => 'featured_h',
'value' => 'h',
)
)
);
$query = new WP_Query($args);
while( $query->have_posts() ) : $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php get_post_class(); ?> ><?php } ?>
<h3 class="<?php echo $postclasscategory; ?>" > <?php echo get_formated_date(get_post_meta($post->ID,'st_date',true));?> </h3>
<div class="eventcontainer">
<?php if(get_post_meta($post->ID,'featured_h',true) == 'h' ) { ?><div class="featured_img_s"></div><?php }?>
<?php if ( $thumb != '' ) { ?>
<a class="headliner-img" href="<?php the_permalink(); ?>">
<img src="<?php echo $thumb; ?>" width="290" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
<?php
} else { ?>
<a href="<?php echo get_permalink($post->ID) ; ?>" class="headliner-img"><img src="<?php echo get_template_directory_uri()."/images/no-image.png"; ?>" height="120" alt="<?php echo @$post_img[0]['alt']; ?>" /></a>
</div>
<?php endwhile; ?>
<h3 id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</li> <!-- -->
<?php endforeach; ?>
<?php }else{ ?>
<p><?php _e('Not a single Event is there','templatic');?></p>
<?php } ?>
<?php
echo '</ul>';
echo $after_widget;
}
function update($new_instance, $old_instance) {
//save the widget
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['category'] = strip_tags($new_instance['category']);
$instance['post_number'] = strip_tags($new_instance['post_number']);
$instance['post_link'] = strip_tags($new_instance['post_link']);
$instance['more_link'] = strip_tags($new_instance['more_link']);
$instance['character_cout'] = strip_tags($new_instance['character_cout']);
$instance['event_sorting'] = strip_tags($new_instance['event_sorting']);
return $instance;
}
function form($instance) {
//widgetform in backend
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'category' => '', 'post_number' => '','character_cout' => '','more_link' => '', 'event_sorting' => 'Latest Published' ) );
$title = strip_tags($instance['title']);
$category = strip_tags($instance['category']);
$post_number = strip_tags($instance['post_number']);
$post_link = strip_tags($instance['post_link']);
$more_link = strip_tags($instance['more_link']);
$character_cout = strip_tags($instance['character_cout']);
$sorting = strip_tags($instance['event_sorting']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php echo TITLE_TEXT; ?>:
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('category'); ?>"><?php echo CATEGORY_IDS_TEXT; ?>:
<input class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" type="text" value="<?php echo esc_attr($category); ?>" />
</label> <p><?php echo fetch_categories_ids('eventcategory'); ?></p>
</p>
<p>
<label for="<?php echo $this->get_field_id('post_number'); ?>"><?php echo NUMBER_POSTS_TEXT; ?>:
<input class="widefat" id="<?php echo $this->get_field_id('post_number'); ?>" name="<?php echo $this->get_field_name('post_number'); ?>" type="text" value="<?php echo esc_attr($post_number); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('event_sorting'); ?>"><?php echo SORT_EVENT; ?>:
<select name="<?php echo $this->get_field_name('event_sorting'); ?>" id="<?php echo $this->get_field_id('event_sorting'); ?>">
<option selected="selected" value="Latest Published"><?php _e('Latest Published','templatic'); ?></option>
<option <?php if ($sorting == 'Random') { echo 'selected=selected'; } ?> value="Random"><?php _e('Random','templatic'); ?></option>
<option <?php if ($sorting == 'Alphabetical') { echo 'selected=selected'; } ?> value="Alphabetical"><?php _e('Alphabetical','templatic'); ?></option>
<option <?php if ($sorting == 's_date') { echo 'selected=selected'; } ?> value="s_date"><?php _e('As Per Start Date','templatic'); ?></option>
</select>
</label>
</p>
<?php
}
}
register_widget('blaestfeatured');
// BLAEST FEATURED ENDS
?>
我为自己尝试了几个晚上,但不幸的是我的PHP技能充其量只是黯淡无光..
任何帮助都将深表感谢! - 克里斯
编辑:使用整个小部件代码更新。
答案 0 :(得分:1)
以下是您的查询:
$args = array(
'post_type' => 'my_custom_post_type',
'posts_per_page' => 10,
'meta_query' => array(
array(
'key' => 'featured_h',
'value' => 'h',
)
)
);
$query = new WP_Query($args);
while( $query->have_posts() ) : $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php get_post_class(); ?> ><?php } ?>
<h3 class="<?php echo $postclasscategory; ?>" > <?php echo get_formated_date(get_post_meta($post->ID,'st_date',true));?> </h3>
<div class="eventcontainer">
<?php if(get_post_meta($post->ID,'featured_h',true) == 'h' ) { ?><div class="featured_img_s"></div><?php }?>
<?php if ( $thumb != '' ) { ?>
<a class="headliner-img" href="<?php the_permalink(); ?>">
<img src="<?php echo $thumb; ?>" width="290" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
<?php
} else { ?>
<a href="<?php echo get_permalink($post->ID) ; ?>" class="headliner-img"><img src="<?php echo get_template_directory_uri()."/images/no-image.png"; ?>" height="120" alt="<?php echo @$post_img[0]['alt']; ?>" /></a>
</div>
<?php endwhile; ?>