Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Last active October 20, 2023 18:38
Show Gist options
  • Save finalwebsites/a56a821e093034df8457a41160c5ed8d to your computer and use it in GitHub Desktop.
Save finalwebsites/a56a821e093034df8457a41160c5ed8d to your computer and use it in GitHub Desktop.
WordPress FAQ shortcode with stucutured data notations
<?php
function fws_create_faq_list($atts) {
$atts = shortcode_atts(
array(
'soort' => ''
),
$atts
);
$html = '';
$args = array(
'post_type' => 'faq',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
);
$soort = array_map('trim', explode(',', $atts['soort']));
if (count($soort) > 0) {
$args['tax_query'] = array(
array(
'taxonomy' => 'soort-faq',
'field' => 'slug',
'terms' => $soort
)
);
}
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$html .= '
<div itemscope itemtype="http://schema.org/FAQPage">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$html .= '
<section itemscope itemprop="mainEntity" itemtype="http://schema.org/Question">
<h3 itemprop="name">'.get_the_title().'</h3>
<div itemprop="acceptedAnswer" itemscope itemtype="http://schema.org/Answer">
<div itemprop="text">'.get_the_content().'</div>
</div>
</section>';
}
$html .= '
</div>';
}
wp_reset_query();
return $html;
}
add_shortcode( 'fws_faqlist', 'fws_create_faq_list' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment