Sidebars generating a 1 after HTML
-
Hello all,
Hoping I’m using the right sub-forum.
I am developing a theme and I have 2 sidebars, both doing the same thing. I haven’t completed the styling yet, so if you go visit, the primary sidebar will be at the bottom left, and the second sidebar I am using to hold the search bar.
You will notice that there is a “1” under each one, not contained in any tag or anything. Just a random 1, and for the life of me I cannot figure out where it’s coming from.
I’ve checked the register_sidebar and get_sidebar documentation to see if there is a setting I am not turning off. I’ve also searched the vastness of the internet, but I guess I’m not wording my search parameters correctly.
Here is the relevant functions.php code:
function american_cyanide_sidebar() { register_sidebar( array( 'name' => esc_html__( 'Primary Sidebar', 'american-cyanide' ), 'id' => 'sidebar-primary', 'descritpion' => esc_html__( 'Widgets added here will appear on the side of the home page.' ), 'before_widget' => '<section id="%1s" class="widget %2s">', 'after_widget' => '</section>', 'before_title' => '<h2 class="sidebar-widget-title">', 'after_title' => '</h2>', ) ); register_sidebar(array( 'name' => esc_html__( 'Search Sidebar', 'american-cyanide' ), 'id' => 'sidebar-search', 'description' => esc_html__( 'Space for the Search Widget.' ), 'before_widget' => '<section id="%1s" class="widget %2s">', 'after_widget' => '</section>', 'before_title' => '<h6 class="search-bar-title">', 'after_title' => '</h6>', ));And here is the code from primary and search php files, respectively:
<?php if ( is_active_sidebar( 'sidebar-primary' ) ) { ?> <ul class="sidebar"> <?= dynamic_sidebar( 'sidebar-primary' ); ?> </ul> <?php } ?><?php if ( is_active_sidebar( 'sidebar-search' ) ) { ?> <div class="search"> <?= dynamic_sidebar( 'sidebar-search' ); ?> </div> <?php } ?>Thanks in advance.
The page I need help with: [log in to see the link]
-
I have come up with a hack.
I added an id attribute to the container tags of my sidebars, ul and div (see original post), of sidebar-primary and sidebar-search. I then wrote the following in JavaScript:
const sidebars = document.querySelectorAll("[id^='sidebar-'"); if (sidebars) { for (sidebar of sidebars) { var inner = sidebar.innerHTML.toString().trim(); sidebar.innerHTML = inner.substr(0, inner.length - 1); } }I have this hack running on my local server. I have left the online version alone so that people can see the issue.
I’d still like an official fix, if there is one, of course; please and thank you. Again, I think I’ve done something wrong in the setup of the template files, which is what is generating the “1” character after the HTML of the sidebar is rendered, I just don’t know what it is.
-
This reply was modified 4 years, 2 months ago by
wileycoyote78. Reason: removed an unnecessary line from the sample code
Don’t use
<?=(before the dynamic_sidebar() calls), use the normal<?php. The former returns a truthy value, which manifests itself as a 1 in output.I should have known it was something simple.
That’s the second time you’ve pulled me out of a jam, bcworkz!
Thanks!
Happy to help.
For the record, “returns a truthy value” is a little inaccurate. The
<?=echoes the truthy value returned by dynamic_sidebar(). Another curiosity is<?isn’t allowed on my PHP version, yet it processes<?= -
This reply was modified 4 years, 2 months ago by
The topic ‘Sidebars generating a 1 after HTML’ is closed to new replies.