function get_date_posts($params, $content){
$default_params = array(
\'post_type\' => \'post\',
\'order\' => \'asc\',
\'orderby\' => \'meta_value\',
\'posts_per_page\'=> 5,
\'meta_key\' => \'fl_date_picker\'
);
if (isset($params[\'type\']) && !empty($params[\'type\']) && post_type_exists($params[\'type\']))
$params[\'post_type\'] = $params[\'type\'];
if (isset($params[\'taxonomy\']) && !empty($params[\'taxonomy\']) && taxonomy_exists($params[\'taxonomy\'])
&& isset($params[\'slug\']) && !empty($params[\'slug\']))
$params[$params[\'taxonomy\']] = $params[\'slug\'];
foreach(array(\'type\', \'slug\', \'taxonomy\') as $key)
if (isset($params[$key]))
unset($params[$key]);
$params = wp_parse_args($params, $default_params);
$params[\'meta_query\'] = array( array(
\'key\' => $params[\'meta_key\'],
\'value\' => date("Ymd"),
\'compare\' => \'>=\',
\'type\'=> \'date\'
)
);
$transient_hash = \'get_date_posts_\'.substr(md5(serialize($params)),0,10);
if (false === ($html = get_transient($transient_hash))){
add_filter(\'query\', \'q\');
global $request_sql;
// init
$html = \'\';
$rp = new WP_Query($params);
// $html .= \'<h2>START</h2>\';
// $html .= \'<pre>\'.var_export($params, 1).\'</pre>\';
// $html .= $request_sql.\'<hr>\';
if ($rp->have_posts()){
$html .= \'<ul>\';
while ( $rp->have_posts() ) {
$rp->the_post();
$html .= "<li>";
if (($thumb_id = get_post_thumbnail_id())){
$thumb = wp_get_attachment_image_src($thumb_id, \'panel-small\', false);
$flag = get_field(\'fl_country\');
$html .= "<a href=".get_permalink()."><div id=\\"calimgf\\" style=\\"background:url({$thumb[\'0\']}) no-repeat left top;\\">";
$html .= "<span id=\\"loullos\\" style=\\"float:right;\\" class=\'". $flag ."\' >" ."</span>";
}
$html .= "<span class=\\"calendate\\" >".get_field(\'fl_event_date\')."</span>";
$html .= "<span class=\\"calenhead\\" >".get_field(\'fl_event_title\')."</span>";
// debug
// $html .= "<small>".get_post_meta(get_the_id(), $params[\'meta_key\'], 1)."</small>";
$html .= "</div></a></li>";
}
$html .= \'</ul>\';
}
if (!empty($html))
set_transient($transient_hash, $html, 60);
}
return $html;
}
上面的函数使用自定义字段按日期对帖子进行排序
\'fl_date_picker\'
我需要修改此函数,以便用户指定要在短代码中显示的帖子数。为此,我尝试使用:
extract( shortcode_atts( array(
\'post_type\' => \'post\',
\'order\' => \'asc\',
\'orderby\' => \'meta_value\',
\'posts_per_page\'=> 5,
\'meta_key\' => \'fl_date_picker\'
), $atts ) );
然而
fl_date_picker
如果我这样做并且未保留后订单,则不再有效。