I want to get the value of the category and say that if I am in a specific category such a text will appear and if it is not then another text will appear
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
if($category_id == 83){
echo '123';
}else{
echo '1234';
}
This is what I tried to do and it doesn't work for me. Is there a way to make it work well?
editing:
It is important for me to note that I want to display within the post the number of the category to which the post is associated.
WordPress Get Category ID from Slug
Here we pass the $slug to WordPress’ get_category_by_slug() function. That gives us an object of useful category data, including the ID, which we then call directly:
$slug = 'sales'; // assume Sales is category name you can get cat id from slug name
$cat = get_category_by_slug($slug);
$catID = $cat->term_id;
Related
I'm building a wordpress theme, but got stuck on a problem. It's on the blog archive page. When you go to the blog, you see a list of blog posts. And on each post preview, you see the author, title, et cetera. My problem is that I'm trying to display one category for each post preview. I don't want to show people an entire list of every category a post has. I want to limit the results of the_category() to ONE.
You could do something like this:
$category = get_the_category(); echo $category[0]->cat_name;
However, that will just return the first category name on the list. Instead, I'd suggest using a plugin to set the primary category.
For instance, with WP Category Permalink, (possibly outdated) you can get the primary category like this:
<?php
$perma_cat = get_post_meta($post->ID , '_category_permalink', true);
if ( $perma_cat != null && is_array($perma_cat)) {
$cat_id = $perma_cat['category'];
$category = get_category($cat_id);
} else {
$categories = get_the_category();
$category = $categories[0];
}
$category_link = get_category_link($category);
$category_name = $category->name;
?>
<?php echo $category_name ?>
I have an advanced custom field set up to show on a woocommerce subcategory that allows the user to define a colour via the color picker field.
This field will apply that colour to a number of elements related to that sub category (Styling the sub category thumbnail, the product page itself etc).
Im currently using as per the the ACF documentation this code to pull the field in and display it on the subcategory page:
$term = get_queried_object();
$color = get_field('colour', $term); // Get ACF Field
This works fine until it comes to the parent category for the sub pages. I am unable to call the field in for the sub categories of the parent. I understand I need to use get_terms(). I am unable ot get that working though.
This is some code I found which I have added to the loop on content-product_cat.php. However it just breaks the woocommerce loop. What would I need to do to this code to get the parent category page to show all the child subcategories each with its related color field?
// current term
$current_term = get_queried_object();
// child terms
// this returns an array of terms
$args = array(
'taxonomy' => 'YOUR TAXONOMY HERE',
'parent' => $current_term->term_id,
// you may need other arguments depending on your needs
);
$child_terms = get_terms($args);
// you need to maybe loop through the child terms gotte
// to pick which one you want to use
// I'm assuming that you only want to use the first one
$child_term = false; // set it to false to begin with
// we'll use this later
if ($child_terms) {
$child_term = $child_terms[0];
}
// make a decision
if ($child_term) {
// get field value(s) from child term
$color = get_field('color', $child_term);
} else {
// get field value(s) from current term
$color = get_field('color', $current_term);
}
// do something with the values
echo $color;
I found the solution here:
https://wordpress.stackexchange.com/a/341632
add_action('woocommerce_after_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
the_field('krotki_opis_kategorii', $term_id);
}
From Alex Uleberg:
get_queried_object_id on shop archive page it will return the id of the page and not the category. When you use the hook, the $category object will be passed in through the hook.
How can you get a product by it's name? I know you can do it by ID and SKU, but for my current situation I need to grab a product by its title. I've been looking it up and can't seem to find the function.
My function occurs on the single product page, but the product I need to get data from WILL NOT be the current product that the user is looking at. Instead it will have the same name as the current product, just ending with a different symbol. (the product the user will be looking at ends with "-r" and the one I need to search for ends with "-$$$")
So far in my Functions.php:
function fill_refurbished_addon_selectbox(){
//get id of current product
global $product;
$id= $product->get_id();
//get name of product
$currentName = get_the_title($id);
//remove -r, add -$$$ and store in var
$searchFor = substr($currentName, 0, -2) . "-$$$";
echo $searchFor;
//find $$$ product by title
$coreCharge = ***GET PRODUCT BY TITLE ($searchFOr)***;
//get price of $$$ product, store in var
//populate dropbox
}
add_action('woocommerce_before_add_to_cart_button', 'fill_refurbished_addon_selectbox', 20);
The reason is, I need to fill a select box with info from the -$$$ product.
please try this
$product = get_page_by_title( 'Product Title', OBJECT, 'product' );
get_page_by_title retrieves a post given its title. If more than one post uses the same title, the post with the smallest ID will be returned.
syntax:- get_page_by_title( $page_title, $output, $post_type );
How can I get "original" category name in Magento where I already translated category name for current store view. I would like to get category name as entered in All Store views because I would like to send original (English) name to GA for tracking - I need this when I'm on category page.
I can get localized category name in this way:
<?php
$currentCategory = Mage::registry('current_category');
$name = $currentCategory->getName();
?>
But couldn't find a way to get untranslated name without additional calls to database.
As mentioned above, this will need additional database requests. The following should work:
$defaultStoreCategory = Mage::getModel('catalog/category');
$defaultStoreCategory->setStoreId(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
$defaultStoreCategory->load($currentCategory->getId());
$name = $defaultStoreCategory->getName();
Try with this code
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->load(YOUR_CATEGORY_ID);
Hope this helps.
$skin_type = "green";
$category_id = get_cat_ID( single_cat_title("", false));
if($category_id == 218){
$skin_type = "blue";
}
I have this code at the top of my header to select different backgrounds for my categories.
It works great when navigating between Categories with the menu, but it single_cat_title() does seem to work when I click a post to enter the single.php . What am I doing wrong and how can I get around it?
Thanks
single_cat_title() works on category pages because the category is included in the query vars. On a single page, there isn't a category name or id included in the query vars, just the post id.
to take a look at the query vars, include the following in your page, either in the header or on category.php and in single.php to see the difference.
<?php var_dump($wp_query->query_vars);?>
You'll want to use get_the_category($post_ID) to get the categories of the single post. The function returns an array of objects, one for each category assigned to the post. To grab the first, or only category, use the array index "0". If used outside the loop, pass the post id to the function. Inside the loop, it will default to the current post id. Since you are controlling your header with this function, I bet you'll be passing the post id.
$skin_type = "green";
$post_id = $_GET['p'];
$category = get_the_category($post_ID);
$category_id = $category[0]->**cat_ID**;
$if($category_id === 218){
$skin_type = "blue;
}
Edit:
I realize I was assigning the category name to $category_id above, I've changed the object property to cat_ID which is the proper property
Udate to answer question in comment
If post "SFSF" is listed in categories 1,2,3, and 4, and you want to change the color of category 3 when the post is viewed on single.php, then you must add the category ID to the link that you click, assuming your navigation is category based. When you click on category 3 in your navigation menu, you'll see in the query string
"www.example.com/cat=3"
Let's assume this is the title of the post.
<a href = "<?php the_permalink();?>/my_cat=<?php echo $_GET['cat'];?>">
<?php the_title();?>
</a>
Here, you've added the custom query var "my_cat" and assigned it the value of the current page's category id.
On single.php, Instead of getting the category ID from the category name like I show above, you'll retrieve it from the query string similar to the way you retrieve the post ID.
$skin_type = "green";
$category_id = $_GET['my_cat'];
$if($category_id === 218){
$skin_type = "blue;
}