• Resolved utnalove

    (@utnalove)


    Hello,

    I created a theme. It has custom template for pages. For example:

    <?php /* Template Name: Page Home  */?>

    is my homepage.

    All the custom templates are working fine. The content I put in the template php files shows well.

    But in the homepage I need to do something similar I did in the FAQ page.

    <?php echo do_shortcode(the_content()); ?>

    The above in the FAQ collects the faq shortcode:

    function faq_shortcode($atts, $content = null) {
    
    return do_shortcode($content);
    
    }
    
    add_shortcode('faq', 'faq_shortcode');

    And all is fine there.

    But when I put <?php echo do_shortcode(the_content()); ?> in the homepage template file, instead of showing the content of the shortcode I put in the edit mode of that specific page that is using the “Page Home” template, it shows blog posts instead.

    If I temporarily change the page template to “default template” then all the custom edits go away (obviously), but the short code info show up!

    How can I make the <?php echo do_shortcode(the_content()); ?> to read the content I put in the page edit mode while using a custom template?

    FYI:

    • For blog posts I have another template which is working fine.
    • In settings => appearance I have set the “Page Home” template for home as a static page. And for blog posts I have chosen the blog posts specific template.

    Please help

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    The results of do_shortcode() depends on what shortcode you’ve placed within your home page content. Without knowing how that shortcode works, I cannot speculate on why you are getting blog posts in return.

    In the case of your [faq] shortcode, it invokes another shortcode that appears between [faq] and [/faq] tags so I still don’t know what shortcode is responsible for what appears on the page. It’s unclear why you’d need to nest shortcodes like that, but it obviously can work. It just seems unnecessarily convoluted to me.

    Thread Starter utnalove

    (@utnalove)

    Thank you. Seems legit. Here is more details:

    functions.php
    
    /* FAQ pages custom code */
    function faq_shortcode($atts, $content = null) {
        return do_shortcode($content);
    }
    add_shortcode('faq', 'faq_shortcode');
    
    function faq_question_shortcode($atts, $content = null) {
        static $count = 0;
        $count++;
    
        $output = '<div class="question-block" data-container="toggle-' . $count . '" itemprop="mainEntity" itemscope itemtype="https://schema.org/Question">';
        $output .= '<h3 itemprop="name" class="question">' . $atts['question'] . '</h3>';
        $output .= '<div id="toggle-' . $count . '" itemprop="acceptedAnswer" itemscope itemtype="https://schema.org/Answer" class="answer">';
        $output .= '<div itemprop="text" class="text">';
        $output .= do_shortcode($content);
        $output .= '</div></div></div>';
    
        return $output;
    }
    add_shortcode('question', 'faq_question_shortcode');
    
    faq template:
    
    <?php /* Template Name: FAQ */?>
    
    <?php get_header(); ?>
    
    <section class="faq-page" itemscope itemtype="https://schema.org/FAQPage">
      <div class="wrapper">
        <h1 class="headline">FAQ</h1>
        <div class="q-a-container">
    
        <?php echo do_shortcode(get_the_content()); ?>
    
        </div>
        <div class="snippet">Masz dodatkowe pytania? Napisz na maila:
          <script
            language="JavaScript">user = 'info'; site = 'example.com'; document.write('<a href=\"mailto:' + user + '@' + site + '\" class="text">'); document.write(user + '@' + site + '</a>');</script>
        </div>
      </div>
      <div class="wrapper">
        <a href="<?php echo home_url(); ?>/umow-wizyte/" class="btn">UMóW WIZYT?</a>
      </div>
    </section>
    
    <?php get_footer(); ?>

    In that FAQ template it works fine.

    The shortcode is:

    [faq]
    [question question="question xxx"]
    answer xxx [/question]
    
    [question question="question yyy"]
    answer yyy [/question]
    [/faq]

    Is this information enough in order to understand why the <?php echo do_shortcode(the_content()); ?> inside the faq template works well, but in the homepage template collects the blog posts?

    Moderator bcworkz

    (@bcworkz)

    I don’t see anything there that would cause a blog post listing to appear on the page. As you say, for FAQ it all works fine.

    You don’t want to use the_content() as an arg for do_shortcode() because it does not return a value, it directly outputs page content. You should use get_the_content() instead. But using the_content() inappropriately would not cause a blog listing to be output.

    Why do you even need to use do_shortcode() since both the_content() and get_the_content() should have already processed shortcodes? I’m guessing it’s because you’ve nested shortcodes so you need another pass through to complete the process? Again, unrelated to there being a blog post listing.

    Are you sure the static page setting is currently active and it didn’t somehow revert back to latest posts? Are you sure your page home template doesn’t somewhere make it’s own query for a blog listing? If those are confirmed, then I suspect some other theme or plugin code is affecting the page’s query so that the result is a blog listing instead of page home.

    As a test, deactivate all plugins. Copy your page home template file to the 2021 theme and activate that theme. If my suspicion is correct, you’ll only see your page’s content and not a blog listing on your home page.

    Thread Starter utnalove

    (@utnalove)

    I did try both the_content()?and?do_shortcode()

    Yes. Static page setting is currently active for the homepage. Config is correct.

    Are you sure your page home template doesn’t somewhere make it’s own query for a blog listing? If?

    Well… here maybe the reason. In the homepage there is a little section that showcases the last 3 blogs. Here is the site.

    The code is…

    <?php
    
    $args = array(
      'post_type' => 'post',
      'posts_per_page' => 3,
      'order' => 'DESC',
    );
    
    $recent_posts = get_posts($args);
    
    if ($recent_posts):
    
    ?>
    [...]
    
    <div class="card-wrapper">
          <?php foreach ($recent_posts as $post):
        setup_postdata($post);
        $post_link = get_the_permalink($post);
        $post_title = get_the_title($post);
        $post_categories = get_the_category($post);
          ?>
    
          <div class="card">
            <a href="<?php echo $post_link; ?>">
              <?php echo get_the_post_thumbnail(
          $post->ID,
          'medium',
          array(
            'srcset' => wp_get_attachment_image_url(get_post_thumbnail_id($post->ID), 'thumbnail') . ' 480w, ' .
            wp_get_attachment_image_url(get_post_thumbnail_id($post->ID), 'medium') . ' 640w, ' .
            wp_get_attachment_image_url(get_post_thumbnail_id($post->ID), 'large') . ' 960w'
          )
        ); ?>.....

    Is there a solution in this case to get the content where I want (as I described in my original question)?

    Moderator bcworkz

    (@bcworkz)

    Sorry for the slow reply, I’ve been ill.

    That query ends up being the posts in the BAZA WIEDZY section, right? That appears to be working correctly. I cannot explain how doing your shortcode would result in a standard blog archive. There must be something odd about the shortcode that I’m not seeing.

    I’m not sure you understood my suggestion about trying get_the_content(). Did you try inserting this in your home page template:
    <?php echo do_shortcode(get_the_content()); ?>
    (added get_ to what you had)

    Where on the template did you add the code? It should be within the template’s main loop because the global $post var needs to be initialized before get_the_content() will work correctly.

    If that doesn’t help, what is the home page’s main content in the editor? Specifically what shortcodes are in it?

    I know you’re not getting the results you want. What are the results you wanted anyway? Are you still trying to get FAQs? Some other question/answer structure?

    Thread Starter utnalove

    (@utnalove)

    Thank you. I found the solution.

    The custom query I was using to display the latest blog posts was the cause of the issue. When I was using setup_postdata($post) within my custom loop, it was changing the global $post object context in WordPress. As a result, when get_the_content() was being called later, it was referring to the last post in my custom loop, not the page content.

    To solve this issue, I had to reset the WordPress post data after my custom loop. I did it using wp_reset_postdata(). This function restores the global $post object to the current post in the main query which, in my case, was the homepage content.

    Moderator bcworkz

    (@bcworkz)

    I’m glad you found the problem! This wouldn’t be the first time I’ve forgotten about resetting post data. I want to think it would have eventually occurred to me. You saved me the effort ??

    I’ll go ahead and mark this topic as resolved.

    Thread Starter utnalove

    (@utnalove)

    Glad it helped!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘get_the_content() inside a custom template is getting blog posts’ is closed to new replies.