Woocommerce Display Category Image - get_woocommerce_term_meta deprecated - wordpress

I'm in need of some help.
I want to display the category image on the current category page, and I have googled this, and each answer I find uses the same code.
They all use get_woocommerce_term_meta to retrieve the ID of the thumbnail used so that you can then use wp_get_attachment_url to get the image address.
All sounds great, but whenever I try this code it returns nothing, and I think that it is because get_woocommerce_term_meta is deprecated.
Does anyone know of a way round this, so I can get the image address when I have the category ID?
This is the code that I have in place:
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
echo $cat->term_id."<br />";
echo $thumbnail_id;
$image_src = wp_get_attachment_url($thumbnail_id);
$cat->term_id returns the correct ID of the category, but $thumbnail_id returns 0.
The code is in header.php.

get_woocommerce_term_meta() may be deprecated however it hasn't yet been removed. The issue is elsewhere in your setup.
With that said we can resolve the deprecation issue quite easily. Simply replace usages of get_woocommerce_term_meta() with the new native WordPress function get_term_meta().
get_woocommerce_term_meta() will simply pass its arguments on to the new function anyway so we can be sure the problem isn't with the deprecated function.
Likely causes of the issue:
Key used to save the image isn't the same as the key being used to retrieve
Not passing in correct term ID
No image set

Related

What is different between post_navigation() and get_the_post_navigation();?

What is difference between these WordPress functions, and how to implement it?
the_post_navigation(); get_the_post_navigation();
the_archive_title(); get_the_archive_title();
the_post_pagination(); get_the_post_navigation();
the_archive_description(); get_the_archive_description();
I already googled for this, but I'm still not getting it right.
All the functions that starts with get_ are only returning the "result" of the function : If you put this function in a php page and watch this page in a browser, nothing will be displayed.
If you want the result to be displayed, you have to add echo before the function and this is exactly what the function that starts with the_ are doing.
You may ask yourself why sometimes we want only the result to be returned by the function and not displayed. It's because sometime, we have to do some additional operations with the result before displaying it.
Example:
$content = get_the_content();
$content = str_replace('Hello', 'Bye', $content);
echo $content;
If not operation are needed so you only need to do:
the_content();
You also ask "How to implemenent it ?". To implement the function, you will have to add it in some specific php files. For example, for the get_the_post_navigation() function you will have to add it in the single.php file in your theme folder. You will need basics on php.

Trying to use a Get_user_meta variable breaks my site

I am having a weird problem. Anytime I try to use a variable I get from the WordPress function get_user_meta, it breaks the site. It is weird because every other variable works just fine and is coded exactly the same. Wether they are empty or not.
$is_optionalcopd_completed = get_user_meta( $user_id, 'is_optionalcopd_completed' );
$is_optionalasthma_completed = get_user_meta( $user_id, 'is_optionalasthma_completed' );
the bottom one works just fine, but if i even try to echo out $is_optionalcopd_completed, i just get a white screen
Any ideas what the problem may be?
Do you get any php errors (WP_DEBUG enabled)?
Try to use var_dump() instead of echo, because get_user_meta() returns default a array.
Like the notes state: make sure the meta value exists https://codex.wordpress.org/Function_Reference/get_user_meta#Notes.
Even if the user meta doesn't exists, it will not returns a blank screen, it returns an empty string.
Does your code use it after you want to echo it ?
I suggest you to verify if there is no typo error in the code.
Do you see this user meta with a var_dump(get_user_meta($user_id)); ?
That's only hints, waiting for more element to help ;-)

WP Post Meta Tags not working as expected

I have been trying to associate CUSTOM data to each of my WP POSTS using the following piece of code :
if($condition === true){
if ( ! update_post_meta ($post_id, '_someData', $someVariable) ) {
add_post_meta($post_id, '_someData', $someVariable);
}
}
However, seems like the META VALUE is RESET to default i.e. Zero OR Blank. Our WordPress website has around 40 plugins, and I think one of these WordPress plugins, is coming in my way of doing things. All of my logic works fine, on a demo WordPres website. Is there a way for me to have total control to set the META Value for a given POST ? Also, is there a way where I can be notified that the META Key is about to change and then I can decide whether OR not to change the Meta Value ?
Any pointers or reference URL's can be of great help. Thanks !
You only need to call update_post_meta in either scenario. Calling add_post_meta() is not necessary and could be causing this problem.
From the codex:
The function update_post_meta() updates the value of an existing meta key (custom field) for the specified post.
This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned.

Passing arguments to WordPress feed URL

As we all know the WordPress' feed url is www.mysite.com/feed.
I have edited the feed-rss2.php file to show thumbnails if a certain GET parameter is passed. See the code below:
<?php if($_GET['c'] == 'detailswiththumb') echo the_post_thumbnail( array(100,100) ); ?>
But when I open the feed address like this:
www.mysite.com/feed?c=detailswiththumb
The code doesn't work. Can the arguments be passed this way? Am I missing something? Please help.
Firstly, the function is get_the_post_thumbnail() not the_post_thumbnail().
Then, their is one more problem in your code, that you have to pass the post id to get its thumbnail (for more info see http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail).
So, first you will have to extract the post id from somewhere and then only you would be able to get the thumbnail. But, I think it would be very tough, so try giving up this thought, for it would take you a lot of time and no living being is going to access that path.
There were browser cache issues. Even with Google Chrome's incognito window. Had to test it with passing fake arguments like...
www.mysite.com/feed?c=detailswiththumb&fakearguments=123
...to clear the cache. And the code is fine.
Sorry for wasting your time guys.

Wordpress URL routing problem

I was wondering which is the best approach to get the catgeory ID when listing the posts within a particular category. Normally, the urls look something like this : www.example.com/?cat=4 and it is pretty easy to get the id. However, I really need the urls to be routed like this www.example.com/categories/hotels . wordpress provides an easy way to do the "pretty" routing, however all of the GET paramater information is lost this way. In this case, the $_GET variable is assigned nothing. I need to be able to say $category = $_GET["cat"] or something like that
What is the easiest approach ?
Can you use the Wordpress get_the_category function to grab the ID (from member variable cat_ID) once you're in the template?
See http://codex.wordpress.org/Function_Reference/get_the_category
e.g.
foreach((get_the_category()) as $category) {
$id = $category->cat_ID;
// do something with $id
}
The thing is that the guy might not really want to associate categories with post ids. In this case, there's the global $wp->query_vars array that contains all the data coming from the GET request even when the routing has been "prettified"
What about the category base setting in the backend? "Configuration" > "Permalinks" and there the last paragraph. See here for docs.

Resources