0

I've tried to look for a specific answer/tutorial on this subject but I'm coming up short and wanted to see what the SE community could suggest. At this point, any guidance whatsoever would be most appreciated.

Basically I'm trying to determine the current page being viewed within a class method I'm setting up, but I haven't had any luck so far. The idea is to enqueue scripts/styles if the appropriate conditions are met: Feature is enabled and set to all pages, or feature enabled and set to display on the current page being viewed.

Here is the latest version of the code I've been working on:

<?php    

if( !class_exists('CTA_Modal') )
{    


    Class CTA_Modal {    

        public $current_post;    

        public function __construct($post_id) 
        {
            $this->current_post = $post_id;
            // $this->load_feature();   
        }    

        public function load_feature()
        {
            $modal_display = array(
                'enabled'   => true,
                'all_pages' => true,
                'pages'     => array()
            );    

            $modal_display['enabled']   = get_field('ctamodal_enabled', 'options');
            $modal_display['all_pages'] = get_field('ctamodal_show_all', 'options');
            $modal_display['pages']     = ( $modal_display['all_pages'] ) ? array() : get_field('ctamodal_show_pages', 'options');    

            if( $modal_display['enabled'] )
            {    

                if( $modal_display['all_pages'] ) 
                {
                    add_action( 'wp_enqueue_scripts', array($this, 'enqueue') );
                }
                else if( !empty($modal_display['pages']) ) 
                {
                    $pages = $modal_display['pages'];    

                    foreach ($pages as $page) {
                        if( $page === $this->current_post )
                        {
                            add_action( 'wp_enqueue_scripts', array($this, 'enqueue') );        
                        }
                    }    

                }

            }
            echo '<!--';
            echo $this->current_post;
            echo '-->';
        }    

        public function enqueue()
        {
            $vendor = get_stylesheet_directory_uri() . '/vendor';
            wp_enqueue_style('fancybox-css', $vendor . '/fancybox/jquery.fancybox.css');
            wp_enqueue_script('fancybox-js', $vendor . '/fancybox/jquery.fancybox.js', array('jquery'), '2.1.5');   
        }    

    }    

}

I can't seem to figure out why declaring global $post; doesn't have the desired effect either. I was originally declaring the global and reseting the query right in this file (i.e. file the code above lives in) and also in the functions.php file, but I wasn't able to get a different result.

Any suggestions/guidance would be most appreciated!

1

https://wordpress.stackexchange.com/a/199997/86773

As the advice suggested here, switching my hook to "wp" instead of "init" allowed me to make the page comparison I wanted using is_page().

0

Have you tried get_the_ID function?

Try this one to get the ID and after that you can get all the info you want. Does it help?

  • Thanks for your reply. I'm very familiar with get_the_ID(), but that isn't what I need here. I actually finally found the answer here (after searching for about an hour...somehow lol): wordpress.stackexchange.com/a/199997/86773 Basically, I needed to use the "wp" hook instead of init, which allowed me to do the is_page() comparison I was trying to perform. – Ian Svoboda Apr 28 '16 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.