I don't get it!!! I have managed to include the product category description in their respective pages. I have 6 categories which with four of them I need to add a description. From the four, three worked as it should. However, there is one of them that not only shows its description but also another category's.
There are two of each category for multiligual purpose. But you can see the four categories I need to add the description.
1 - Tops
2 - Bottoms
3 - One Pieces
4 - Sport Wear
The first three worked as it should. Ie. Bottoms category page.
But the fourth, Sport Wear keeps showing not only its description but also the Top category's as well.
I have already recreated the category Sport Wear, but it remained the same way. I have no clue why this is happening. Any idea would be very much appreciated.
Here is the link: https://morenabeachwear.com/en/product-category/sport-wear/
And here the code I used to loop and display description.
function action_woocommerce_archive_description( ) {
global $post;
$args = array( 'taxonomy' => 'product_cat');
$terms = wp_get_post_terms($post->ID,'product_cat', $args);
foreach ($terms as $term) {
if (is_product_category()) {
echo $term->description;
}
}
};
add_action( 'walker_edge_after_page_title','action_woocommerce_archive_description', 10, 2 );
It turned out I didn't need the loop. And made sure to echo the first item of the array returned from wp_get_post_terms
function action_woocommerce_archive_description( ) {
global $post;
$terms = wp_get_post_terms($post->ID,'product_cat');
if (is_product_category()) {
echo $terms[0]->description;
}
};
add_action( 'walker_edge_after_page_title', 'action_woocommerce_archive_description', 10, 2 );
Related
I have problem with my catogories in my custom post type. I am trying to do a basic apartments booking system. I created custom post called Apartments and then I added a categories. The category tree looks like this:
Countries
- Spain
-- Barcelona
-- Madrid
- Greece
-- Athenes
and so on.
I want to achive effect when the user enter to the apartment in Barcelona, the higher category will be displayed in header (in this example Spain). When the user eneter to the apartment in Athenes the Greece category should be in header. I tried solution that I have found on stack but it does not work for me.
$category = get_the_category();
foreach ($category as $cat) {
echo $cat->name;
}
$ancestors = get_ancestors( $category[0]->term_id, 'category' );
$direct_parent_id = $ancestors[0];
echo $direct_parent_id;
Only the second category is displayed and as you can see the categories are displayed in alphabetical order instead of hierarchical.
I hope you understand what I mean.
Thank you in advance for directions
Function get_the_category returns the object WP_Term. So, it's possible to get the parent from that object.
Used this answer, in your case it will be:
First, add this function to your functions.php file of active theme/child theme to have access to it from other files of theme/child theme:
function get_deep_child_category( $categories )
{
$maxId = 0;
$maxKey = 0;
foreach ( $categories as $key => $value )
{
if ( $value->parent > $maxId )
{
$maxId = $value->term_id;
$maxKey = $key;
}
}
return $categories[$maxKey];
}
Note: in some cases this will not work as expected. For example, if your post has category Some_category, which is child of Some_category_parent, and the Some_category_parent was created after your Spain category, then, this will return the category Some_category.
Next step will be to use it. For example, in your post template:
$categories = get_the_category();
if ( $categories ){
$deep_child = get_deep_child_category( $categories );
echo get_cat_name($deep_child->parent);
}
After getting the deepest category object with function get_deep_child_category, we got the ID of it's parent category. get_cat_name() returned the name of parent category using it's ID. In your case, it's Spain.
This is my very firs time to play around with wordpress. The theme I selected have a nice static look but that look disappears in dynamic mode. I followed some web tutorials to add my own function that fetches posts in one category and print in the position. I thought it was working fine until i noticed it is messing up the data. Here is my function first:
$args = array( 'posts_per_page' => 6, 'offset'=> 0, 'category' => 6,'orderby'=>'id','order'=>'desc' );
$posts = get_posts($args);
if(!empty($posts)){//yes we have posts
//loop results now
foreach($posts as $story) {
setup_postdata( $story );
the_ID();
echo '<br/>'.the_title();
echo '<br/>'.the_excerpt();
}//end loop
}//
The problem is the_ID and the_title() alaways take after the first record while the exceprt shows correct value of both records.
I did print array on posts variable and it holds two distinct ids and titles for the two records I have. But when I print, only the firs one is showing.
Someone suggested in some blog the correct way is :
$title = apply_filters('the_title', $story->title);
$content = apply_filters('the_content', $story->content);
but then the title and content variables are null/blank.
Any idea what I am doing wrong please? I am newbie to wordpress so sorry.
It is solved...avoid using get_posts() function and use WP_query() instead.
I’m trying to set a sidebar in the single product page that show all products of the same categories as the product displayed.
That's how I proceed:
1) First I’ve created a sidebar called “Products_of_same_Category” to put in there a widget to show what I needed, then in function.php of my child theme I added the following snippet to execute php code in text widget:
// Enable PHP in widgets
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
2) Then when I saw that the snippet runs ok I added that code to test it:
<?php
$prod=get_the_term_list( $post->ID, 'product_cat');
echo $prod;
?>
And all worked fine, it gave me the exact name of the current category of the product displayed in the single product page.
3) So I've tried another test, deleting the previous code, to view if a shortcode translated in php should works in a widget too (writing at this time the exact category name requested, in this case "towels" - in the code below I substitute it with THE-CATEGORY-I-LIKE):
<?php echo do_shortcode('[product_category category=“THE-CATEGORY-I-LIKE” per_page="20" columns="1" orderby="title" order="desc"]'); ?>`
And all was again well done!
4) Finally I mixed all in this code to show the list of products of same categories but something goes wrong:
<?php $prod=get_the_term_list( $post->ID, 'product_cat', '', '', '' );
echo do_shortcode('[product_category category="'.$prod.'" per_page="20" columns="1" orderby="title" order="desc"]'); ?>
In last case the code doesn't display anything. I don't understand where I made mistakes, the syntax is wrong or the solving approach is illogical?
I really appreciate any help about this.
The problem is how you get the category slug. get_the_term_list will give you a formatted linked list of the categories, so it will display category names, not category slugs, which are different things. "Towels" would be your category name, but the category slug would be "towels". And product_category shortcode expect a slug, not a name.
The correct approach to get the category product slug is the following:
$terms = get_the_terms($post, 'product_cat');
if($terms && ! is_wp_error($terms)) {
foreach($terms as $term) {
echo do_shortcode('[product_category category="'.$term->slug.'" per_page="20" columns="1" orderby="title" order="desc"]');
}
}
This will display the products of all the categories associated to your product. See get_the_terms doc for reference.
In order to remove from the results the current product shown, you can make use of the woocommerce_shortcode_products_query filter. It isn't documented, but you can find it by looking at the product_category shortcode code located in includes/class-wc-shortcodes.php. In the product_category() method you will find the following line:
$return = self::product_loop( $query_args, $atts, 'product_cat' );
Where $query_args is a WP_Query parameters array. In the same class you will find the method product_loop() called here and see the following:
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts ) );
So the query arguments are filtered - you will be able to work with that to add the desirated behavour. What you want to do is to use the post__not_in parameter to the query like this:
function remove_current_product_from_wc_shortcode($args, $atts) {
if(is_product()) { // check if we're on a single product page
$args['post__not_in'] = array(get_queried_object_id());
}
return $args;
}
add_filter('woocommerce_shortcode_products_query', 'remove_current_product_from_wc_shortcode');
This code should go in your theme functions.php - please not this is untested, so if it doesn't work look at the get_queried_object_id() return if it contain the current product ID.
Have Woocommerce setup with a range of variable products. In the variable tab I've setup a unique price, image, description, weight & dimensions for each item.
All variable data displays as expected on the front-end except the dimensions & weight.
Despite hours of searching, I cannot find any documentation, tutorials, hints on how to hook into it.
Have Woocommerce templates setup and know that I will need to hook into the do_action( 'woocommerce_single_variation' ); in variable.php.
Anyone know how to get each variable's dimensions & weight to display beneath the variable description?
If you have the variation ID, you can use it to create a new WC_Product(). This object will then have properties available on it for the $length, $width, and $height. See the docs here (at the bottom under "Magic Properties").
To get the variations for a given product, you can use the global $product and then the get_available_variations() function.
global $product
$variations = $product->get_available_variations();
foreach ( $variations as $variable_array ){
$variation = new WC_Product( $variable_array['variation_id'] );
echo "The length is {$variation->length}.";
}
If you want to display additional information regarding your variable product add this function to your child theme’s function.php (or plugin). You’ll probably want to alter the html tags to fit your theme:
add_filter( 'woocommerce_product_additional_information', 'tim_additional_tab', 9 );
function tim_additional_tab( $product ){
$variations = $product->get_available_variations();
//print the whole array in additional tab and examine it
//echo '<pre>';
//print_r($variations);
//echo '</pre>';
//html and style to your likings
foreach ( $variations as $key ){
echo $key['image']['title'].'<br>';
echo $key['weight_html'].'<br>';
echo $key['dimensions_html'].'<br>';
}
}
In Wordpress how do I list custom post types on a category page divided by the subcategory the posts are in?
My example:
I have the custom taxonomy advertisements. I also have a custom post type called advertisement.
Category structure:
Footer
Sidebar
Sponsors page
Gold
Silver
Bronze
If I go to myurl.com/sponsors-page/ it will display all the ads in the gold, silver and bronze category. So far so good. But I want it to display them in order of the subcategories and echo the subcategory name. For example:
Gold
Ad 1
Ad 2
Silver
Ad 3
Ad 4
Bronze
Ad 5
Ad 6
How do I accomplish that? Feel free to question my approach, I'm new to Wordpress.
I feel like this is probably a duplicate, but trust me when I say that I´ve tried to search.
I just started with Wordpress as well, but here's what I think you want to do.
// get available taxonomies
$taxonomies = get_object_taxonomies ( (object) array ('post_type' => 'subcategory' ));
// loop all taxonomies
foreach( $taxonomies as $taxonomy ) {
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
// loop through the terms
foreach( $terms AS $term ) {
// get posts
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug" );
// check for posts
if ( $posts-> have_posts() ) {
// how your header (gold,silver,bronze)
echo '<h2>' . $term-> name . '</h2>';
// loop through posts
while ( $posts-> have_posts() ) {
// get the post
$posts-> the_post();
// show your ad
echo $posts-> post-> post_content;
// Update temporary value
$posts_count++;
}
}
}
}