• Resolved seank123

    (@seank123)


    Trying to use a variable with a php do_sortcode()

    I have this shortcode: [pods name=”menu” where=”weekday.name=’monday'”]{@post_name}<br>[/pods]

    It shows all the Menu posts with the Weekday taxonomy set to Monday.

    I want to use a php DATE function to get the current weekday and have this:

    <?php
    $today = date(l);
    echo do_shortcode( '[pods name="menu" where="weekday.name=' . $today . '"]<div class="menu-block">{@post_name}</div>[/pods]' );
    ?>

    But it returns nothing.

    If I do this it works:

    <?php
    $today = date(l);
    echo do_shortcode( '[pods name="menu" where="weekday.name='monday'"]<div class="menu-block">{@post_name}</div>[/pods]' );
    ?>

    I tried this – but it displays ALL Menus regardless of the Weekday taxonomy:

    <?php
    $today = date(l);
    echo do_shortcode( '[pods name="menu" where="weekday.name='/"$today/"'"]<div class="menu-block">{@post_name}</div>[/pods]' );
    ?>

    What am I doing wrong?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hello @seank123

    I believe your string format should be: /"'.$today.'/"

    Cheers, Jory

    Thread Starter seank123

    (@seank123)

    @keraweb thanks Jory

    That displays the menus but with no Weekday filter – all of the Menu posts are displayed instead of just the ones in the Monday taxonomy

    Thread Starter seank123

    (@seank123)

    OK, trying a different approach – I have this:

    <?php
    
    $today = date("l");
    $time = date("H:i"); 
    
    $mypod = pods( 'menu', $params );
    
    $params = array(
        'limit' => -1, 
      'where'=>"weekday.name = '$today'" ,
    	
    
    );
    
    $mypod = pods( 'menu', $params );
    $end_time = $mypod->field('end_time');
    
    if ($end_time >= date("H:i")) {
    
    	
    while ( $mypod->fetch() ) {
    	    echo $mypod->display( 'post_title' ) . "<br>";
    }
    	
    }
    
    ?>

    The ‘where’=>”weekday.name = ‘$today'” line filters out the posts for the current day properly.

    Now I am trying to filter out by a time field – only display if the ‘end_time’ is greater than the current time.

    But the IF statement still shows all the posts for that day.

    Is there a way to add multiple WHEREs into that $params array?

    Thanks

    Thread Starter seank123

    (@seank123)

    Slept on it and have fixed it:

    <?php
    
    $today = date("l");
    
    $mypod = pods( 'menu', $params );
    
    $params = array(
        'limit' => -1, 
      'where'=>"weekday.name = '$today'" ,
    );
    
    $mypod = pods( 'menu', $params );
    
    while ( $mypod->fetch() ) {
    	$start_time = $mypod->field('start_time');
    	$end_time = $mypod->field('end_time');
    	
    if ($start_time <= date("H:i") && $end_time >= date("H:i")) {	
    	
    	    echo $mypod->display( 'post_title' ) . "<br>";
    }
    }
    
    ?>

    Added the time variables and IF after the WHILE

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @seank123
    Perfect!
    Cheers, Jory

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using variables in do_shortcode()’ is closed to new replies.