修改函数短码_ATTS

时间:2014-02-25 作者:JoaMika

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 如果我这样做并且未保留后订单,则不再有效。

1 个回复
SO网友:s_ha_dum

你误用了shortcode_atts().

您需要在firstargument中向shortcode传递一个默认值数组,您已经完成了这一操作$params 但由于某种原因,你正在通过$atts, 函数中未定义此外,您正在使用extract() with将从提供的数组中创建变量。然而,您的代码并不是为了使用这些提取的变量而编写的。它仍然使用$params. extract() 在我看来,这是一种令人困惑的代码编写方式。坚持$params.

概念验证:

function my_sc_cb($params = \'\') {
  $params = shortcode_atts( 
    array(
      \'post_type\'     => \'post\',
      \'order\'         => \'asc\',
      \'orderby\'       => \'meta_value\',
      \'posts_per_page\'=> 5,
      \'meta_key\'      => \'fl_date_picker\'
    ),
    $params
  );
  var_dump($params); // debugging
}
add_shortcode(\'msccb\',\'my_sc_cb\');
do_shortcode(\'[msccb post_type="page"]\');

结束

相关推荐

How do I make a shortcode?

边走边学。。。我已经构建了这个功能,但我需要它作为一个短代码。我该怎么做?$args = array( \'taxonomy\' => \'product_cat\' ); $terms = get_terms(\'product_cat\', $args); $count = count($terms); $i=0; if ($count > 0) { $cape_list = \'<p class=\"my_term-archive\