Shortcode results appearing in wrong place

I have a news page on a site I’ve built, and I made two shortcodes to insert the news items. This had worked when I first built it, but I just noticed that one shortcode (the columns of headlines on the bottom) appears in the correct place, but the other (latest news item) now appears outside of where the content area element is. I tried changing that element to a raw content element but got the same results. The page is here:

https://lifelearningacademysf.org/news

I didn’t change anything on my end that I know of, so I don’t know what the first news item is appearing outside of where it should be.

Here’s the shortcode code:

function latestNews() {

$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'news',
'posts_per_page' => 1,
);

$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :

    while ( $arr_posts->have_posts() ) :
    $arr_posts->the_post();

?>

 <div class="firstnewsitem">
     
     <a href="<?php the_field('newsurl'); ?>" target="_blank">
     <img src="<?php the_field('mainimage'); ?>" style="width: 100%;">
     
     	   <h3><?php the_time( 'F j, Y' ); ?></h3>
       
     <h2><?php the_title(); ?></h2></a>
     
 	 <?php if( get_field('subhead') ): ?>
 	 	<p><?php the_field('subhead'); ?></p>
 	 <?php endif; ?>

 </div>

    <?php

endwhile;
endif;

wp_reset_query();

}

add_shortcode('latestnews', 'latestNews');

I’ll upload login info in the secure note. Thanks for your help.

Odd – I changed the type of element from Content Area to Text and inserted the shortcode there, and now the page displays properly.

Why would a shortcode work correctly in a text element and not a Content Area or Raw Content element?

Hello @cheshiredave,

Thanks for writing in!

With the Text elements, all of the output will be enclosed with a Text wrapper. Using the Content Area or Raw Content, it will output the exact custom HTML codes that you will insert on this element. The fact that the shortcode do not work in the Content Area or Raw Content could mean that there is something wrong in your shortcode output. If you have got this code from anyone or anywhere, please contact the creators of this code and have it updated.

Best Regards.

Well, the shortcode was working in the Content Area element, and I haven’t changed the code on the website, but something happened along the way and suddenly it was showing results outside the element. I gave you access so you could see the shortcode – it’s nothing unusual. So I don’t know if it was a change to Pro itself that caused it to no longer work. But I would love to have an answer to this question, since it was working and then it wasn’t, and I hadn’t done any structural changes to the site during that period.

Thanks,
Cheshire

Hi Cheshire,

I have checked the page URL but am unable to find the error you described here. I have also found that the site is not using the X or Pro, and the current user does not have the privileges to activate any of that to check the issue further. I would suggest you share the credentials which have the Admin privilege or you can please copy your live site to a staging server so we could troubleshoot freely without breaking your live site.
And give us access in the secure note including:

– WordPress Site URL & Login URL
– WordPress Admin username/password

To create a secure note, click the key icon underneath any of your posts.

Thanks

I don’t understand why you think the site isn’t using Pro. It absolutely is – I’m using the Pro Child theme. I gave you credentials already but the user was set to editor. I have since changed it to admin. The credentials are in the secure note above (obviously I know how to create a secure note, because I already did it in this thread). The site URL is https://lifelearningacademy.org.

Also, the problem is not showing up because I managed to fix it on my own by changing the element the shortcode is in to a text element – it had PREVIOUSLY been in a Content Area element. But I want to know why it USED to work in a Content Area element and then stopped working there, seemingly by itself because I did nothing to the site structure. All I’ve done is update Pro.

Your message is extremely frustrating – I don’t understand how you could possibly have had this reaction. I’m appreciative of the support staff, but this response was really unhelpful.

Thanks,
Cheshire

Hi Cheshire,

I am sorry that my comment does not satisfy you but, while I checked your site earlier the TwentyTwenty One theme was activated and the user privilege was not admin and that is what I have mentioned in my post. It seems that you have already managed to solve the problem by yourself, please let us know if you need any further help on this.

Thanks

That was simply not possible. The site has been the Pro Child theme from day one and has never changed.

Furthermore, you still have not answered the primary question. Why would a shortcode work for months in a Content Area element and then stop working?

I now see that Content Area has been deprecated, but that was never flagged for me as a problem. So some Pro update made that element stop working correctly?

OK, so you can see the problem in action, I have duplicated the news page and put the same shortcode into two elements: a text element and a raw content element. You can see what’s going wrong here:

https://lifelearningacademysf.org/news2

The text element is outputting the shortcode correctly. The raw content element is throwing the shortcode results into the wrong place. The page SHOULD look like this:

The info in the secure note above is still active in case you are willing to take a look and see what’s gone wrong. I have troubleshooted by turning off all plugins with no better luck. I have also checked this on Chrome as well as Safari on Mac and see the exact same results.

Thanks.

Hi @cheshiredave,

The issue has something to do with the code. To fix your issue, you need to use a PHP buffer to your code. Create a buffer in your shortcode callback before running your code, clear the buffer and return the content right after. Here’s a quick example:

function latestNews() {
	
// start buffer
ob_start();
	
	$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'news',
    'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );
 
if ( $arr_posts->have_posts() ) :
 
   	while ( $arr_posts->have_posts() ) :
        $arr_posts->the_post();
	
?>

     <div class="firstnewsitem">
	     
	     <a href="<?php the_field('newsurl'); ?>" target="_blank">
	     <img src="<?php the_field('mainimage'); ?>" style="width: 100%;">
	     
	     	   <h3><?php the_time( 'F j, Y' ); ?></h3>
           
	     <h2><?php the_title(); ?></h2></a>
	     
	 	 <?php if( get_field('subhead') ): ?>
	 	 	<p><?php the_field('subhead'); ?></p>
	 	 <?php endif; ?>

     </div>

        <?php

    endwhile;
	
	wp_reset_query();
	
endif;	
	
// return buffered output
return ob_get_clean();
}

add_shortcode('latestnews', 'latestNews');

Please note that custom development is outside the scope of our support. Issues that might arise from the use of custom code and further enhancements should be directed to a third party developer you can avail One where we can answer questions outside of the features of our theme.

Hope that helps.

Thank you, and I’ll explore that, but the central questions remain:

It used to work in a Content Area/Raw Content element, and I didn’t change anything, and the only change is that Pro got updated. So what was it about Pro updating that made something that previously worked stop working?

And why does the shortcode work correctly in a Text element but not a Raw Content element?

These are THEMECO questions, not custom development questions. PLEASE answer them. It’s so frustrating to have my central questions keep getting ignored.

Hey @cheshiredave,

Sorry for the frustration! I believe the reason why your shortcode is working in the text element is that the text element is already buffered so it works out of the box. I am referring to the PHP buffer I shared above.

Why your shortcode is not working with the Raw Content element?

  • I suspect that the dev team made the Raw Content element as simple as possible ( removing parts like buffer )

Hope that helps.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.