函数短码-设置参数

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

function get_gifts_posts($params, $content){
        $default_params = array(
            \'post_type\'     => \'post\',
            \'order\'         => \'desc\',
            \'orderby\'       => \'date\',
            \'posts_per_page\'=> 5,
        );

        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);

        $transient_hash = \'get_gifts_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);
                        $html .= "<a href=".get_permalink()."><div id=\\"calimgf\\" style=\\"background:url({$thumb[\'0\']}) no-repeat left top;\\">";
                    }

                    $html .= "<span class=\\"calendate\\" >".get_field(\'fl_price_ex\')."</span>";
                    $html .= "<span class=\\"calenhead\\" >".get_field(\'fl_title\')."</span>";
                    $html .= "</div></a></li>";
                }
                $html .= \'</ul>\';
            }

            if (!empty($html))
                set_transient($transient_hash, $html, 60);

        }
        return $html;
    }   
如何指定要在短代码中显示为参数的帖子数量?

现在我默认显示5篇帖子\'posts_per_page\'=> 5,

如果用户没有指定帖子的数量,那么该函数在默认情况下应继续显示5篇帖子。

1 个回复
最合适的回答,由SO网友:helgatheviking 整理而成

有一个基本的shortcode example in the Codex.

function bartag_func( $atts ) {
     extract( shortcode_atts( array(
          \'foo\' => \'no foo\',
          \'baz\' => \'default baz\'
     ), $atts ) );
     return "foo = {$foo}";
}
add_shortcode( \'bartag\', \'bartag_func\' );
数组是短代码默认值,用于填充任何未在短代码参数中特别传递的属性。应用到您的代码时,它看起来像:

function get_gifts_posts($atts, $content){
    extract( shortcode_atts( array(
        \'post_type\'     => \'post\',
        \'order\'         => \'desc\',
        \'orderby\'       => \'date\',
        \'posts_per_page\'=> 5,
    ), $atts ) );

// function truncated
// but you may now use $post_type, $order, $orderby and $posts_per_page variables

}   
add_shortcode(\'super_awesome_shortcode\', \'get_gifts_posts\' );

Update

现在可以这样使用快捷码:

[super_awesome_shortcode posts_per_page="10"] 其中$posts\\u per\\u页面等于10

[super_awesome_shortcode]

其中$posts\\u per\\u page将等于默认值5。

shortcode_atts() 将用户短代码属性与已知属性相结合,并在需要时填写默认值。

结束

相关推荐

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\