$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - If is_page elseif is_page not working like I want it to|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - If is_page elseif is_page not working like I want it to

matteradmin9PV0评论

My code looks like this:

if (is_page('mounting-guides')):
  $guides = get_field('guide_pdfs');
elseif (is_page('user-manuals')):
  $guides = get_field('user_manual_pdfs');
elseif (is_page('cleaning-guides')):
  $guides = get_field('cleaning_guide_pdfs');
  if ($guides):
    // Do stuff
  endif;

Only the last elseif works. If I'm viewing the Mounting Guides or User Manuals page, nothing shows up. If I put my HTML in between the elseif's it works fine:

if (is_page('mounting-guides')):
  $guides = get_field('guide_pdfs');
  if ($guides):
    // Do stuff
  endif;
elseif (is_page('user-manuals')):
  $guides = get_field('user_manual_pdfs');
  if ($guides):
    // Do stuff
  endif;
elseif (is_page('cleaning-guides')):
  $guides = get_field('cleaning_guide_pdfs');
  if ($guides):
    // Do stuff
  endif;

My code looks like this:

if (is_page('mounting-guides')):
  $guides = get_field('guide_pdfs');
elseif (is_page('user-manuals')):
  $guides = get_field('user_manual_pdfs');
elseif (is_page('cleaning-guides')):
  $guides = get_field('cleaning_guide_pdfs');
  if ($guides):
    // Do stuff
  endif;

Only the last elseif works. If I'm viewing the Mounting Guides or User Manuals page, nothing shows up. If I put my HTML in between the elseif's it works fine:

if (is_page('mounting-guides')):
  $guides = get_field('guide_pdfs');
  if ($guides):
    // Do stuff
  endif;
elseif (is_page('user-manuals')):
  $guides = get_field('user_manual_pdfs');
  if ($guides):
    // Do stuff
  endif;
elseif (is_page('cleaning-guides')):
  $guides = get_field('cleaning_guide_pdfs');
  if ($guides):
    // Do stuff
  endif;
Share Improve this question edited Jan 31, 2019 at 21:15 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 31, 2019 at 21:03 sk03sk03 571 silver badge9 bronze badges 1
  • Would it not make more sense to just use a single field named pdfs? – Tom J Nowell Commented Jan 31, 2019 at 21:20
Add a comment  | 

1 Answer 1

Reset to default 0

Well, if you take a look at the indentation of the code, which is correct, it should be pretty clear, why it is so.

if (is_page('mounting-guides')):
  $guides = get_field('guide_pdfs');
  // you don't do anything in this case
elseif (is_page('user-manuals')):
  $guides = get_field('user_manual_pdfs');
  // you don't do anything in this case
elseif (is_page('cleaning-guides')):
  $guides = get_field('cleaning_guide_pdfs');
  // this check is done only if you're on page 'cleaning-guides'
  if ($guides):
    // Do stuff
  endif;

If you want to "do stuff" for all of these pages, it should be like this:

if (is_page('mounting-guides')):
  $guides = get_field('guide_pdfs');
elseif (is_page('user-manuals')):
  $guides = get_field('user_manual_pdfs');
elseif (is_page('cleaning-guides')):
  $guides = get_field('cleaning_guide_pdfs');
endif;

if ($guides):
  // Do stuff
endif;

This way you set $guides if one of conditions is true, and after all of these checks you check if this variable is set and "do stuff".

Post a comment

comment list (0)

  1. No comments so far