• Resolved Darryl.R

    (@darrylr)


    Hi

    Thanks for a great plugin.

    I’ve got the slider working fine on my site. I use Storefront as my theme.
    Storefront has a widget area below the header. I’ve added the slider shortcode inside a text widget, and the slider displays fine.

    The issue is that I only want to show the slider on the home page and prevent the shortcode from being called when the page is not the home page.

    So I want to add some code to my child theme functions file to prevent it from loading, but I’m not a coder and I’m getting stuck.

    Basically I’m trying to say this:
    If the post is not ID 105 (homepage id), then don’t run [soliloquy] shortcode, else run the shortcode.

    here’s my “code”:

    add_action('soliloquy','hp_slider');
    
    function hp_slider()
    	{
    		if !($post_id='105') {
    			exit();
    		else {
    				do_shortcode('[soliloquy id="1599"]');
    			}
    		}
    	}

    Thank you

    https://www.ads-software.com/plugins/soliloquy-lite/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Darryl ??

    Do you have a link to the page in question? So the slider is running on the homepage but it is also running on all other pages (because I’m presuming the page templates of the theme all call for the same widget?)

    There are a few ways you could do this, 1 I can think of would be to use a child theme, create a unique homepage template and then create a unique homepage widget area.

    That’s just one way, but if you could send me a link to your site, I’d be able to see what’s happening on the site itself.

    Thanks

    Thread Starter Darryl.R

    (@darrylr)

    Hi Akyusa01

    Thanks for the feedback.

    I’m using a child theme, but I would prefer to leave the page templates as stock as possible and rather do any changes via the functions.php file, hence looking for a solution to inhibit the display of the slider to selected pages.

    Hey Darryl,

    Give the following a go, your original function wasn’t structured properly to use $post_id, let me know if the following works better:

    add_action( 'soliloquy', 'hp_slider' );
    function hp_slider() {
    	if ( is_page(105) ) {
    		do_shortcode('[soliloquy id="1599"]');
    	}
    }

    See: https://codex.www.ads-software.com/Conditional_Tags

    Thread Starter Darryl.R

    (@darrylr)

    Hi Erica

    Thanks for the reply!

    It still doesn’t work – but at least the website doesn’t break!

    I think I need to call the function from the widget? My thinking was to prevent the shortcode from running unless its the correct page. So I’m not sure if do_shortcode() is the correct why to go about it?

    Thread Starter Darryl.R

    (@darrylr)

    Ok, looks like i got it to work.

    This is what I needed to do:

    1. Enable PHP in my widget by adding this code to the functions.php file:

    add_filter('widget_text','execute_php',100);
    function execute_php($html){
         if(strpos($html,"<"."?php")!==false){
              ob_start();
              eval("?".">".$html);
              $html=ob_get_contents();
              ob_end_clean();
         }
         return $html;
    }

    2. Function to run the slider shortcode depending on the page ID:

    add_action('soliloquy','hp_slider');
    
    function hp_slider() {
    	if (!is_page(105)) {
    		return $html;
    	}
    		else {
    	 		echo do_shortcode('[soliloquy id="1599"]');
    	}
    }

    3. Call PHP function in the widget:

    <?php hp_slider(); ?>

    Thanks for the help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Loading Slider on specific pages’ is closed to new replies.