You can’t return the include
statement. And you shouldn’t use include
template parts.
And here’s how to do it correctly:
function custom_code() {
ob_start();
get_template_part( 'custom-template.php' );
return ob_get_clean();
}
add_shortcode( 'custom_shortcode', 'custom_code' );
So what we’re doing here is:
- Start output buffering.
- Include given template part using
get_template_part
function. - Return all the output generated by that template and delete the buffer.
Related Posts:
- Using locate-template & shortcodes doesn’t appear to work
- Make “sidebar template” the default template for new pages
- How to extract the variables out from “add_shortcode” function?
- Overwriting TwentyTwelve template file with child theme template, but lower in the hierarchy
- Load templates, pass arguments, and render output from functions.php
- Shortcode to output get_header not working
- Creating mixture of shortcodes to use in the visual/text editor
- Use AJAX in shortcode
- Include files in child theme functions file
- How to add attributes to a shortcode
- How to display random users with avatars
- Implementing DNS Prefetching with WordPress
- Child theme – overriding add_image_size by a child theme [duplicate]
- How to override functions.php in child theme?
- Can shortcodes contain conditional statements? Even without them my shortcode renders blank page
- I created a child theme and it doesn’t work for some of the css files
- Child Theme Not Overriding Parent Theme
- How to override enqueued styles using a child theme
- Dequeue script in template isn’t working
- Display a text message if the shortcode is found?
- Help to resolve Syntax error, unexpected ‘endwhile’ (T_ENDWHILE) [closed]
- Adjust which tempalte a page uses with a function?
- How to change menu icon which is overriden (i.e. by WooCommerce) [closed]
- How to store / access files in child theme folder
- How do I get a child theme to load scripts from the parent theme?
- Attach parent category template to all subcategories
- Add class or ID to any WordPress function
- how to edit functions.php in a child theme
- How to remove howdy dropdown menu content
- Display Password Protected external RSS in wordpress template
- How to enable template page only for a post id page
- Avoid loading css from parent theme
- How to get specified parent page title in my function
- Possible to display shortcode based on the category?
- Selective Product Category for Carousel
- How to change login labels
- Shortcode pagination not advancing
- Modify a function without editing template
- Having issues with a foreach inside of a shortcode with ACF gallery
- Problem only while using require_once() within functions.php
- How to filter $content in shortcode function
- Create a custom taxonomy that will be used to create and filter markers in a Google Map
- Automatically include all php files in a child theme directory
- Override the function twentytwelve_scripts_styles in a child theme
- Full page template function
- Display gallery on bottom after content
- Skeleton Child Theme Add Icon Bar to Header Flex Grid
- WordPress Shortcode function display outside of widget
- Help With Creating Shortcode
- Shotcode argument issues
- Exclude category from shortcode
- get_pages() Returns Only One Item
- How to edit classes in body tag?
- Adding an option to a shortcode
- How to use author meta in shortcode?
- Use a shortcode to display custom meta box contents
- Woocommerce checkout field
- Parent theme styles overriding child theme CSS [closed]
- Specify multiple categories for custom post template – FATAL ERROR
- Tracking the number of shortcodes for a list?
- Child theme function.php causes fatal error
- How to make a new function that includes a template
- Can I include a custom PHP function in a WordPress function?
- How to create a shortcode out of a php function
- Is it possible to use add_filter in an included file in the child theme’s functions.php?
- How to store a number (coming from cookie) into query vars for later usage in other filters?
- Solution dealing with Child Theme / Parent theme functions
- Removing parent theme CSS without editing parent theme
- How to override this theme function in child theme
- Looking for Functions File that doesn’t Exist
- Problem with shortcode inside a shortcode
- Using One Function To Create Two Different Shortcodes
- Render page with specific id from functions.php
- the_date() and the_time() functions display actual date an time instead of published date and time
- page shows short code not output
- Add Javascript code to functions.php childtheme
- Replace theme function
- How would I correctly implement a new if statement in a child functions file?
- More than one search results page template for two searches on site
- Child Theme not loading multiple stylesheets
- How to retrieve image IDs from shortcode content?
- Display list of pages that contain a certain string within the slug
- Including admin-options.php file in Child Themes
- How To Show Shortcodes In WordPress Custom Fields?
- Load a Child Theme’s style.css just before the closing tag
- Is it possible to change parameters of Parent theme function in the Child theme?
- Functions.php in child theme that loads CSS file breaks website
- unregister_sidebar in child theme not working
- Unregistering custom tinymce plugin?
- How to display data from custom fields in my custom shortcode?
- Modify function Shortcode_atts
- restrict access to specific urls on a specific period of time
- Add title & subtitle to shortcodes
- Shortcode Variations?
- shortcode inside post called by ID does not render as expected
- Child Theme not working – CSS gone
- Custom shortcode not being included in content paragraph [duplicate]
- Display function from functions.php in tag.php
- Which template file to edit to edit homepage in Mystile [closed]
- post_row_actions filter from parent theme not executing in child theme