skip to Main Content

I’m using Advanced Custom Fields in WordPress and using PHP to write out the title of an object. But, instead of getting the title on its own, the code is retrieving the title plus presumably another title. It’s not clear where the other title is coming from. I’ve done a var_dump of the object to show what it contains, and I’ve added an extra echo with just a "." which shows it’s looping through something else but where is it when its not in the var_dump?

This is the code:

 foreach ($actions as $action ){ ?>
  <?php var_dump($action); ?>
  <?php  foreach ($action['lp_earning_method'] as $post){
    
     setup_postdata($post);  
      echo the_title();  
      wp_reset_postdata(); 
      echo ".";

  } ?>

Here is the contents of the var_dump.

array (size=1)
  'lp_earning_method' => 
    object(WP_Post)[19675]
      public 'ID' => int 125527
      public 'post_author' => string '1' (length=1)
      public 'post_date' => string '2022-05-30 14:33:18' (length=19)
      public 'post_date_gmt' => string '2022-05-30 13:33:18' (length=19)
      public 'post_content' => string '' (length=0)
      public 'post_title' => string 'Visit our website' (length=17)
      public 'post_excerpt' => string '' (length=0)
      public 'post_status' => string 'publish' (length=7)
      public 'comment_status' => string 'closed' (length=6)
      public 'ping_status' => string 'closed' (length=6)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'dbd_loyalty-lws_woorewards_pro_events_visit' (length=43)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2022-09-06 16:54:25' (length=19)
      public 'post_modified_gmt' => string '2022-09-06 15:54:25' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'post_parent' => int 125516
      public 'guid' => string 'https://adeline.templweb.com/news/lws-wre-event/dbd_loyalty/dbd_loyalty-lws_woorewards_pro_events_visit/' (length=104)
      public 'menu_order' => int 0
      public 'post_type' => string 'lws-wre-event' (length=13)
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)

Here is what it’s writing out on the front end.

Visit our website……………..Loyalty Points…….

I want it to only output what I can see is the post-title in the object "Visit our website".

2

Answers


  1. Chosen as BEST ANSWER
      foreach ($actions as $action ){ 
         $post = $action['lp_earning_method'];
         setup_postdata($post);  
          echo the_title();  
          wp_reset_postdata(); 
      } ?>
    

    There was an extra loop not required.


  2. It seems that "$actions" is an array that contains more posts. At looping it you are having that problem. Check it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search