widgets - Can't use is_home in template-functions.php

admin2025-06-06  5

I'm trying to load a slider in my header, but only on the homepage.

I'm using The Ultralight template if that helps.

I'm trying to (in template-functions.php) do the following:

<?php if ( is_page( 'home' ) ) : ?>
            dynamic_sidebar( 'Homepage Widget' ); 
        <?php endif; ?>

But this doesn't work. Now from a quick google, it seems I need to enqueue the request, but when I try this, I get a syntax error:

<?php
        add_action( 'wp', 'wpse47305_check_home' );
        function wpse47305_check_home() {
            if ( is_home() )
                add_action( 'wp_enqueue_scripts', 'my_scripts' );
        }

        function my_scripts() {
         dynamic_sidebar( 'Homepage Widget' ); 
        }
        ?>

Edit: So this doesn't seemingly add my sidebar in where I was expecting it to load in. I presume i've misunderstood the concept here.

I'm trying to load a slider in my header, but only on the homepage.

I'm using The Ultralight template if that helps.

I'm trying to (in template-functions.php) do the following:

<?php if ( is_page( 'home' ) ) : ?>
            dynamic_sidebar( 'Homepage Widget' ); 
        <?php endif; ?>

But this doesn't work. Now from a quick google, it seems I need to enqueue the request, but when I try this, I get a syntax error:

<?php
        add_action( 'wp', 'wpse47305_check_home' );
        function wpse47305_check_home() {
            if ( is_home() )
                add_action( 'wp_enqueue_scripts', 'my_scripts' );
        }

        function my_scripts() {
         dynamic_sidebar( 'Homepage Widget' ); 
        }
        ?>

Edit: So this doesn't seemingly add my sidebar in where I was expecting it to load in. I presume i've misunderstood the concept here.

Share Improve this question edited Nov 19, 2018 at 10:17 Andrew Berry asked Nov 19, 2018 at 10:10 Andrew BerryAndrew Berry 991 bronze badge 3
  • 1 if( is_home() || is_front_page() ) might be helpful. Because by default is_home() detects the blog page. And I don't think, enqueuing is necessary here. If the problem is specific to the Ultralight theme, then I'm afraid, the question would be off-topic here. – Mayeenul Islam Commented Nov 19, 2018 at 10:34
  • 2 You seem to be mixing up a few things. dynamic_sidebar() goes into the template itself, wherever you want it to appear. If it's in the header, then that would be header.php (probably, it depends on the theme). But to use dynamic sidebar you first need to register it so that you can put widgets in it. Use register_sidebar() to do that. See here for an example. I'm not sure what you're trying to do with scripts here. How are you trying to add this slider, is it a widget? – Jacob Peattie Commented Nov 19, 2018 at 10:35
  • solve your problem ? – vikrant zilpe Commented Nov 19, 2018 at 13:14
Add a comment  | 

2 Answers 2

Reset to default 0

I havent tested yet but try using this, $url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; if is same as "example"

Yes, as @Jacob Peattie mentioned, I had got myself confused. I needed to put this in my header.php rather than in the template-functions.php

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749166422a316922.html

最新回复(0)