I'm trying to customise WooCommerce's Shop page (called 'Products' in this instance) to include a template part from my main WordPress theme.
get_template_part() does not work. I believe that's because a plugin can't tell which files will exist in a theme so it's disabled. Alternatively, I wonder if it's because the function is being called from within the plugin directory, so it's looking in the wrong directory.
include() does not work. I get the following errors:
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in ...\woocommerce\archive-product.php on line 52
Warning: include(.../partials/acf-flexible-layout.php): failed to open stream: no suitable wrapper could be found in ...\woocommerce\archive-product.php on line 52
Warning: include(): Failed opening '.../partials/acf-flexible-layout.php' for inclusion (include_path='.;C:\php\pear') in ...\woocommerce\archive-product.php on line 52
note: the three dots (...) in the file paths are a reference to my theme.
Something else to note is that the template part I'm trying to include reference Advanced Custom Fields associated with the Products / Shop page in WordPress.
copying the template part's code directly into the archive-product.php does not work. I'm not sure why this is.
Any help would be much appreciated.
EDIT I have already checked How to include a file using get_template_part() in a plugin? but it doesn't seem suitable:
- The answer chosen gives
include()
as a solution. I've already said this doesn't work for me. - It's not a plugin I'm developing. It's WooCommerce. Any updates to the plugin could delete the files I add / change.
- I don't fully understand the other promising answer given, so I'm not able to adapt it to suit my needs (if that's even possible).
I'm trying to customise WooCommerce's Shop page (called 'Products' in this instance) to include a template part from my main WordPress theme.
get_template_part() does not work. I believe that's because a plugin can't tell which files will exist in a theme so it's disabled. Alternatively, I wonder if it's because the function is being called from within the plugin directory, so it's looking in the wrong directory.
include() does not work. I get the following errors:
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in ...\woocommerce\archive-product.php on line 52
Warning: include(.../partials/acf-flexible-layout.php): failed to open stream: no suitable wrapper could be found in ...\woocommerce\archive-product.php on line 52
Warning: include(): Failed opening '.../partials/acf-flexible-layout.php' for inclusion (include_path='.;C:\php\pear') in ...\woocommerce\archive-product.php on line 52
note: the three dots (...) in the file paths are a reference to my theme.
Something else to note is that the template part I'm trying to include reference Advanced Custom Fields associated with the Products / Shop page in WordPress.
copying the template part's code directly into the archive-product.php does not work. I'm not sure why this is.
Any help would be much appreciated.
EDIT I have already checked How to include a file using get_template_part() in a plugin? but it doesn't seem suitable:
- The answer chosen gives
include()
as a solution. I've already said this doesn't work for me. - It's not a plugin I'm developing. It's WooCommerce. Any updates to the plugin could delete the files I add / change.
- I don't fully understand the other promising answer given, so I'm not able to adapt it to suit my needs (if that's even possible).
- Did you tried this, already? wordpress.stackexchange/questions/124789/… – Robbert Commented Jan 25, 2018 at 15:27
- Possible duplicate of How to include a file using get_template_part() in a plugin? – Robbert Commented Jan 25, 2018 at 15:34
- FYI that's the same post, duplicated. I have edited my answer to explain that I have reviewed that answer already. It may help, or the solution may be in there, but I can't see it at the moment. – user135578 Commented Jan 26, 2018 at 12:18
- You know that you can override WooCommerce templates in your theme, right? – swissspidy Commented Jan 26, 2018 at 12:23
- Yes, thanks. I already have a number of overrides. – user135578 Commented Jan 26, 2018 at 13:05
1 Answer
Reset to default 1I have a solution to this, at least in my specific case.
Solution
get_template_part()
did work in the end, but ACF needed the ID of the post (the Shop page). When calling ACF fields from outside of the loop you need to specify a post ID.
For all of the other pages on the site where the template is being used, it knew the ID already so I didn't have to specify. Seemingly, where I was calling it from in archive-product.php was outside of the loop. If someone could clarify that, it may be helpful.
So, in archive-product.php:
<?php get_template_part( 'partials/acf', 'a-template-file' ); ?>
Then in the template file:
$post_id = '';
if ( is_shop() ) {
$post_id = 20; // 20 being the ID of my Shop page
} else {
$post_id = get_the_ID();
}
if ( have_rows('flexible_content_layout', $post_id) ) {
while ( have_rows('flexible_content_layout', $post_id) ) {
the_row();
// etc, etc...