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;
}
Related
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
I'm using the Custom Field Plugin to add a custom Field and call it to the content using the_field(); in my single.php
I just want to display this field on the all the categories except the "Articles which is 13", so I was trying somethin' like this:
<?php if !is_category( '13' ); { ?>
<h3 class="single-product__price"><?php the_field('precio'); ?></h3>
<?php } ?>
But it is not working. I'm wondering how to do this.
You should use in_category, and put the conditional statement in parentheses.
<?php if (!in_category( $cat )) { ?>
<h3 class="single-product__price"><?php the_field('precio'); ?></h3>
<?php } ?>
Make sure $cat is either ID (integer), name or slug (string) of the category.
Edit: I'm assuming you actually want to display the input on posts that are not in a particular category, hence in_category. If you do indeed want to display it on all other Category archive pages, then is_category would be correct.
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]');
}
So in a nutshell, you have two options for displaying Contacts in Joomla:
Show all Joomla Contact Categories.
Show all Joomla Contacts in a single Category.
I want to use the first option, but merge a list underneath each Category showing the list of contacts within that category, and a link to their profile.
The simplest way I thought of this was to edit a template override of the file com_contact/categories/default_items.php
I found a point where I want the list to appear, and then copied and pasted the code from the Category view (that generates the list of contacts).
<ul>
<?php // Add list of contacts for each category
foreach ($this->items as $i => $item) : ?>
<li>
<a href="<?php echo JRoute::_(ContactHelperRoute::getContactRoute($item->slug, $item->catid)); ?>">
<?php echo $item->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
But I am assuming I can't just copy and paste, as there needs to be an extra node added to $this->items.
At the moment, no list is being generated, just the <ul> outside the foreach loop.. but also interestingly, the <li> and the <a> IS being generated.. but linking to the current page I'm on (Probably because $item->slug is still being seen as the category).
So can anyone point me in the right direction as to how to reference the contacts within a category? All I'm after is the name and the slug/URL.
UPDATE:
I saw this in the same file (default_items.php) and although I realise it's referring to child categories... would this be a place to start for the actual contacts within the categories?
<?php if (count($item->getChildren()) > 0) :?>
<div class="collapse fade" id="category-<?php echo $item->id;?>">
<?php
$this->items[$item->id] = $item->getChildren();
$this->parent = $item;
$this->maxLevelcat--;
echo $this->loadTemplate('items');
$this->parent = $item->getParent();
$this->maxLevelcat++;
?>
</div>
<?php endif; ?>
BUMP - Does anyone have any experience with this? Or being able to call individual contacts when viewing a category? How are they linked?
For Category view in file default_children.php after tag <li... add code:
<?php
// Get Category Model data
$categoryModel = JModelLegacy::getInstance('Category', 'ContactModel', array('ignore_request' => true));
$categoryModel->setState('category.id', $child->id);
$categoryModel->setState('list.ordering', 'a.name');
$categoryModel->setState('list.direction', 'asc');
$categoryModel->setState('filter.published', 1);
$contacts = $categoryModel->getItems();
?>
For Custom Fields add this after previus code:
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
foreach($contacts as $contactItem) {
$currentContFields[] = FieldsHelper::getFields('com_contact.contact', $contactItem, true);
}
I am displaying the featured image for all my posts on one page (the image then links to the individual post). I want to give each post from the same category the same class (which will be the name of the category slug), so for example, if a post is in a category called "cat1" I would like the div that contains the post to have the class "cat1" and so on.
So far I have this:
<article class="<?php get_the_category($post->ID)?>">
but this just returns blank, can someone tell me what I'm missing here please.
The Codex is your friend:
// add category nicenames in body and post class
function so20621481_category_id_class($classes) {
global $post;
foreach((get_the_category($post->ID)) as $category)
$classes[] = $category->category_nicename;
return $classes;
}
add_filter('post_class', 'so20621481_category_id_class');
NB: for this to work, your article element should contain the post_class template tag:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>