Display A div only on a single page - php

I was working on a word press theme. I created a slider using revolution slider. I need to display it in my category page named wedding. so i pasted the following code into my header.php
<?php echo do_shortcode('[rev_slider wedding]'); ?>
Now as it is in my header.php the slider is displaying in all pages.
The category listing page has a class named term-66 where 66 is the category id.
So i want to display the slider only in that particular page. Some thing like
if is class .term-66
{
<?php echo do_shortcode('[rev_slider wedding]'); ?>
}
Not sure on applying the above logic or some jquery ?? Please help Thank You.

if it is a custom taxonomy named listing you should use is_tax($tax, $term) instead:
if ( is_tax( 'listing', 'wedding' ) ) {
echo do_shortcode('[rev_slider wedding]');
}

You can use is_category to check if the current page is, in fact, the category you're looking for.
You can pass is_category a Category ID, Category Title, Category Slug or Array of IDs, names, and slugs, as per the documentation.
if( is_category( 66 ) ) {
echo do_shortcode('[rev_slider wedding]');
}
If you're using a custom taxonomy, then you can use is_tax in the same way:
if ( is_tax( 'listing', 'wedding' ) ) {
echo do_shortcode('[rev_slider wedding]');
}

Related

How to echo a shortcode in a specific product category

I have this php code and it displays a button in the single product page. Everything works fine but the button generated from the shortcode is displayed in EVERY product category.
I want to display the button from shortcode ONLY in a specific product category.
<?php
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote()
{
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
?>
I tried this but the site crashed
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote()
if ( in_category( $cat 'category-slug' ) ) {
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
Let's see if I can help you!
First of all you're missing two curly brackets in your function and that is why your site crashed. You need to wrap your function with the curly brackets to avoid this.
What I think you want to do is to only execute the shortcode if it's a certain product category archive page. You can do this by using if( is_product_category() ) You need to define the category either by slug, id or title.
Try this function and change "example" to the title of your category. Let me know how it works out!
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote() {
if( is_product_category('example') ) { //change "example" to category title, id or slug
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
}

Woocommerce | Need to show content in a div only if a certain product category is selected

I need to show content in a div in Woocommerce only if a certain product category is selected
I'm editing archive-product.php file and have added
<?php
if(in_category('cg-odobreno')){
?>
<div>
/**/
</div>
<?php } ?>
This in_category part is related to actual categories not product categories if I'm not mistaken. How can I select a product category?
You're looking for is_category. More # https://developer.wordpress.org/reference/functions/is_category/
<?php if( is_category( 'cats' ) ) {
//... Best cats page template ever...
} else if( is_category( 'dogs' ) ) {
//... Dogs are better anw!...
} else if( is_category( array( 'cats', 'dogs' ) ) ) {
//... Oh boy! Cats & dogs in the same spot! things are gonna get uggly...
} else {
//... Sadly no dogs nor cats here...
}; ?>
You can use slugs, names or ids.
In_category() will use in case if you want to check that category exists in particular post/product
In your case you can do it with is_category().

Style post archive blog preview based on category

I have a page showing your typical series of small post teasers - images, excerpt etc which advertise properties for lease.
I would like to have the ability to add a 'new' or 'featured' icon based on its corresponding 'new' or 'featured' category or tag - either will do.
I have added these categories but they do not appear in the code when output and so I cannot target them.
How would I be able to perform the action:
If a post thumbnail has category of 'new' add the class 'new' so I can then target and style - repeating for each category.
I found this which I think is similar but does not work
There will be multiple categories displaying on the archive page, but I want to style only the previews that have a certain category - I do not want to style the individual post page.
Unfortunately my php skills are limited
Thanks
$post = $wp_query->post;
if ( in_category('new', $post->ID) ) { ?>
<body <?php body_class('new'); ?>>
<?php
}
elseif ( in_category('featured', $post->ID) ) { ?>
<body <?php body_class('featured'); ?>>
<?php
}
else { ?>
<body <?php body_class('class-name-generic'); ?>>
<?php
}
?>
here you go get_the_category($post->ID); will return the array of categories of that post you need to loop through the array
$category_detail=get_the_category('4');//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}

Output custom code on a defined sub category archive pages only in WooCommerce

Actually In WooCommerce, I am trying to display some custom code (a video), only in a defined product sub-category archive page.
This is the code I am using:
<?php if( has_term('viola-chan','product_cat' $tag_ID=18) ) { ?>
my video html code
<?php } ?>
But it doesn't work and it is showing up on other parent pages too.
Here is a live link
How can I output custom code on a defined sub category archive pages only in WooCommerce?
As this is related to display something on product category archives pages, you should use instead:
<?php if( is_product_category( 'viola-chan' ) ) { ?>
<div>my video html code here</div>
<?php } ?>
But "viola-chan" should be your sub-category hereā€¦
Related documentation: WooCommerce Conditional Tags: Product category page
Try third variable post id as shown below:
global $post;
if(has_term( 'viola-chan','product_cat', $post->ID )) {
//do something if post has viola-chan term
}else{
//do something else if the post don't have viola-chan term
}
Ref.- https://wordpress.stackexchange.com/questions/166576/if-product-is-in-sub-category-show-code

Conditional if statement for a particular WP page

I am using Woo Commerce plugin and I want to display extra text for a specific product page.
The product ID seen in my body is:
single single-product postid-2624
So I tried the following code but it didn't work:
<?php
function ip_more_content() {
if ( is_single('2624'); ) {
echo 'show something';
}
else {
echo '';
}
}
add_action( 'woocommerce_product_thumbnails' , 'ip_more_content', 9 );
?>
How can I make WP do something based on a specific product id?
I guess that 'woocommerce_product_thumbnails' is running inside the loop, so you cab grab item id by simply using get_the_ID() function. So, edit your conditional like this:
if ( get_the_ID() == 2624 ) {
And it should work it out.

Categories