Site logo

Archived topic

Sidebar dissapears using shortcode

9 replies · Started by Alberto on April 17, 2021

Viewing posts 1–10 of 10

Hello
I choose the right sidebar to be dsiplayed in a page, but it disappears if I enter a shortcode in the page.
In that page there's only a shortcode, written by me, where I do a query and use ob_start(), returning ob_get_clean().

Any thoughts?

Thanks in advance

Hello
No, I'm not asking about creating a shortcode, I've just created one but in the page I've used it, the sidebar is not been display even though I check the right sidebar option.
So, what I'm asking for is how to display the sidebar using a shortcode.

Thanks

Hi Alberto,

Any chance you can link us to the site in question?

You can use the private information field.

Let me know :)

Hi there,

does the shortcode work if you add it to a page ?

And can you share the shortcode PHP you're using ?

Hi, David
Thanks for replying!
Yes, the shortcode is working, as tou can see at circuloesceptico.org/novo/noticias But the right sidebar is not been displayed.

The code is:

$consulta = new WP_Query( array(
            'post_type'     => 'post',
            'posts_per_page'    => $items,
            'tax_query' => array(
                        array (
                            'taxonomy' => 'category',
                            'field' => 'slug',
                            'terms' => 'noticias',
                        )
                    ),
        ));

    ob_start();
    ?>

    <?php if( $consulta->have_posts() ): ?>
        <div class="listado-artig row">  
    
        <?php while ( $consulta->have_posts() ) : $consulta->the_post();
            $id = get_the_ID();
            $imx = get_the_post_thumbnail_url($id,'large');
            $tit_aux = get_the_title();
            $tit = wp_trim_words( strip_tags($tit_aux), 11);
            if( has_excerpt($id) ){
                $extracto = get_the_excerpt( $id );
            } else {
                $extracto = wp_trim_words( strip_tags(get_the_content()), 20 );
            } ?>
            
            <div class="listado-artig-item">
                <div class="listado-artig-item__imx"><img src="<?php echo $imx; ?>" /></div>
                <div class="listado-artig-item-txt">
                    <a href="<?php the_permalink(); ?>"><div class="listado-artig-item-txt__tit"><?php echo $tit; ?></div></a>
                    <div class="listado-artig-item-txt__txt"><?php echo $extracto; ?></div>
                    <a class="button btn-cies" href="<?php the_permalink(); ?>"><?php _e('Leer más', 'circuloesceptico'); ?></a>
                </div>
            </div>
            
        <?php endwhile; ?>
        </div>
    <?php endif; 

    //get_sidebar();

    ?>

    <?php
    return ob_get_clean();

Hope it helps

Hi there,

Is this the full code for your shortcode? There seem to be parts missing.

Normally, shortcode snippets go like this.

add_shortcode('shortcode_name',function(){
ob_start();
//your code here
return ob_get_clean();
});

Note/s: When you do loops, it's good practice to add wp_reset_postdata(); on the end.
https://developer.wordpress.org/reference/functions/wp_reset_postdata/

Hello, Elvin
Thanks for your reply!
Seems adding wp_reset_postdata(); do the trick.

Regards

No problem. Glad to be of any help. :)

This archived topic is closed to new replies.