title: Wordpress the_category() to php string
body: <p>I'm trying to get the WordPress <code>the_category()</code> function to output as a string so I can use it as part of another variable.</p>
  <p>This is the code I'm using, I'd like <code>$the_category</code> to output the HTML result of <code>the_category()</code>.</p>
  <pre><code>    function more_post_ajax(){
  
      $ppp = (isset($_POST[&quot;ppp&quot;])) ? $_POST[&quot;ppp&quot;] : 3;
      $page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0;
  
      header(&quot;Content-Type: text/html&quot;);
  
      $args = array(
          'suppress_filters' =&gt; true,
          'post_type' =&gt; 'post',
          'posts_per_page' =&gt; $ppp,
          'paged'    =&gt; $page,
      );
  
      $loop = new WP_Query($args);
  
      $out = '';
  
      if ($loop -&gt; have_posts()) :  while ($loop -&gt; have_posts()) : $loop -&gt; the_post();
      
          $the_permalink = get_the_permalink();
          $the_post_thumbnail = get_the_post_thumbnail( medium );
          $the_category = the_category();
          $the_title = get_the_title();
          $the_excerpt = get_the_excerpt();
      
      
          if ( has_post_thumbnail() ) :
              $thumbnail = '&lt;div class=&quot;card-img-wrap&quot;&gt;&lt;a href=&quot;'.$the_permalink.'&quot; title=&quot;'.$the_title.'&quot; class=&quot;stretched-link&quot;&gt;&lt;img src=&quot;'.$the_post_thumbnail.'&quot; class=&quot;card-img-top wp-post-image&quot; alt=&quot;&quot; loading=&quot;lazy&quot; /&gt;&lt;/a&gt;&lt;/div&gt;';
          endif;
      
          $out .= '&lt;div class=&quot;col-md-4 col-sm-2&quot;&gt;
                  &lt;article itemscope=&quot;&quot; itemtype=&quot;http://schema.org/Article&quot;&gt;
                      &lt;div class=&quot;card shadow mb-3&quot;&gt;'.$thumbnail.'
                          &lt;div class=&quot;card-body&quot;&gt;
                              &lt;div class=&quot;postcat text-muted text-center&quot;&gt;'.$the_category.'&lt;/div&gt;
                              &lt;h4 class=&quot;posttitle text-center&quot; itemprop=&quot;headline&quot; title=&quot;'.$the_permalink.'&quot; rel=&quot;bookmark&quot;&gt;&lt;a href=&quot;'.$the_permalink.'&quot;&gt;'.$the_title.'&lt;/a&gt;&lt;/h4&gt;
                              &lt;div class=&quot;postexcerpt text-center mb-3&quot;&gt;'.$the_excerpt.'&lt;/div&gt;
                              &lt;div class=&quot;card-footer&quot;&gt;
                                  &lt;div class=&quot;social-icons&quot;&gt;
                                      &lt;ul class=&quot;unstyled&quot;&gt;
                                          &lt;li class=&quot;social-facebook&quot;&gt;&lt;a href=&quot;//www.facebook.com/sharer.php?u='.$the_permalink.'&quot; title=&quot;Share to Facebook&quot; target=&quot;_blank&quot;&gt;&lt;i class=&quot;fab fa-facebook&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                                          &lt;li class=&quot;social-twitter&quot;&gt;&lt;a href=&quot;//twitter.com/home?status='.$the_permalink.'&quot; title=&quot;Share to Twitter&quot; target=&quot;_blank&quot;&gt;&lt;i class=&quot;fab fa-twitter&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                                          &lt;li class=&quot;social-pinterest&quot;&gt;&lt;a href=&quot;//pinterest.com/pin/create/link/?url='.$the_permalink.'&quot; title=&quot;Share to Pinterest&quot; target=&quot;_blank&quot;&gt;&lt;i class=&quot;fab fa-pinterest-p&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                                      &lt;/ul&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/article&gt;
              &lt;/div&gt;
  ';
  
      endwhile;
      endif;
      wp_reset_postdata();
      die($out);
  }
  </code></pre>
  <p>If possible, I'd like to do something similar for <code>the_post_thumbnail()</code>.</p>