Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- in header.php =>
- <?php
- global $aFaQItems;
- $aMeta =get_post_meta($post->ID);
- //Extract FAQ items from faq custom fields
- $aFaQItems=[];
- if (isset($aMeta["faq"]))
- foreach ($aMeta["faq"] as $index=>$sFaq)
- {
- $sAnswer="";
- $sQuestion="";
- $iIndex = strpos($sFaq, "?", 0); //get pos of first question mark..
- if ($iIndex == true)
- {
- $sQuestion = substr($sFaq, 0, $iIndex );
- $sAnswer = substr($sFaq, $iIndex+1 );
- $aFaQItems[]=[$sQuestion, $sAnswer];
- }
- else
- continue;
- }
- ?>
- also in header.php, before closing </head> tag
- <?php
- //Extract FAQ items from faq custom fields and echo the appropriate jsonld to the page..
- if (isset($aMeta["faq"]))
- {
- echo '
- <script type="application/ld+json">
- {
- "@context": "https://schema.org",
- "@type": "FAQPage",
- "mainEntity": [
- ';
- $iNumFaqs=sizeof($aFaQItems);
- foreach ($aFaQItems as $index=>$aFaq)
- {
- ?>
- {
- "@type": "Question",
- "name": "<?php echo $aFaq[0]?>?",
- "acceptedAnswer": {
- "@type": "Answer",
- "text": "<?php echo addslashes(wpautop($aFaq[1]));?>"
- }
- }<?php
- if(($index+1)!= $iNumFaqs)
- echo ',';
- }
- echo '
- ]
- }
- </script>';
- }
- ?>
- in review template, or page / post template
- <?php
- global $post, $aMeta;
- ?>
- <div class="row no-gutters" id="faqrowhome" style="">
- <div class="col-md-12 text-center">
- <h2 class="moduleheader" style="margin: auto; margin-bottom: 10px;">Frequently Asked Questions</h2>
- </div>
- <div class="col-md-12 " id="">
- <?php
- foreach ($aFaQItems as $index=>$aFaq)
- {
- if ($index>1)
- $sIsExpanded="true";
- else
- $sIsExpanded="false";
- ?>
- <div class="card">
- <div class="card-header" id="heading-<?php echo $index;?>">
- <h5 class="mb-0">
- <button class="btn btn-link <?php if ($index>1) echo 'collapsed';?>" type="button" data-toggle="collapse" data-target="#collapse-<?php echo $index;?>" aria-expanded="<?php echo $sIsExpanded;?>" aria-controls="collapse-<?php echo $index;?>">
- <?php echo $aFaq[0];?>?
- </button>
- </h5>
- </div>
- <div id="collapse-<?php echo $index;?>" class="collapse <?php if ($index==0) echo 'show';?>" aria-labelledby="heading-<?php echo $index;?>" data-parent="#accordionExample">
- <div class="card-body">
- <?php echo wpautop( $aFaq[1], $br = true );?>
- </div>
- </div>
- </div>
- <?php
- }
- ?>
- </div>
- </div>
- <?php
- ?>
Add Comment
Please, Sign In to add comment