在同一页面中运行两次自定义短代码

时间:2015-01-15 20:38:27

标签: php wordpress shortcode

我需要wordpress问题的帮助。

我创建了一个自定义短代码,用于检索具有特定参数的表格内的数据列表:

add_shortcode("archive", "archive_render");
function archive_render($atts) {

extract(shortcode_atts(array(
    "rientro" => "no",
    "year" => "",
), $atts));

global $wpdb;

$rientro == "si" ? $rientro = "yes" : "no";

$query = "SELECT event_name FROM wp_em_events WHERE EXTRACT(YEAR FROM event_end_date) = ".$year." AND event_end_date < CURDATE()";
$pasts_event = $wpdb->get_col($query);

function get_pasts_event( $pasts_event ){

    foreach ( $pasts_event as $past_event_slug ) {
        $output .= "<li><a href='".get_site_url()."/eventi/".$past_event_slug."'>$past_event_slug</a></li>";
    }

    return $output;
}

$string = '[one_third last="'.$rientro.'" class="" id=""][accordian class="" id=""][toggle title="'.$year.'" open="no"]<ul>'.get_pasts_event($pasts_event).'</ul>[/toggle][/accordian][/one_third]';

echo do_shortcode( $string );

}

我想检索过去日期与当前日期相比的所有事件。

如果我在页面中添加两次短代码,则只有第一个短代码有效,页面会停止显示内容的其余部分。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

wordpress短代码应该返回一个字符串,而不是回显它 让我重新安排你的代码

function get_pasts_event( $pasts_event ){

    foreach ( $pasts_event as $past_event_slug ) {
        $output .= "<li><a href='".get_site_url()."/eventi/".$past_event_slug."'>$past_event_slug</a></li>";
    }

    return $output;
}

add_shortcode("archive", "archive_render");
function archive_render($atts) {

    extract(shortcode_atts(array(
        "rientro" => "no",
        "year" => "",
        ), $atts));

    global $wpdb;

    $rientro == "si" ? $rientro = "yes" : "no";

    $query = "SELECT event_name FROM wp_em_events WHERE EXTRACT(YEAR FROM event_end_date) = ".$year." AND event_end_date < CURDATE()";
    $pasts_event = $wpdb->get_col($query);


    $string = '[one_third last="'.$rientro.'" class="" id=""][accordian class="" id=""][toggle title="'.$year.'" open="no"]<ul>'.get_pasts_event($pasts_event).'</ul>[/toggle][/accordian][/one_third]';

    return $string;
}