Skip to content

Commit

Permalink
fix: switch to wp_query in shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
danbleile committed Nov 1, 2023
1 parent 28e47c3 commit 2757bf2
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions wsuwp-html-snippets.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,35 @@ public function display_html_snippet( $atts ) {
if ( ( empty( $atts['id'] ) || 0 === absint( $atts['id'] ) ) ) {
return '';
}


$post = get_post( $atts['id'] );
$args = array(
'p' => $atts['id'],
'post_type' => 'wsu_html_snippet',
);

$the_query = new WP_Query( $args );

ob_start();

if ( $the_query->have_posts() ) {

while ( $the_query->have_posts() ) {
$the_query->the_post();

the_content();
}
}

// Restore original Post Data.
wp_reset_postdata();

$content = ob_get_clean();

if ( is_multisite() && ms_is_switched() ) {
restore_current_blog();
}

if ( ! $post || $this::$content_type_slug !== $post->post_type ) {
return '';
}

if ( in_array( $atts['container'], array( 'div', 'span' ) ) ) {
$container_open = '<' . $atts['container'];

Expand All @@ -159,19 +177,12 @@ public function display_html_snippet( $atts ) {

$container_open .= '>';

$content = $container_open . apply_filters( 'the_content', $post->post_content ) . '</' . $atts['container'] . '>';
$content = do_shortcode( $content );
} else {
$content = apply_filters( 'the_content', $post->post_content );
$content = do_shortcode( $content );
}

$content = $container_open . $content . '</' . $atts['container'] . '>';

if ( ! has_filter( 'the_content', 'wpautop' ) ) {
$content = wpautop( $content );
}

return $content;

}

/**
Expand Down

0 comments on commit 2757bf2

Please sign in to comment.