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

admin2025-06-03  3

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".

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748956347a315138.html

最新回复(0)