Skip to main content
Code Review

Return to Answer

replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

Your get_data() function is broken. You are overwriting $results['title'] and $results['field'] on each iteration, instead of appending it to $results array. Moreover, if no posts existed to loop over, you'd get an "undefined variable $results" notice. Let's fix both issues:

function get_data(){
 $results = array(); // <- Initialize array to prevent "undefined variable" notices
 if (have_posts()) : 
 while (have_posts()) : the_post();
 $results[] = array( // <- Append a new result set to the results
 'title' => get_the_title(),
 'field' => get_field('field'),
 );
 endwhile;
 endif;
 return $results;
}

About the code in your template file, it'd be better not to use query_posts() at all. Use the pre_get_posts hook to alter the main query if needed. Please, see this question over at WordPress Answers see this question over at WordPress Answers.

Your get_data() function is broken. You are overwriting $results['title'] and $results['field'] on each iteration, instead of appending it to $results array. Moreover, if no posts existed to loop over, you'd get an "undefined variable $results" notice. Let's fix both issues:

function get_data(){
 $results = array(); // <- Initialize array to prevent "undefined variable" notices
 if (have_posts()) : 
 while (have_posts()) : the_post();
 $results[] = array( // <- Append a new result set to the results
 'title' => get_the_title(),
 'field' => get_field('field'),
 );
 endwhile;
 endif;
 return $results;
}

About the code in your template file, it'd be better not to use query_posts() at all. Use the pre_get_posts hook to alter the main query if needed. Please, see this question over at WordPress Answers.

Your get_data() function is broken. You are overwriting $results['title'] and $results['field'] on each iteration, instead of appending it to $results array. Moreover, if no posts existed to loop over, you'd get an "undefined variable $results" notice. Let's fix both issues:

function get_data(){
 $results = array(); // <- Initialize array to prevent "undefined variable" notices
 if (have_posts()) : 
 while (have_posts()) : the_post();
 $results[] = array( // <- Append a new result set to the results
 'title' => get_the_title(),
 'field' => get_field('field'),
 );
 endwhile;
 endif;
 return $results;
}

About the code in your template file, it'd be better not to use query_posts() at all. Use the pre_get_posts hook to alter the main query if needed. Please, see this question over at WordPress Answers.

Source Link
Geert
  • 111
  • 2

Your get_data() function is broken. You are overwriting $results['title'] and $results['field'] on each iteration, instead of appending it to $results array. Moreover, if no posts existed to loop over, you'd get an "undefined variable $results" notice. Let's fix both issues:

function get_data(){
 $results = array(); // <- Initialize array to prevent "undefined variable" notices
 if (have_posts()) : 
 while (have_posts()) : the_post();
 $results[] = array( // <- Append a new result set to the results
 'title' => get_the_title(),
 'field' => get_field('field'),
 );
 endwhile;
 endif;
 return $results;
}

About the code in your template file, it'd be better not to use query_posts() at all. Use the pre_get_posts hook to alter the main query if needed. Please, see this question over at WordPress Answers.

lang-php

AltStyle によって変換されたページ (->オリジナル) /