jeudi 14 janvier 2021

If Else Shortcode Condition to include or exclude pages ID

I have this code, got it from someone on the internet

  function if_statement($atts, $content = null) {
    if (empty($atts)) return '';
        
    $callable = array_shift($atts);
    if (is_callable($callable)) {
      $condition = (boolean)call_user_func_array($callable, $atts);
    } else {
       throw new Excaption('First argument must be callable!');
    }
   
    $else = '[else]';
    
    if (strpos($content, $else) !== false) {
      list($if, $else) = explode($else, $content, 2);
    } else {
      $if = $content;
      $else = "";
    }
        
    return do_shortcode($condition ? $if : $else);
  }
 
  // register shortcode
  add_shortcode('if', 'if_statement');

This is what it does

[if is_user_logged_in]
You are already logged in. Please use the menu to select proper option.
[else]
Display registration form.
[/if]

My question is that how can I make this function perform a hide/show to pages using is_page() / or any so that it would be like this

[if show_on_page="3,4"]
Please show this on the pages ID 3 & 4
[else]
while this one will show on every other pages excluding 3 & 4
[/if]

so this means that I the first will show on pages 3 & 4 and the else will show to every page except 3 & 4, hope this make sense....

Aucun commentaire:

Enregistrer un commentaire