Support

Account

Home Forums Front-end Issues get_field stops working when moving to WPML

Solved

get_field stops working when moving to WPML

    • philipp

    • August 15, 2013 at 4:10 pm

    Hello

    We got a working site that uses the following code

     <div class="row product-single-row snippets">
      <?php $post_objects = get_field('snippet_list');
        if( $post_objects ):
        $counter_tab = 0; ?>
        <div class="span12">
         <ul class="nav nav-tabs nav-snippets">
          <?php foreach( $post_objects as $post):
            $counter_tab++;
            setup_postdata($post); ?>
          <li><a href="#snippet<?php echo $counter_tab ?>" data-toggle="tab">
           <?php echo the_field('snippet_label'); ?></a>
          </li>
          <?php endforeach;
            wp_reset_postdata();
            $counter_tab = 0; ?>
         </ul>
         <div class="tab-content">
          <?php foreach( $post_objects as $post):
            $counter_tab++;
            setup_postdata($post); ?>
          <div class="tab-pane fade" id="snippet<?php echo $counter_tab ?>">
           <?php echo get_the_content(); ?>
          </div>
          <?php endforeach; ?>
         </div>
        </div>
        <?php wp_reset_postdata();
          endif; ?>
      </div>           
    

    The “snippet_list” is a Post Object and the “snippet_label” is a text type for this Post type.

    When we transfered the whole content (using XML export & import) and the theme to a new development server that uses WPML for translation, the above code stopped working. We also switched to WordPress 3.6 at the development server. We also tried to add some new pages but that didn’t work either.

    Does anybody have an idea whats wrong here?

    • Elliot

    • August 16, 2013 at 1:08 pm

    Hi @philipp

    Your first step is to debug variables:

    
    <?php
    
    // test field
    $post_objects = get_field('snippet_list');
    
    echo '<pre>';
    	print_r( $post_objects );
    echo '</pre>';
    die;
    
    //test post_id
    global $post;
    
    echo '<pre>';
    	print_r($post->ID);
    echo '</pre>';
    die;
    
     ?>
    

    Are these variables producing the correct data?

    • philipp

    • August 16, 2013 at 8:08 pm

    Hi elliot

    Thanks for your hints. This set me on the right track.

    I had to change two things. First i had to use
    $post_objects = get_field('snippet_list',false,false);
    to get the correct arry of post Ids

    Second i had to use

    foreach( $post_objects as $postID):
       $post = get_post($postID);

    to get the correct posts.

    So the whole code looks like this at the end:

    <div class="row product-single-row snippets">
      <?php $post_objects = get_field('snippet_list',false,false);
        if( $post_objects ): 
        $counter_tab = 0; ?>
      <div class="span12">
       <ul class="nav nav-tabs nav-snippets">
        <?php foreach( $post_objects as $postID):
          $post = get_post($postID); 
          $counter_tab++;
          setup_postdata($post); ?>
        <li><a href="#snippet<?php echo $counter_tab ?>" data-toggle="tab"><?php the_field('snippet_label'); ?></a></li>
        <?php endforeach; 
          $counter_tab = 0; ?>
       </ul>
       <div class="tab-content">
        <?php foreach( $post_objects as $postID):
          $post = get_post($postID); 
          $counter_tab++;
          setup_postdata($post); ?>
        <div class="tab-pane fade" id="snippet<?php echo $counter_tab ?>">
         <?php echo get_the_content(); ?>
        </div>
        <?php endforeach; ?>
       </div> 
      </div>
      <?php wp_reset_postdata();
        endif; ?>
     </div>

    I’m not sure, this is the optimal solution, but at least it works the way I expect.

    Thanks again. I realy appreciate your help.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.