0

I'm trying to create a custom theme, since I need to convert html pages to be used at WordPress.

In about.php page, I have this shortcode to retrieve posts of a category:

<?php echo do_shortcode('[categoryposts catname="about-section-1"]'); ?>

functions.php:

 function wpb_postsbycategory($atts='') {

    // Get parameters\attributes from shortcode
    $attributes = shortcode_atts(
        array(
            'catname' => '',//this is default value
        ),
        $atts,
        'featured'
    );

   // the query
   $the_query = new WP_Query( array( 
       'category_name' => $attributes['catname'], 
       'posts_per_page' => 5
   ) );
      
   // The Loop
   $string = "";
   if ( $the_query->have_posts() ) {
       $string .= '<ul class="postsbycategory widget_recent_entries">';
       while ( $the_query->have_posts() ) {
           $the_query->the_post();
               if ( has_post_thumbnail() ) {
                    $string .= '<li>';
                    $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
               } else { 
                    // if no featured image is found
                    $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
               }
            }
    } else {
        // no posts found
        $string .= '<li>No Posts Found</li>';
   }
   $string .= '</ul>';
      
   return $string;
      
   /* Restore original Post Data */
   wp_reset_postdata();
 }
 // Add a shortcode
 add_shortcode('categoryposts', 'wpb_postsbycategory');

Though about.php is a PAGE (since I linked it using Template Name), if I use the_title() & the_content():

  • before the shortcode line, they properly get current Page title & content.
  • but after the shortcode line, they wrongly get some Post title & content.

Question: How can I retrieve Page title & content (instead of Post), after having used shortcode? Do I need some reset functionality?

0

1 Answer 1

4

Do I need some reset functionality?

Yes, you do — when you call $the_query->the_post(), the global $post variable will be modified so that the the_ functions like the_title(), the_content() and the_permalink() will use the current post in your custom loop, and thus after the loop ends, you should call wp_reset_postdata() in order to restore the $post global to the current post in the main query (that's referenced via the global $wp_query variable), because otherwise then the issue in question would happen.

And your code/function is actually calling wp_reset_postdata(), except that it never got called because it's called after the return line, i.e. you did return $string; wp_reset_postdata();..

So you would just need to swap their position, like so:

wp_reset_postdata(); // call this first

return $string;      // then return the output

But I would probably call the function right after the while block ends, i.e. while ( ... ) { ... } wp_reset_postdata();, because there's nothing to reset if the $the_query->the_post() didn't run.

Additionally, the $post_id as in get_the_post_thumbnail($post_id, array( 50, 50) ) is never defined in your code, so you should change that to get_the_ID(), or use null instead to use the current post in your loop.

1
  • 1
    Thanks for taking the time to explain. You're a great girl\guy. Virtual hugs.
    – compliance
    Commented Apr 12, 2022 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.