-
AuthorPosts
-
February 4, 2021 at 10:39 am #1277647
Hi,
I want to add a widget on every blog-post site after the main content, but before the sidebar.
When I add a widget to “display everywhere” it is added after the sidebar, so this does not help.
I could also add a custom widget area to each page, but I would prefer a “global” solution for all posts.I already found this:
add_action( 'ava_after_content', 'enfold_customization_category_widget_area' ); function enfold_customization_category_widget_area() { if (is_singular(post)) { dynamic_sidebar( 'my-sidebar-name' ); } }
… but the hook “ava_after_content” does not seem to work.
Other hooks like ava_after_main_container or ava_before_footer work fine, but that’s not the position I need.Any suggestions?
February 4, 2021 at 12:26 pm #1277667try a different if-clause !
add_action( 'ava_after_content', 'enfold_customization_category_widget_area' ); function enfold_customization_category_widget_area() { if (is_singular('post')) { dynamic_sidebar( 'my-sidebar-name' ); } }
or use:
if(is_single()){
if you like to have that on portfolio too use:
if(is_singular('post') || is_singular('portfolio')){
February 4, 2021 at 12:37 pm #1277669Hi.
it’s not really a matter of the if-clause. It’s obviously a matter of “ava_after_content”.
As I said, it works with different hooks like ava_after_main_container or ava_before_footer, but ava_after_content has no effect at all…Or maybe I misunderstand the name of this hook…
I just realized, my widget is added at blog archive pages to ALL posts…
That’s also not what I need…So the basic question is: How can I add a widget after my blog content, before the sidebar.
PS: Unfortunately our code has no effect at all…
February 4, 2021 at 12:42 pm #1277670The hook determines only the place where it is inserted.
Did you create that widget-area at Widgets : “my-sidebar-name” ?Are these single posts created with classic editor or with advanced Layout Builder ?
Try one post with classic editor!
February 4, 2021 at 1:02 pm #1277676Yes, I know.
I need a hook for the position after the content, before the sidebar.
As I said, it already works with other hooks like ava_after_main_container or ava_before_footer, so yes, my widget “my-sidebar-name” exists.For me, the hook ava_after_content sounded like what I was looking for, but maybe I was wrong.
I’m posting with the ALB, which is preferred (we even lost the share-box and the comments because of that, but the ALB is still preferred).
The ava_after_content indeed works with the classic editor, but since the content is also inserted after every single post on archive pages, I assume it’s not the correct hook anyway…Maybe I have no other choice than using the insert-widget on every single post page.
But maybe someone from kriesi has a better solution…?But thank you for your input, Günni :)
February 4, 2021 at 1:26 pm #1277691maybe a mod knows a better way – you can do it with wordpress standard functions this way:
( you don’t need to insert that dynamic side bar manually – it is done by that code allready)just a moment – the given code is not totaly correct – it works but it is not at the end of content in all cases – sorry
- This reply was modified 3 years, 9 months ago by Guenni007.
February 4, 2021 at 1:41 pm #1277701Hi,
thanks a lot.
Where do I have to enter this code?When I enter it at the /themes/enfold/functions.php it has no effect…
When I enter it at wp-includes/widgets.php, my widget appears in the middle of my content -between an image and some text of the same textarea, which is kinda strange… it’s also added to my source code at this strange position…
February 4, 2021 at 2:13 pm #1277707thats the reason why i erased it. ( All code goes to child-theme functions.php )
i thought that we can use another filter of enfold:
but thisadd_filter('avf_template_builder_content', 'avf_template_builder_content_comment_mod' , 10 , 1); function avf_template_builder_content_comment_mod($content = "") { if(is_singular('post') || is_singular('portfolio')){ $sidebar_content = dynamic_sidebar( 'my-sidebar-name' ); $content = $content . $sidebar_content ; } return $content; }
inserts the widget area before ( allthough it is
$content = $content . $sidebar_content
) content. ?
Sorry – you had to wait for a modFebruary 4, 2021 at 2:19 pm #1277710OK here is a code that should work:
add_filter('avf_template_builder_content', 'avf_template_builder_content_comment_mod' , 10 , 1); function avf_template_builder_content_comment_mod($content = "") { if(is_singular('post') || is_singular('portfolio')){ $sidebar_content = do_shortcode("[av_sidebar widget_area='my-sidebar-name']"); $content = $content . $sidebar_content ; } return $content; }
remove the or portfolio clause if you do not want to include them!
But the existing shortcode is only for those post/portfolios when they are made by Advanced Layout BuilderFebruary 4, 2021 at 2:23 pm #1277711YES! Thanks a lot!
This indeed works great! :)Thank you VERY much :)
February 4, 2021 at 2:58 pm #1277723This should work on both post with classic editor and posts with alb editor ( don’t know if it will work for Gutenberg):
add_filter('the_content', function($content) { if (is_singular('post')) { $content .= '[av_sidebar widget_area="my-sidebar-name"]'; } return $content; } );
And if you got more than one widget in that widget area – you can play with flex layout to have those widgets besides each other :
#main .avia-builder-widget-area { display: flex; flex-flow: row wrap; justify-content: space-between; } #main .avia-builder-widget-area::before, #main .avia-builder-widget-area::after { display: none; } #main .avia-builder-widget-area .widget { padding: 0; margin-bottom: 30px; }
February 4, 2021 at 3:17 pm #1277730That’s really great, thanks a lot for your help!
BONUS-QUESTION:
I’m using a page as footer. As this page also contains a content area… is there any way to hide my widget from the footer page?
At the moment it is added after the content -.as I wanted…
… but also after the content of the footer page…February 4, 2021 at 4:09 pm #1277740hm – that is strange because if the footer-page is a page – then this content shouldn’t be affected.
you can do this to child-theme functions.php below the above code :
function remove_widget_area_from_footer_page(){ if (is_singular('post')) { ?> <script> (function($){ $(window).on('load', function () { $('#footer-page .avia-builder-widget-area').remove(); }); })(jQuery); </script> <?php }} add_action('wp_footer', 'remove_widget_area_from_footer_page');
February 4, 2021 at 5:28 pm #1277754I’m already at home, so I’ll try it tomorrow.
I also have another idea using the page_id to exclude the footer page, which also should work.
I’ll post about it tomorrow.
Again, thank you very much for now :)February 5, 2021 at 7:58 am #1277884So, back at work…
I added the page_id of the footer-page to the if-clause, so it does not appear in the footer anymore.
This works fine for me.if((is_singular('post') || is_singular('portfolio')) && get_the_ID() != '106' && get_the_ID() != '107')
106 = page_id of German footer, 107 = page_id of English footer
yes, it’s very static, but the widget-name is also static, so it doesn’t really matter.PS: Ja, wir hätten wohl die ganze Zeit Deutsch schreiben können, aber ich hatte auf Englisch geschrieben, weil viele Mods hier Englisch sprechen. Aber nochmal vielen lieben Dank an Günni, Du hast mir mit Deinen Antworten wirklich SEHR geholfen :)
February 5, 2021 at 8:40 am #1277905Hi GrandmasterA,
Thanks for the update. So everything is working as it should now then? Thanks @guenni007 for helping out :-)
Best regards,
RikardFebruary 5, 2021 at 11:32 am #1277943maybe Günter could insert an extra class to that page ( similar to privacy-policy page ) which is set to be the footer-page
where then in fact the question would be how to rewrite the code so that it works with a class to body.
i tested of course !is_page(106) but even with the ID it does not work. so !is_page(‘kontact-section’) won’t work too.Thank you GrandmasterA for your addition to the if-clause.
Preventing something from emerging is definitely better here than just hiding it later.February 5, 2021 at 11:34 am #1277944Hi Rikard, yes, everything works as it should now.
Thanks again, Günter :)February 5, 2021 at 7:04 pm #1278129Hi GrandmasterA,
Great :)
We are closing the thread.
If you need further assistance please let us know in a new one.
Best regards,
Victoria -
AuthorPosts
- The topic ‘Add custom widget after main content, but before sidebar’ is closed to new replies.