Php WordPress-仅显示(自定义)标记的帖子
我目前正在构建一个基于templatic events v2主题的网站。我做了一些定制工作,效果很好,但这一项是我力所能及的 问题如下 我需要建立一个小部件,它只输出标记为特色的事件(自定义帖子)。现在我有一些不同的方式来显示事件,但它们基本上只是按类别或日期排序 示例站点: 以下是我正在使用的小部件的当前代码:Php WordPress-仅显示(自定义)标记的帖子,php,wordpress,widget,Php,Wordpress,Widget,我目前正在构建一个基于templatic events v2主题的网站。我做了一些定制工作,效果很好,但这一项是我力所能及的 问题如下 我需要建立一个小部件,它只输出标记为特色的事件(自定义帖子)。现在我有一些不同的方式来显示事件,但它们基本上只是按类别或日期排序 示例站点: 以下是我正在使用的小部件的当前代码: class blaestfeatured extends WP_Widget { function blaestfeatured() { //Construct
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
?>
class blaestu小部件{
函数blaestfeatured(){
//建造师
$widget_ops=array('classname'=>'小部件类别列表视图','description'=>'由Christoff Sandstrøm定制的特色事件小部件');
$this->WP_小部件('blaestfeatured','blaestfeatured',$Widget_ops);
}
函数小部件($args$instance){
//打印小部件
提取($args,EXTR_SKIP);
echo$before_小部件;
$widget_id=$args['widget_id'];
$title=empty($instance['title'])?'':应用_过滤器('widget_title',$instance['title']);
$category=empty($instance['category'])?“”:应用_过滤器('widget_category',$instance['category']);
$post_number=empty($instance['post_number'])?'5':应用_过滤器('widget_post_number',$instance['post_number']);
$post_link=empty($instance['post_link'])?'':应用_过滤器('widget_post_link',$instance['post_link']);
$more_link=empty($instance['more_link'])?'':应用_过滤器('widget_more_link',$instance['more_link']);
$character\u cout=empty($instance['character\u cout'])?'15':应用过滤器('widget\u character\u cout',$instance['character\u cout']);
$sorting=empty($instance['event\u sorting'])?'Latest Published':应用过滤器('widget\u event\u sorting',$instance['event\u sorting']);
?>
以下是您的疑问:
$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; ?>
$args=array(
“发布类型”=>“我的自定义发布类型”,
“每页帖子数”=>10,
“元查询”=>数组(
排列(
“键”=>“特色”,
'值'=>'h',
)
)
);
$query=新的WP\u查询($args);
而($query->have_posts()):$query->the_post();?>
回答得很好,虽然使用元查询时不需要使用“meta_key”
。非常感谢!我知道这又是一个相当迟钝的问题,但我如何使用它来输出它?我尝试在这里搜索一些,但没有多大意义:/Thank you!我无法运行它,可能是因为我怀疑的其他代码t、 请参阅我的新回复@Banago最终使其不崩溃并输出一些内容,但它仍然只打印前五个事件:/