0

I have this code to display 4 posts of type "testimonial" on the Home page:

<?php
            $custom_posts = get_posts([
                'numberposts' => 4,
                'post_type' => 'testimonial'
            ]);
            ?>

            <?php foreach ($custom_posts as $post) : setup_postdata($post); ?>

                <article>
                    <?php the_excerpt(); ?>

                    <a href="<?php the_permalink(); ?>">Continue reading</a>

                    <p><?php echo get_post_meta($post->ID, 'pacient', TRUE); ?></p>
                </article>

            <?php endforeach; ?>

This works as expected. Excerpts are shown. Sunshine rainbows. Unfortunately if I move exactly the same code to the sidebar on other side no excerpts are shown. There is content trimmed down for each post instead.

In other words: This exact same code (copy/pasted) on home page produces excerpts. In the sidebar on other page it produces trimmed content with "[...]" at the end. I have no clue why. Can the problem be related to where the get_post function is called (after Query or something).

Why this function behaves differently in both cases? How do I force it to show excerpt only and not the post content. Am I missing something?

6
  • I don't understand your question. The excerpt can't change its behaviour to suite itself Commented Aug 11, 2014 at 8:26
  • This exact same code (copy/pasted) on home page produces excerpts. In the sidebar on other page it produces trimmed content with "[...]" at the end. I have no clue why. Can the problem be related to where the get_post function is called (after Query or something).
    – Macejkou
    Commented Aug 11, 2014 at 8:34
  • You should add that info directly to your question in the form of an edit. Thank you Commented Aug 11, 2014 at 8:50
  • are you using hand-written excerpts? does your theme possibly use any filters for the excerpt?
    – Michael
    Commented Aug 11, 2014 at 10:50
  • I guess there is a filter added in functions.php which is showing full excerpt in homepage and trimmed excerpt in the sidebar. Did you check functions.php or any other file included in functions.php? Commented Jun 21, 2017 at 6:52

1 Answer 1

1

Your problem has nothing to do with the_excerpt(). the_excerpt() template tag does not change its behavior by itself or change its behavior according to location. The only time the_excerpt() changes its behavior is when you manually filter the output through a custom function.

I most probably think that all of your misery is caused by neglet :-). Neglet to what you may ask, and the simple answer is, negletting clean up after yourself when you done, ie resetting your postdata once you've done with your custom query

Many people neglet this unmost important function wp_reset_postdata. This is a gloden rule, always always, whenever you create a custom query with either get_posts or WP_Query, you have to reset your query, otherwise every other query afterwards will be affected negatively.

So, I think by resetting your postdata after your custom query (add wp_reset_postdata just after endforeach), everything will be fine

Just a point of note, don't use : and endforeach. It makes code difficult to read and to debug, and code editors can't read this as well. To make things easier in the future, rather use curly brackets ({})

1
  • Many thanks for your advices. I was tinkering with it for a while. I have added wp_reset_postdata after all custom queries but nothing changed. I tried to remove all queries except the one and still the same code produces excerpts on static home page and trimmed content with "[...]" on page/post. I really dont understand the behaviour of this :-( I believe Wordpress is altering the custom query in some way if you are inside the post or page. Is it possible?
    – Macejkou
    Commented Aug 12, 2014 at 12:10

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.