One of the major problems I have with a big site is the number of templates. So I figure I could call an optional footer within the page using a shortcode.

function trainer_footer( ){
add_shortcode( 'trainer', 'trainer_footer' ); 

This works.

But there are two questions;

  1. the content of sidebar-trainer appears at the top of the content rather than in the place of the shortcode. Why?

  2. I've read it's not a good idea to write PHP into a shortcode like this. Why?

  1. First, get_footer() is meant to load page level template files, not post-level ones. You should be using get_template_part() instead.
  2. Shortcodes execute as a filter on the_content. Per the norms of all filters, they should return content, not echo it. echoing content will cause effects like you are seeing. Apparently, the template file you are loading with the shortcode is printing charaters to the screen instead of returning a string as it should be.
  3. I don't know where you've read that about shortcodes and PHP but it is is sort of nonsense, at least as you've presented the case. Shortcodes virtually all do more, much more, than echo static strings-- take a look at the Core gallery shortcode, for example. To do that, you need PHP.
| improve this answer | |
  1. Probably because of not closed DIVs or CSS. You have to check on the source code of the page.

  2. There is a good practice (and also to avoid issues) to return shortcode content, instead of outputting it directly (which is what the get_footer() does).

Besides that, I really don't think you should use a shortcode to call a footer. I can see so many best ways better than that: using meta fields, page templates, even using basic ID comparison.

| improve this answer | |

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.