Text Widget : Content appearing Before widget tittle
-
Hi,
To include the Facebook/Twitter share buttons in text widgets, I created a shortcode.
When I put that short code in the text widget, the title (“partager”) appears before the buttons … strange …url : http://www.voirleloup.com
(in the footer right column)
It is not a css problem as the div containing the buttons is before the div containing the tittle …Any idea ?
thanks
——————————————————-
shortcode code :/*-----------------------------------------------------*/ /* share /*-----------------------------------------------------*/ function guigui_share($atts, $content = null) { $output = get_template_part('template-part-social-share','childtheme'); return $output; } add_shortcode('share', 'guigui_share');
————————————————-
template-part-social-share code :<div class="tt-share clearfix"> <?php global $ttso; $blog_retweet = $ttso->st_blog_retweet; $blog_fb_like = $ttso->st_blog_fb_like; $blog_pinterest = $ttso->st_blog_pinterest; if($blog_retweet == "true") { ?> <span class="retweet-share"> <script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script> <a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php the_permalink(); ?>" data-via="<?php bloginfo('name'); ?>" data-text="<?php the_title(); ?>" data-related="<?php bloginfo('name'); ?>" data-count="horizontal">Tweet </span> <?php } if($blog_fb_like == "true") { ?> <span class="facebook-share"> <iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink(get_The_ID())); ?>&layout=button_count&show_faces=false&&action=like&colorscheme=light"></iframe> </span> <?php } if($blog_pinterest == "true") { ?> <span class="pinterest-share"> <?php $pinterestimage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?> ID)); ?>&media=<?php echo $pinterestimage[0]; ?>&description=<?php the_title(); ?>" class="pin-it-button" count-layout="horizontal">Pin It </span> <?php } ?> </div><!-- END tt-share -->
-
Sorry, I made a mistake at the beginning of my post :
the title (“partager”) appears after the content of the widget (the share buttons)caused by the fact that ‘get_template_part()’ does not return a string, but fires the template directly;
you might need to use output buffering;try for example:
function guigui_share($atts, $content = null) { ob_start(); get_template_part('template-part-social-share','childtheme'); $output = ob_get_contents(); ob_end_clean(); return $output; }
http://php.net/manual/en/function.ob-start.php
http://www.php.net/manual/en/function.ob-get-contents.phpThanks Alchymyth !
I understand my mistake (that’s the first step.)
I replaced my version of the content by yours.
The shortcode appears as [share] … and is not interpreted any more …
Here is the html I get in the text widget where I put the shortcode …<br> <br> [share]
Thanks anyway … i’m closer to the solution
have you kept this line:
add_shortcode('share', 'guigui_share');
?
because it was not actual part of the function, I did not repeat it in my code section.
Great ! The share buttons are there !
Cool, but there is a new issue :
Some <br.> and <p>tags appeared and the layout is messed up.
This problem appears with the ob_get_contents() solution and not with the one I used before you (kindly) helped me …
How do these appear ? … mystery (at least for me.)You still can see the footer here :
http://www.voirleloup.com/inscriptionBTW, I tried to opt in and out the ‘automatically generate paragraphers’ of the text widget
No effect …… one more strange thing :
No matter what text i type before or after the [share] shortcode in the text widget, it won’t be displayed on the page …
The only thing we have is the 2 share buttons …well man, I gave up.
Down with the text widget, down with the shortcode.
I simply modified the footer and it took me 5 minutes …
The result is perfect although the method is not elegant …
Thanks a lot anyway for your help.
NicolasAlchymyth, your solution did work for me. I had a similar problem: I wrote a function that contained a loop, put it in a shortcode, and put the shortcode in a widget. It worked, except the widget title was under the loop output. Adding the ‘output buffer’ (whatever that it) fixed it.
My function is:
function keyshots_widget() { ob_start(); $custom_query = new WP_Query('cat=15&posts_per_page=1'); while($custom_query->have_posts()) : $custom_query->the_post(); ?> <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> <?php endwhile; $output = ob_get_contents(); ob_end_clean(); return $output; wp_reset_postdata(); // reset the query } add_shortcode("keyshots-widget", "keyshots_widget");
in your case, you could have used string concatenation;
example:
function keyshots_widget() { $output = ''; $custom_query = new WP_Query('cat=15&posts_per_page=1'); while($custom_query->have_posts()) : $custom_query->the_post(); ?> $output .= '<p><a href="' . get_permalink($post->ID) . '">' . get_the_title() . '</a></p>'; <?php endwhile; wp_reset_postdata(); // reset the query return $output; } add_shortcode("keyshots-widget", "keyshots_widget");
Hey, I like it! That’s simpler.
- The topic ‘Text Widget : Content appearing Before widget tittle’ is closed to new replies.