I have a post type called "Milestones" which has several custom fields and custom a template to render it. What I am looking for is a way to return all the rendered output from a shortcode.

The reason I am trying to do this is that I have 11 "milestone" sections with a lot of common HTML but there are some differing images and text so I thought to create a custom post type with a template having the common HTML and I am using the custom fields (ACF Pro plugin) to insert the parts which differ.

Now I am trying to use a shortcode that would display the whole rendered sections (common HTML and custom field values included).
What I have so far is this:

function shortcode_company_milestone($atts){
    $atts = shortcode_atts(array(
        'year' => '2017'
    ), $atts );

    $milestone = get_page_by_title($atts['year'], OBJECT, 'milestone');

    return apply_filters('the_content', $milestone->post_content );

The problem with this is that $milestone->post_content only returns the text that is in the default content editor of the post type, which I am not actually using as I am mainly using the custom fields. It also doesn't return the template behind the post type, which is the common HTML. It seems to me that the post is not 'rendered' which is what I really need. Can someone point me in the right direction?

  • In general you just need to copy the relevant section of your template, but anywhere that you output HTML you will need to add it to a string which you return at the end of your shortcode function. – Andy Macaulay-Brook Jun 22 '18 at 17:42
  • @AndyMacaulay-Brook you mean to copy the relevant section of my template into the shortcode function? I was trying to avoid that. The reason I created a template was because of the "cleaner" approach of having a separate template file (which I could then use with an HTML editor) instead of having to type HTML code line by line in a php function (and all the awkwardness that that entails for maintenance). – frezq Jun 24 '18 at 13:02

Your Answer

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

Browse other questions tagged or ask your own question.