Run custom shortcode twice in the same page

I need help with a wordpress question.

I created a custom shortcode that retrieves a list of data inside a table with specific paramenter:

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

}

I want to retrieve all events that has past date compared with the current date.

If I add the shortcode twice in the page, only the first shortcode works and the page stop to display the rest of the content.

Anybody can help me to solve this problem?

Related posts

Leave a Reply

1 comment

  1. wordpress shortcode should return a string, not echoing it
    let me re-arrange your code

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