I'm trying to display images attached to custom taxonomies on the individual product pages. I've used the Custom Taxonomy plugin (https://wordpress.org/plugins/taxonomy-images/) and added the images using Advanced Custom Fields. I have 2 custom taxonomies called 'range_logo' and 'features', each with images assigned to them. I need to output the images to the single product page using the hook 'woocommerce_before_add_to_cart_form'.
As you can see below, I have tried outputting a line of text to test if the hook is working correctly, and it is, but the taxonomy image isn't shown when I add the code which is supplied with the Taxonomy Images plugin.
add_action(
"woocommerce_before_add_to_cart_form","product_taxonomy_image");
function product_taxonomy_image () {
echo 'Taxonomy image here';
print apply_filters( 'taxonomy-images-queried-term-image', '' );
}
Expected results are that the appropriate taxonomy image for the product would be displayed. Instead I got this error:
Notice: term_taxonomy_id is not a property of the current queried object. This usually happens when the taxonomy-images-queried-term-image-id filter is used in an unsupported template file. This filter has been designed to work in taxonomy archives which are traditionally served by one of the following template files: category.php, tag.php or taxonomy.php. Learn more about template hierarchy. in /var/sites/a/mysite.com/public_html/wpnew/wp-content/plugins/taxonomy-images/legacy/includes/public-filters.php on line 398
I'm using the Avada theme - maybe the layout of the theme's template files could be the reason for the error? I think I may need to use archive.php instead, but I don't have much experience with PHP.
I've tried a few possible solutions and have looked elsewhere for help, but I've hit a wall with getting this to work and I'm not sure what I should be trying next.
welcome to the community. First off, I would like to mention that you have written a bit confusing detail about the taxonomy images as you have mentioned using the plugin Taxonomy Images as well as Advanced custom fields
If you are using the former plugin, you can do something as shown below:
add_action("woocommerce_before_add_to_cart_form","product_taxonomy_image");
function product_taxonomy_image () {
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="my-custom-class-name">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'product_cat',
) );
}
The above code will give you the images from the category associated with that product. If it is another custom taxonomy that you created, please change the value in taxonomy parameter.
If you are using ACF you will have to get product terms first. Let me know if that's the case and I will share that code with you.
Related
So - I've been running around ways to do this and get half way there which is a bit infuriating!
Using jetpack portfolio - I want to show normal categories in my blog section and posts, and jetpack project types on the portfolio page and single projects.
My current code looks like this - and shows the jetpack portfolio types for the first post on the archive-jetpack-portfolio, and display correctly on the single project. but doesn;t work at all for the blog posts.
<?php
if (is_page_template('page-blog.php','single.php','index.php' )){
wp_list_categories(array(
'show_option_all' => '',
'title_li' => '',
'separator' => '%20',
'orderby' => 'name'
));
} else {
the_terms($posts->ID, 'jetpack-portfolio-type');
}
?>
So if these pages - show this, else show this. I know the $post-ID is where it's falling over for the archive page. Don't know why the blog bit isn't working.
Hope that's enough to go on. I bow to your infinite knowledge hive-mind!
Depending on custom template you have created you will need to call for custom footer using get_footer function. Here you can read more about get_footer function. You just need to specify $name parameter.
Monika
I'm building a custom plugin for WordPress to display some custom posts.
So far so good, it worked (I had to re-save the permalinks), and now I'm able to see it in my archive page and, since I added the slug in the array for the register_post_type function, I can see them adding the slug in the url (for example: mywebsite.com/coolposts:
'rewrite' => array( 'slug' => 'coolposts' ),
So far so good but I have a problem and I don't know how to solve it.
I want to display my custom posts "coolposts" in my custom query:
$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50');
while ($the_query->have_posts()) :
$the_query->the_post();
if ((($the_query->current_post+1) % 3 == 0) && ($the_query->current_post+1 !== count($the_query->posts))):
echo "</div><div class='row'>";
endif;
endwhile;
Because, for some reason, my custom posts are excluded from the main query.
Am I doing something wrong?
Just to be clear, I don't want to display ONLY my customs posts, I want to display all of my posts.
I assume you registered custom post type with the name coolposts when registered with register_post_type function. If so then just add another param post_type=coolposts. To avoid unexpected situation you can add one more param post_status=publish, this param will ensure only published posts are displayed.
$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50&post_type=coolposts');
Using a WordPress multisite network, I need to access custom post types from our main site from within a shortcode.
For example, our main site (ID 1) is where the custom post types (Case Studies) are stored. In functions.php I have the following:
//[casestudy]
add_shortcode( 'casestudy', 'casestudy_shortcode' );
function casestudy_shortcode( $atts ) {
$a = shortcode_atts( array(
'id' => ''
), $atts );
switch_to_blog(1);
//Get fields from custom post type with Advanced Custom Fields Pro
//and return HTML output with them
restore_current_blog();
}
Then call the shortcode with [casestudy id="123"] where the ID is the post ID of the case study.
The problem is, doing this returns the Case Study HTML fine but breaks some features of the page and also populates the 'recent posts' widget with blog posts of the main site.
Any ideas on what's going wrong? Thanks.
Adding my comment as an answer:
Reading the OP code comments, it looks like restore_current_blog() is never called, because the HTML is returned before calling it.
Make sure that the return part is the last line in there.
First,I know wordpress won't allow you to use template for posts (WooCommerce's product page is built via post). So I look for the Template Hierarchy.
It says there:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post.
For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
single-{post-type}.php – If the post type is product, WordPress would look for single-product.php.
single.php – WordPress then falls back to single.php.
singular.php – Then it falls back to singular.php.
index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.
So I created a template and name it single-product-ccc (ccc is one of my product's slug name), but nothing happened, nothing was affected.
But by creating a template named single-product will affect all of the product pages.
Why is that happening?
I don't get it. Even a single-2313.php (2313 is one post's id) will overwrite the default single.php for that 2313 post.
Why single-product-slug is not working in the same way?
Thanks.
Let me first correct you at one point. You say: "WooCommerce's product page is built via post".
This is not correct. WooCommerce creates a new post type 'product'. You can see all custom posts types that WooCommerce creates here: https://docs.woocommerce.com/document/installed-taxonomies-post-types/
The 'product' post type uses the template single-product.php.
As you say, WordPress Template Hierarchy Docs say that you can create a template for a particular product:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post. For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
WooCommerce is a WordPress plugin and as so, it doesn't always follow WordPress rules strictly, and this is one of those cases. But you can use a filter to correct this very easily. In your theme's functions.php simply add:
add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
function custom_single_product_template_include( $template ) {
if ( is_singular('product') && (get_the_ID()==30)) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-30.php';
}
return $template;
}
where get_the_ID() is the id of your Product. Now, you can create a new template as for example single-product-30.php
Try adding this in functions.php
function your_theme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'your_theme_add_woocommerce_support' );
and now onwards, you can use woocommerce/single-product.php for customisation
My first post so I hope someone can help :)
I need to change http://designoriginal-test.co.uk/ef-travel/portfolio to http://designoriginal-test.co.uk/ef-travel/exhibitions
How can I go about doing this? There is no 'physical' page in the Wordpress backend so I am not able to change the slug manually.
Thanks in advance!
Zak
Looking at your site's source code I see body has classes post-type-archive and post-type-archive-portfolio, so it's not a taxonomy archive but a post type archive.
<body class="archive post-type-archive post-type-archive-portfolio nictitate-builder nictitate-header-style-1">
So, portfolio is a Custom Post Type. This allows two possibilities:
You're using a plugin to handle CPT's (eg: Custom Post Types UI).
In this case, you should have a submenu in your WP Dashboard where you could rename the post type.
Your CPT's are defined in your custom theme's (or custom plugin) source code
In this case, you should locate there (check your functions.php) some piece of code like this:
register_post_type( 'portfolio', … );
where you'll be able to rename the portfolio too.
In both cases I recommend you to backup your Database first. And if you're editing some source code files, backup them too before making changes.
Update:
After some research on the theme I've seen there's a free version of it. This version requires (via TGM Plugin Activation) a custom plugin from the same developer named Kopa Nictitate Toolkit.
function kopa_register_required_plugins() {
$plugins = array(
array(
'name' => 'Kopa Nictitate Toolkit',
'slug' => 'kopa-nictitate-toolkit',
'source' => 'http://downloads.wordpress.org/plugin/kopa-nictitate-toolkit.zip',
In this plugin they register the portfolio's CPT. So, check if you have this kopa-nictitate-toolkit folder in your /wp-content/plugins/. If so, there's a file named portfolio.php where they register the Portfolio CPT.
Edit:
You'll see that besides registering the CPT
register_post_type('portfolio', $args);
they also register some custom taxonomies.
register_taxonomy('portfolio_project', 'portfolio', $taxonomy_category_args);
register_taxonomy('portfolio_tag', 'portfolio', $taxonomy_tag_args);
You'll need to change this too so the theme won't broke. Check register_taxonomy docs.
Hope this helps!
Just go to the page and change the page name and its Permalink