$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>widgets - Can't use is_home in template-functions.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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

Post a comment

comment list (0)

  1. No comments so far