Question

I've created a shortcode for my theme that will add a portfolio to a page/post. Because the code for the portfolio is quite long and complex, I have put it in a separate file and am trying to use get_template_part to call it from my shortcodes.php file.

Here is the code I currently have:

function portfolio_new($atts) 
{        
ob_start();  
get_template_part('/includes/shortcode_helpers/portfolio', 'shortcode');  
$ret = ob_get_contents();  
ob_end_clean();  
return $ret;    
}

add_shortcode('portfolio_new', 'portfolio_new');

However, this returns nothing. My file containing the portfolio php is located in incudes/shortcode_helpers/portfolio_shortcode.php

Can anyone point out where I have gone wrong here

Was it helpful?

Solution

You need to rename your template file to portfolio-shortcode.php (with a dash rather than an underscore). get_template_part is checking for the following file:

{current_theme}/includes/shortcode_helpers/portfolio-shortcode.php

You also need to change the first parameter of get_template_part to remove the leading slash:

get_template_part('includes/shortcode_helpers/portfolio', 'shortcode');

See get_template_part on the WordPress Codex for the order of the files this function tries to include.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top