Find out if a page is closed in template

WPPizza – A Restaurant Plugin for WordPress Support Add-Ons Extensions Timed Menu Support Find out if a page is closed in template

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #6790
    florian
    Participant

      Hi Olly

      I’m using Timed Menu with install option 1 ‘pages’.
      I have a custom page in my template where I use query_posts() to loop trough a number of other pages and display a link to them. During that loop I would like to find out if a page is closed or open right now.

      I saw that in wppizza-timed-menu.php you are adding some filter methods like
      add_filter(‘get_pages’, array($this,’wppizza_tm_exclude_pages’));/*exclude pages from page widgets*/
      that removes pages from being displayed.

      What I want to do is not remove the pages, but just display some information if the page is closed or open.

      Basically I need a function like this:

      function is_page_active ($pageid) {
      //check wppizza_tm settings
      return $pageState;
      }

      The only way that came to mind so far was to use query_posts and get_pages and then compare the results. but that’s definitely not the moste elegant way. 🙂

      Is there a way to do this?

      Thanks in advance
      Florian

      #6792
      Olly
      Admin & Mod

        depends a bit on what modifications you did to your template, but something like this i would have thought
        (choose whatever action hook in the template you need )

        
        add_action('wppizza_loop_outside_end','my_custom_function',10,1);
        function my_custom_function($query){
        if(count($query->posts)<=0){
        /**no items, do stuff**/	
        }
        /*or just use (probably better here)**/
        if($query->post_count<=0){
        /**no items, do stuff**/
        }
        }
        

        PS: $query is an object with all sorts of things (i.e whatever the query returns) so if ->post or ->post_count is not what you need, maybe something else will do

        #6795
        florian
        Participant

          This would work on the page where the actual menu items are being displayed right? because that’s where the wppizza-loop-xxx.php files get executed.
          But on the page I’m currently displaying, I don’t display any menu items. Just all the pages that contain menu items. To explain it a little more:

          This page displays a list of pages:
          http://www.ynot-takeout.com/restaurants/

          I’m putting out links to the pages with something like this:
          query_posts(array(‘showposts’ => 99, ‘post_parent’ => $this_page_id, ‘post_type’ => ‘page’, ‘order’ => ‘ASC’, ‘orderby’ => ‘title’));

          while (have_posts()) { 
          //display <a>"><?php echo the_title() ?></a>                 
          }

          But I would like to change that loop to something like this:

          while (have_posts()) { 
          $id = the_id();
          $class = 'open';
          if (is_the_page_closed($id)){
          $class= 'closed';
          }
          //<a class="<?php echo $class ?>" href="<?php echo the_permalink() ?>"><?php echo the_title() ?></a>
          }

          if the server has to run through all the menu items first, that would make it necessary to query 1300 items and I don’t think that is going to help performance…
          Or if I think of another solution, would it be possible for me to read the wppizza-tm-options to get the something like options[‘pageid’][‘openingtimes’] and then check by myself if the page is currently open or closed?

          Thanks

          #6796
          Olly
          Admin & Mod

            still not sure exactly what you are after but let me guess a bit:

            that page you are referring to seems to be just a list of subsites (i.e blogs) rather than menu items so I am not sure how the timed menu comes into play here ?!
            or are you actually NOT using a multisite setup for this sort of thing ?
            (or maybe I am just misunderstanding things)

            in any case , thinking out loud and also writing a bit for anyone else that comes across this:

            *if* you are using a multisite setup you would probably want to do/run a
            switch_to_blog / restore_current_blog loop and within that do the following (of the top of my head, but should work). something like this:

            
            if ( is_multisite() ) {
            $blogs = $wpdb->get_results("SELECT blog_id FROM {$wpdb->blogs}", ARRAY_A);
            if ($blogs) {
            foreach($blogs as $blog) {
            switch_to_blog($blog['blog_id']);
            /*get wppizza options**/
            $options=get_option('wppizza');
            /*check if open**/
            $isOpen=wpizza_are_we_open($options['opening_times_standard'],$options['opening_times_custom'],$options['times_closed_standard']);
            if($isOpen){
            /*do stuff if open**/
            }else{
            /*do stuff if closed**/	
            }
            restore_current_blog();
            }}
            }
            

            (admittedly that wpizza_are_we_open function could be more user friendly , and i might just add another function that produces the same result without having to get the $options here seperately, but that’s for another day)

            however, if the story is – as i suspect – you are *NOT* using a multisite setup and you are using the timed menu to open/close your individual shops by making pages unavailable – if every page refers to a restaurant/shop (as opposed to opening times per shop), then something like the following *might* be what you are looking for

            
            /**************
            get the timed menu options
            **************/
            $wpptmoptions=get_option('wppizza_timed_menu');
            /**************
            get all the menu items on a page (before timed menu settings have been taken into consideration) 
            returns array with the keys being the page id and the values being the menu items
            ****************/
            $wpptmMenuItemsOnPage=$wpptmoptions[items_on_pages];
            /**************
            get the timed menu settings
            returns  array with the settings you have set in the timed menu (i.e dates/times/days/pages/menuitems etc
            **************/
            $wpptmSettings=$wpptmoptions[timed_items];
            

            so, assuming you are just excluding whole pages (as a page would be all items for a restaurant here if i interpret things correctly)
            something like this (there are probably other ways to code this too, but as a dummy example)

            
            $wpptmCurrentTime=current_time('timestamp');/*wordpress time*/
            $shopOpen=array();
            foreach($wpptmSettings as $tm){
            if($tm[enabled]){
            /*
            check here if $wpptmCurrentTime is within set time date etc
            probably want to convert the below to timestamps to compare to $wpptmCurrentTime
            returns stuff like
            $tm[start_date] => 2014-12-02//set start date
            $tm[end_date] => 2014-12-25//set end date
            $tm[start_time] => 08:45//set start tim
            $tm[end_time] => 17:45	//set end time
            $tm[day]=>Array([1] => 1,[2] => 2) //returns array of set days 0 to 6 
            $tm[enabled] =>1
            etc
            */
            if(time-date-check-passed[you'd need to write this check]]){
            $shopOpen=array_merge($shopOpen,array_keys($tm['pages']));
            }
            }}
            /**flip keys to be able to use isset as opposed to in_array (and also end up with unique keys)**/
            $shopOpen=array_flip($shopOpen));
            
            
            /**within your pages loop you could now do smth like**/
            if(isset($shopOpen[$pageId])){
            /**do stuff if open**/
            }else{
            /**do stuff if closed**/	
            }
            

            `

            #6798
            florian
            Participant

              Your second assumption is true. We don’t use a multisite setup. The links on the referred page are just child posts (‘pages’ to use the wordpress names) and they each represents a restaurant. The opening times of those restaurant pages are handled by the TimedMenu plugin, not the opening times of wppizza.

              So your second example how to get the options and then implement the check will be my approach.

              Thanks for laying it out how to do it Olly!

              (admittedly that wpizza_are_we_open function could be more user friendly , and i might just add another function that produces the same result without having to get the $options here seperately, but that’s for another day)

              Right. After all I wanted to know if there already is such a function. 🙂

              #6826
              Olly
              Admin & Mod

                instead of the “wpizza_are_we_open” function referred to previously , you should now (as of v2.11.5.10) simply be able to use

                
                wpizzaShopOpen();
                

                without any arguments and omit

                
                $options=get_option('wppizza');
                

                there

              Viewing 6 posts - 1 through 6 (of 6 total)
              • The topic ‘Find out if a page is closed in template’ is closed to new replies.