-
AuthorPosts
-
July 6, 2016 at 10:40 am #657365
Hi,
I’m trying to output a loop as a shortcode, but the result is being placed out of order in the page flow. My shortcode is in functions.php like this:
function test_posts() { // WP_Query arguments $args = array ( 'category_name' => 'featured', ); // The Query $featured_post_query = new WP_Query( $args ); // The Loop if ( $featured_post_query->have_posts() ) { while ( $featured_post_query->have_posts() ) { $featured_post_query->the_post(); the_title(); } } else { // no posts found } // Restore original Post Data wp_reset_postdata(); } function test_shortcode() { $output = test_posts(); return $output; } add_shortcode( 'test', 'test_shortcode' );
Do I need to format the code differently?
Ash
July 6, 2016 at 2:22 pm #657459Hey Ash!
Have you tried switching to a default theme and reproduced the same issue there?
Can you please post the link to your page where we can see the issue and temporary admin logins?
P.S.: I closed this one – https://kriesi.at/support/topic/shortcode-content-being-placed-out-of-dom-order-2/. Please do not create duplicated topics as it is making it harder for us and for other users to follow.Best regards,
YigitJuly 6, 2016 at 2:28 pm #657469Thanks.
I still get the same issue with the parent Enfold theme. I just need to know how to wrap a loop in the $output operators as you showed me in the original answer. This code (that you gave me) works for outputting html:
function test_shortcode() { $output = "<h2>My Shortcode</h2>"; return $output; } add_shortcode( 'shortcode', 'test_shortcode' );
But fails when I try to create a loop as shown in my opening post.
I’m working locally so can’t provide access at the moment. Also, I tried to re-open the original topic , but it wouldn’t allow me to.
Regards,
Ash
July 6, 2016 at 2:56 pm #657495Hey!
What i meant to say was, have you tried it on default themes like twenty fifteen and so on?
You can also changefunction test_shortcode() { $output = test_posts(); return $output; } add_shortcode( 'test', 'test_shortcode' );
to
add_shortcode( 'test', 'test_posts' );
It seems like issue is related to your code and it would be more suitable if you asked it on WordPress forums as fixing custom codes is out of the scope of our support
Regards,
YigitJuly 6, 2016 at 3:08 pm #657505No that doesn’t work. I can’t test with any other theme as I need this to work with the Advanced Layout Editor.
Perhaps I’m not being clear. To reiterate:
1. I create a shortcode in functions.php. For simplicity I’ve called it ‘test’.
2. In the Advanced Layout Editor I insert the shortcode where I want it to appear in the page.
3. When the page renders, the shortcode output is placed before all other DOM elements, not in the correct place as directed by the Advanced Layout Editor.
4. The shortcode renders correctly when returning html, but incorrectly when returning php (ie, a loop).If the shortcode is hard-coded into the page template, it renders correctly.
July 9, 2016 at 1:34 am #658632Hi,
Try using this code as base:
function custom_shortcode_func() { ob_start(); ?> PUT YOUR CODE HERE <?php $output = ob_get_clean(); return $output; } add_shortcode('my_shortcode', 'custom_shortcode_func');
Best regards,
JosueJuly 11, 2016 at 11:56 am #659180Josue,
Many thanks. I actually worked this out just before your answer came through, but thanks for helping out anyway. That solution works well :)
Ace work! :D
July 12, 2016 at 3:14 am #659628Glad you managed to sort it :)
Regards,
Josue -
AuthorPosts
- The topic ‘Outputting Loop as Shortcode’ is closed to new replies.