Guest User

custom WP FAQ schema WP no plugins. ddm

a guest
Mar 26th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.76 KB | None | 0 0
  1. in header.php =>
  2. <?php
  3. global $aFaQItems;
  4. $aMeta =get_post_meta($post->ID);
  5. //Extract FAQ items from faq custom fields
  6. $aFaQItems=[];
  7. if (isset($aMeta["faq"]))
  8.     foreach ($aMeta["faq"] as $index=>$sFaq)
  9.     {
  10.         $sAnswer="";
  11.         $sQuestion="";
  12.         $iIndex =   strpos($sFaq, "?", 0);   //get pos of first question mark..
  13.         if ($iIndex == true)
  14.         {
  15.             $sQuestion = substr($sFaq, 0, $iIndex );
  16.             $sAnswer = substr($sFaq, $iIndex+1 );
  17.             $aFaQItems[]=[$sQuestion, $sAnswer];
  18.         }
  19.         else
  20.             continue;
  21.     }
  22.  
  23. ?>
  24.  
  25. also in header.php, before closing </head> tag
  26.  
  27.  
  28. <?php
  29.     //Extract FAQ items from faq custom fields and echo the appropriate jsonld to the page..
  30.      
  31.     if (isset($aMeta["faq"]))
  32.     {
  33.         echo '
  34.         <script type="application/ld+json">
  35.         {
  36.             "@context": "https://schema.org",
  37.             "@type": "FAQPage",
  38.             "mainEntity": [
  39.         ';
  40.  
  41.         $iNumFaqs=sizeof($aFaQItems);
  42.  
  43.         foreach ($aFaQItems as $index=>$aFaq)
  44.         {
  45.     ?>
  46.                 {
  47.                   "@type": "Question",
  48.                   "name": "<?php echo $aFaq[0]?>?",
  49.                   "acceptedAnswer": {
  50.                     "@type": "Answer",
  51.                     "text": "<?php echo addslashes(wpautop($aFaq[1]));?>"
  52.                   }
  53.                 }<?php
  54.         if(($index+1)!=  $iNumFaqs)
  55.             echo ',';
  56.         }
  57.         echo '
  58.    
  59.          ]
  60.         }
  61.         </script>';
  62.     }
  63.    
  64. ?>
  65.  
  66. in review template, or page / post template
  67. <?php
  68.  global $post, $aMeta;
  69.  
  70. ?>
  71.  
  72.  
  73.                         <div class="row no-gutters" id="faqrowhome" style="">
  74.                             <div class="col-md-12 text-center">
  75.                                     <h2 class="moduleheader" style="margin: auto; margin-bottom: 10px;">Frequently Asked Questions</h2>
  76.                                 </div>
  77.                              <div class="col-md-12 " id="">
  78.                                 <?php
  79.                                     foreach ($aFaQItems as $index=>$aFaq)
  80.                                     {
  81.                                      
  82.                                      if ($index>1)
  83.                                         $sIsExpanded="true";
  84.                                      else
  85.                                         $sIsExpanded="false";
  86.                                 ?>
  87.                                   <div class="card">
  88.                                     <div class="card-header" id="heading-<?php echo $index;?>">
  89.                                       <h5 class="mb-0">
  90.                                         <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;?>">
  91.                                           <?php echo $aFaq[0];?>?
  92.                                         </button>
  93.                                       </h5>
  94.                                     </div>
  95.  
  96.                                     <div id="collapse-<?php echo $index;?>" class="collapse <?php if ($index==0) echo 'show';?>" aria-labelledby="heading-<?php echo $index;?>" data-parent="#accordionExample">
  97.                                       <div class="card-body">
  98.                                        <?php echo wpautop( $aFaq[1],   $br = true );?>
  99.                                       </div>
  100.                                     </div>
  101.                                   </div>
  102.  
  103.                                   <?php
  104.                                     }
  105.                                   ?>
  106.                                
  107.                             </div>
  108.  
  109.                         </div>
  110.  
  111. <?php
  112.  
  113. ?>
Add Comment
Please, Sign In to add comment