How to write a loop and get_field in wordpress?

Hello.

You want to display the cycle if there is an arbitrary field and I can't do it without errors correctly write.

The loop itself without this option I have is output like this:

<?php $populargb = new WP_Query('showposts=5&meta_key=views&orderby=meta_value_num&order=DESC' );
while ( $populargb--->have_posts() ) {
 $populargb->the_post(); ?>
<div class="sidelist">
 <a class="sloy" href="<?php the_permalink() ?>"></a>
 <div class="sidetumb"><?php the_post_thumbnail('thumbnail'); ?></div>
<div class="sidemax"><a><?php the_title(); ?></a>
 <div class="icon-eye"><?php echo get_post_meta ($post--->ID,'views',true); ?></div></div>
</div>
<?php } ?>
<?php wp_reset_query(); ?>


I'm trying to make it so that if there is an arbitrary field "X", the output is displayed as in the example (only the field itself changes the parameter to WP_Query). If no custom fields - the loop does not appear.

I do it with the help of if get_field (in this example, I have it remains the same):

<?php if(get_field('X'))
 $populargb = new WP_Query('showposts='.get_field('X').'&orderby=date&order=DESC' );
 else $populargb = new WP_Query('showposts=6&orderby=date&order=DESC' ); ?>


I tried in every possible way, for example:

<?php if(get_field('X')) //if X is an arbitrary field
 $populargb = new WP_Query('showposts='.get_field('X').'&orderby=date&order=DESC' ); //assign the value in WP_Query
while ( $populargb--->have_posts() ) { //if have posts
 $populargb->the_post(); //get them 
?>
<div class="sidelist">
 <a class="sloy" href="<?php the_permalink() ?>"></a>
 <div class="sidetumb"><?php the_post_thumbnail('thumbnail'); ?></div>
<div class="sidemax"><a><?php the_title(); ?></a>
 <div class="icon-eye"><?php echo get_post_meta ($post--->ID,'views',true); ?></div></div>
</div>
<?php } ?>
<?php endwhile; //end of output 
?>
<?php endif; //end of if condition 
?> 
<?php wp_reset_query(); ?>


But still, as I understand you need to write else. I've tried a lot of different options and the result was always 1 - white screen. Also read about the cycles. Know that you do not understand the logic. So please correct and explain it to me.
June 10th 19 at 14:34
1 answer
June 10th 19 at 14:36
Solution
Already tried?
<?php if(get_field('X')) {
 $populargb = new WP_Query('showposts='.get_field('X').'&orderby=date&order=DESC' );
} else {
 $populargb = new WP_Query('showposts=6&orderby=date&order=DESC' ); 
} ?>

PS White screen means an error occurred. Turn on debug mode.
Yes. In this case, after the code was void.

Wrote:
<?php if(get_field('X')) {
 $populargb = new WP_Query('showposts='.get_field('X').'&orderby=date&order=DESC' );
} else {
 $populargb = new WP_Query('showposts=6&orderby=date&order=DESC' ); 
} ?>
<?php while ( $populargb--->have_posts() ) {
$populargb->the_post(); ?>
<div class="sidelist">
<a class="sloy" href="<?php the_permalink() ?>"></a>
<div class="sidetumb"><?php the_post_thumbnail('thumbnail'); ?></div>
<div class="sidemax"><a><?php the_title(); ?></a>
<div class="icon-eye"><?php echo get_post_meta ($post--->ID,'views',true); ?></div></div>
</div>
<?php } ?>
<?php wp_reset_query();?>


I can not understand how correctly to write. - Lacey.Torphy14 commented on June 10th 19 at 14:39
,
1. so in the logs? there are mistakes?
2. Out of this, if you write <?php var_dump(get_field('x')); ?> ? - alexandrea_Vandervo commented on June 10th 19 at 14:42
1. Fatal error: Uncaught Error: Call to undefined function get_field() in /var/www/mysite.ru/wp-content/themes/mytheme/single.php:53 Stack trace: #0 /var/www/mysite.ru/wp-includes/template-loader.php(74): include() #1 /var/www/mysite.ru/wp-blog-header.php(19): require_once('/var/www/mysite...') #2 /var/www/mysite.ru/index.php(17): require('/var/www/mysite...') #3 {main} thrown in /var/www/mysite.ru/wp-content/themes/mytheme/single.php on line 53
2. Same as paragraph 1. - Lacey.Torphy14 commented on June 10th 19 at 14:45
well here is the answer. You use the get_field function as part of the plugin Advanced Custom Fields? You have it installed and activated? - alexandrea_Vandervo commented on June 10th 19 at 14:48
No, I do not. And I do not remember that I enjoyed them. - Lacey.Torphy14 commented on June 10th 19 at 14:51
then what are You trying to obtain using get_field? Where did You get the name of this function? It is not in core WordPress. - alexandrea_Vandervo commented on June 10th 19 at 14:54
I suggested this feature to access the custom field of Wordpress. Now I understand why it doesn't work. Is it possible somehow differently to refer to an arbitrary field native function or have to put the plugin Advanced Custom Fields and try to use this? - Lacey.Torphy14 commented on June 10th 19 at 14:57
You have to help the function get_post_meta.
Example usage:
<?php $key_1 = get_post_meta( get_the_ID(), 'key_1', true ); ?>
- alexandrea_Vandervo commented on June 10th 19 at 15:00
Thank you! I will now always use! So earned code:

<?php if($key_1 = get_post_meta( get_the_ID(), 'key_1', true )) {
 $populargb = new WP_Query('showposts='.$key_1.'&orderby=date&order=DESC' );
} else {
 $populargb = new WP_Query('showposts=6&orderby=date&order=DESC' ); 
} ?>
<?php while ( $populargb--->have_posts() ) {
$populargb->the_post(); ?>
<div class="sidelist">
<a class="sloy" href="<?php the_permalink() ?>"></a>
<div class="sidetumb"><?php the_post_thumbnail('thumbnail'); ?></div>
<div class="sidemax"><a><?php the_title(); ?></a>
<div class="icon-eye"><?php echo get_post_meta ($post--->ID,'views',true); ?></div></div>
</div>
<?php } ?>
<?php wp_reset_query();?>


But I have one question... please Tell me - what to write else, so if an arbitrary field $key_1 unspecified, then nothing was output? Or in this case, as something else to do? - Lacey.Torphy14 commented on June 10th 19 at 15:03
So?
<?php
if ($key_1 = get_post_meta(get_the_ID(), 'key_1', true)) {
 $populargb = new WP_Query('showposts=' . $key_1 . '&orderby=date&order=DESC');
 while ($populargb--->have_posts()) {
$populargb->the_post();
?>
 <div class="sidelist">
 <a class="sloy" href="<?php the_permalink() ?>"></a>
 <div class="sidetumb"><?php the_post_thumbnail('thumbnail'); ?></div>
 <div class="sidemax"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
 <div class="icon-eye"><?php echo get_post_meta($post--->ID, 'views', true); ?></div>
</div>
</div>
<?php
}
wp_reset_query();
}
?>
- alexandrea_Vandervo commented on June 10th 19 at 15:06
Exactly! Thank You. I thought, what else must be present with if. - Lacey.Torphy14 commented on June 10th 19 at 15:09

Find more questions by tags WordPress