0

I have the following Post - Excerpt:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sagittis augue sed orci efficitur convallis at sit amet lectus. [my-short-code]

I have the following in my functions.php file:

function getMyShortCode_func() {
    echo '<div class="abc">Hello world.</div>';
} // end getMyShortCode_func()
add_filter('the_excerpt', 'shortcode_unautop'); // prevents shortcodes from being auto wrapped in <p> tags
add_filter('the_excerpt', 'do_shortcode'); // enables shortcodes to be used in excerpts
add_filter('get_the_excerpt', 'shortcode_unautop'); // prevents shortcodes from being auto wrapped in <p> tags
add_filter('get_the_excerpt', 'do_shortcode'); // enables shortcodes to be used in excerpts
add_shortcode('my-short-code', 'getMyShortCode_func');

The problem is my shortcode content appears at the beginning of the excerpt when I need to to appear in the flow of the document:

<div id="myExcerpt">
    <span class="postTitleFormat"><a href="http://example.com/this-permalink/">Post Title</a></span>
    <div class="abc">Hello world.</div>
    <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sagittis augue sed orci efficitur convallis at sit amet lectus.</div>
</div>

Definition of Terms:

"Document Flow" - the document flow, or flow of document, is the order in which the elements appear in the web document. Example, whenever there are no css positioning styles in place that alter the normal flow of the elements in a document, then the flow order of the elements mimics the order in which they appear in the web document. In the case of this question, there are no css style definitions that alter the flow of the document elements.

  • Please file an edit and explain what "flow of the document" actually is. – kaiser Mar 4 '15 at 17:33
  • Thanks for pointing that our @kaiser. Please see my amended "Definition of Terms". Thanks for helping me on this. I look forward to your answer. – tammy Mar 4 '15 at 17:40
1

Shortcode's callback should return a string, not echo it.

function getMyShortCode_func() {
    return '<div class="abc">Hello world.</div>';
}
  • Ok, thanks @cybmeta. I'll update my post and test it out. – tammy Mar 4 '15 at 18:04
  • Excellent. That was the answer. Much appreciated @cybmeta – tammy Mar 4 '15 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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