使用html作为短码属性

时间:2013-09-28 作者:Harish Chouhan

我创建了一个快捷码,可以根据meta\\u键检索并显示推荐的热门帖子。有不同的属性,除了一次属性的内容是HTML之外,所有属性都可以工作。

完整代码为:

function dot_irt_top_posts ( $atts ) {

// get our variable from $atts
extract(shortcode_atts(array(
    \'before\' => \'<li>\',
    \'after\' => \'</li>\',
    \'number\' => \'10\',
    \'post_type\' => \'post\',
    \'year\' => \'\',
    \'monthnum\' => \'\',
    \'show_count\' => \'1\',
), $atts));

global $wpdb;

$request = "SELECT * FROM $wpdb->posts, $wpdb->postmeta";
$request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";

if ($year != \'\') {
    $request .= " AND YEAR(post_date) = \'$year\'";
}

if ($monthnum != \'\') {
    $request .= " AND MONTH(post_date) = \'$monthnum\'";
}

$request .= " AND post_status=\'publish\' AND post_type=\'$post_type\' AND meta_key=\'_recommended\'";
$request .= " ORDER BY $wpdb->postmeta.meta_value+0 DESC LIMIT $number";
$posts = $wpdb->get_results($request);

$return = \'\';


foreach ($posts as $item) {
    $post_title = stripslashes($item->post_title);
    $permalink = get_permalink($item->ID);
    $post_count = $item->meta_value;

    $return .= $before;
    $return .= \'<a href="\' . $permalink . \'" title="\' . $post_title.\'" rel="nofollow">\' . $post_title . \'</a> \';

    if ( $show_count == \'1\') {
        $return .= \'<span class="votes">\' . $post_count . \'</span> \';
    }

    //$return .= get_the_post_thumbnail($item->ID, \'showcase-thumbnail\');
    $return .= $after;

}
return \'<div class="top_10_posts">\' . $return . \'</div>\';

 }
 add_shortcode(\'irt_top_posts\',\'dot_irt_top_posts\');
现在,如果我使用下面的短代码,数据将以列表的形式正确返回:

 <ul>
     [irt_top_posts post_type=\'showcase\' number=\'10\']
 </ul>
但如果我设置“before”(在之前)(&;)“属性”之后,它们将显示为内容,而不是作为html执行:

 <div>
     [irt_top_posts post_type=\'showcase\'  before=\'<div>\' after=\'</div>\' number=\'10\']
 </div>
我是否需要一些其他函数来禁用转义HTML?

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

仅传递元素名称并将其转换为快捷码中的标记:

[irt_top_posts post_type=\'showcase\'  container=\'div\' number=\'10\']
在您的回拨中:

$return "<$container>$return</$container>";

SO网友:epilektric

Try using html_entity_decode().

function foo ( $atts ) {

    extract(shortcode_atts(array(
        \'before\' => \'<li>\',
        \'after\' => \'</li>\',
    ), $atts));

    $before = html_entity_decode( $before );
    $after = html_entity_decode( $after );

    $return = $before . \'something\' . $after;

    return $return;

}
结束

相关推荐

Custom field in a shortcode?

我无法将图像从我的一个字段转换为短代码(它与get_the_post_thumbnail($post->ID, \'thumbnail\') 但我需要字段中的值udstiller logo.function logo_shortcode( $atts ) { extract( shortcode_atts( array( \'limit\' => \'50\', \'orderby\' => \'name\',