Search code examples
phpwordpressloopspodscms

Looping in Wordpress with Pods Plugin


I am trying to create a loop for a group of divs that are being entered in as Pods via Wordpress admin panel.

I have the Pods setup. I could have them setup improperly, but I believe they are setup correctly. I have one Pod and the info is as follows:

Pods Information:

  • Label: advisors
  • Name: advisor
  • Type: Custom Post Type
  • Storage Type: Meta
  • Number of Fields: 2

Field 1 Information:

  • Label: Advisor Name
  • Name: advisor_name
  • Field Type: Plain Text

Field 2 Information:

  • Label: Advisor Title
  • Name: advisor_title
  • Field Type: Plain Text

The HTML I am trying to replicate is:

<div class="small-container text-center advisor-list">
      <h1 class="text-center">Header Title</h1>
      <div class="row  gutter-0 padding-30 text-center vpadding-30 ">
             <div class="box column large-3 vpadding-10 medium-3 text-center">
                    <img  src="/wp-content/uploads/2013/08/icon-star.png">
                    <h2>Name</h2>
                    <h4>Title</h4>   
            </div>
            <div class="box column large-3 vpadding-10 medium-3 text-center">
                    <img  src="/wp-content/uploads/2013/08/icon-star.png">
                    <h2>Name</h2>
                    <h4>Title</h4>   
            </div>
            <div class="box column large-3 vpadding-10 medium-3 text-center">
                    <img  src="/wp-content/uploads/2013/08/icon-star.png">
                    <h2>Name</h2>
                    <h4>Title</h4>   
            </div>
            <div class="box column large-3 vpadding-10 medium-3 text-center">
                    <img  src="/wp-content/uploads/2013/08/icon-star.png">
                    <h2>Name</h2>
                    <h4>Title</h4>   
            </div>
    <button id="showPartners" class="bttn bttn-4 bttn-4a vpadding-30">View Our List of Partners</button>
</div>

This is as far as I have gotten with the PHP:

<div class="small-container text-center advisor-list">
  <h1 class="text-center">Header Title</h1>
  <div class="row  gutter-0 padding-30 text-center vpadding-30 ">
    <?php 
      function get_the_pod($pod_name, $pod_fields, $order = 'name'){
      $item_no=0;
      $pod = new Pod($pod_name);
      $pod->findRecords($order);
      while ($pod->fetchRecord()){
        foreach ($pod_fields as &$field){
          $results[$item_no][str_replace(".guid","",$field)] = $pod->get_field($field);
            if($field == end($pod_fields)){$item_no++;}
        }
      }
      return $results;        
      }

     ?>
         <?php $fields = array('name'); ?>
         <?php $advisors = get_the_pod('advisors', 'name DESC'); ?>
         <?php  foreach($advisors as $advisor){ ?>
           <div class="box column large-3 vpadding-10 medium-3 text-center">
             <img  src="/wp-content/uploads/2013/08/icon-star.png">
             <h2 ><?php echo $item['advisor_name']; ?></h2>
             <h4><?php echo $item['advisor_title']; ?></h4>           
           </div>
     <?php } ?>  

    <button id="showPartners" class="bttn bttn-4 bttn-4a vpadding-30">View Our List of Partners</button>
  </div>
</div>

If anyone has any input, it would be greatly appreciated.

Thanks


Solution

  • You are using Pods 1.X methods. Here is much simpler code using Pods 2.X methods. You will want to check out the docs pages for pods() and pods::find(), which has the infromation about how to set $param for pods(), for more information.

             <?php 
                    $param = array( 'orderby' => 't.name' );
                    $advisors = pods('advisors', $param );
    
                    foreach($advisors as $advisor) { 
                       $advisor_name = $advisor->field( 'advisor_name' );
                       $advisor_title = $advisor->field( 'advisor_title' );
             ?>
                 <div class="box column large-3 vpadding-10 medium-3 text-center">
                      <img src="/wp-content/uploads/2013/08/icon-star.png" />
                      <h2><?php echo $advisor_name; ?></h2>
                      <h4><?php echo $advisor_title; ?></h4>           
                 </div>
            <?php } //end foreach
            ?>