Wordpress: Display only category name within The Loop - php

I want to display only the category name within The Loop, without link, markup or anything else. This:
<?php the_category(); ?>
only gets me the category name as a list object with a link.

This will work:
$category = get_the_category();
echo $category[0]->cat_name;

Related

Wordpress categories: how to display name, slug, description and link inside single post?

What I want to do is, within single.php, pull different properties of the Category separately, like this:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<a class="CATEGORYSLUG" href="CATEGORYLINK">
<i class="fas CATEGORYDESCRIPTION"></i>
<span>CATEGORYNAME</span>
</a>
<?php endwhile; ?>
<?php endif; ?>
To produce an end product like this:
So that I can use:
the category slug as a CSS class, to style each category a unique color
the category description as a Font Awesome class (in this case, "fa-wrench"), to assign each category a unique icon
(For this project, each post will only be assigned a single category, but I suppose a future-proof solution would need to output ALL categories assigned to the post in this format.)
So I really just need to know how to individually pull:
Category slug
Category link
Category description
Category name
You can use the get_the_terms() function to get all of the categories assigned to that post object and loop through each one to create your icons etc.
Below is an example, I've called each individual variable so that you can see the object properties clearly.
This will also work if you only have one category assigned.
//get all categories:
$categories = get_the_terms(get_the_ID(), 'category');
//loop through categories
foreach ($categories as $category) {
//get variables
$slug = $category->slug;
$link = get_term_link($category->term_id);
$descr = $category->description;
$name = $category->name;
//echo your content
echo '<a class="'.$slug.'" href="'.$link.'"><i class="fas '.$descr.'"></i><span>'.$name.'</span></a>';
}
You can get this using following code.
$postcat = get_the_category( $post->ID );
Its return all the fields which you want for more info refer this https://developer.wordpress.org/reference/functions/get_the_category/

Hide a custom input element if category

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.

Anchor CMS: Including category ID in post list

I have a normal posts page in an Anchor CMS theme. Beside the posts list is a category list, which I am intending to use as sorting filters for the blog posts. The category list has been output as follows, based on the Anchor Docs:
<?php foreach(Category::dropdown() as $id => $category): ?>
<div class="filter" data-filter=".category-<?php echo $id; ?>"><?php echo $category; ?></div>
<?php endforeach; ?>
I then have to include a corresponding class in each article or post within that list, which would look like this:
<?php if(has_posts()): ?>
<?php $i = 0; while(posts()): ?>
<article class="mix category-<?php echo category_id(); ?>">
<?php endwhile; ?>
<?php endif; ?>
Notice that the article class category-[num] corresponds with the data-filter on the div "filter". This is what allows sorting.
However any way I try to do this I am getting either doubled up posts or just not working. I had tried using a foreach statement as seen in the docs:
<?php foreach(Category::dropdown() as $id => $category): ?>
<article class="mix category-<?php echo category_id(); ?>">
<?php endforeach; ?>
but this makes posts double up, I assume because it is within a while loop?
In the database, categories and posts are in two separate tables, however the category ID is included in the posts table. I have looked for a way to echo this e.g. article_category_id but with no success so far.
How can I include the category ID in the posts list?
Okay, found the answer to my own question.
This was a simple solution actually, once I thought about it. The various function references in Anchor CMS such as article_category etc can be defined by the user by accessing anchor > functions > articles.php.
Within this file, and based on existing functions such as
function article_category() {
if($category = Registry::prop('article', 'category')) {
$categories = Registry::get('all_categories');
return $categories[$category]->title;
}
}
I created a new function for the category ID. It looks like this:
function article_category_id() {
if($category = Registry::prop('article', 'category')) {
$categories = Registry::get('all_categories');
return $categories[$category]->id;
}
}
and then echoed this out in the posts loops:
<?php echo article_category_id(); ?>
Simple as that!

Linking to category magento

I'm looking to get a product on a home page slider in magento to link to the category it is in... so far i have:
<?php
$allIds = $product->getCategoryIds();
foreach($allIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
?>
<?php echo$category->getName() ?><br/>
<?php
}
?>
(this runs within a foreach item) This provides me with the categories (which is great), however the:
<?php echo $category->getCategoryUrl() ?>
Does not seem to link to the correct place (it actually doesn't give me anything). Can anyone help on the matter?
if you want to show only one category link, you don't need to load categories in the loop:
$category = $product->getCategory();
$url = $category->getUrl();
Update: I just realized that the 1st line may not work on the homepage. But you still do not need the loop:
$category = $product->getCategoryCollection()->getFirstItem();
try this
<?php echo Mage::helper('catalog/category')->getCategoryUrl($category);?>
You will find everything related to displaying categories and subcategories here Display Categories and SubCategories in Magento. Hope this will be helpfull..

Display Only One Category

On my WordPress theme, it displays all of the categories a post is in on the homepage for the post block, I only want to display one category even if a post is under multiple categories.
i.e. Category One, Category Two, Category Three
I want it to be Category One...
This is the code that is in place at the moment:
<h2><?php the_category(', ') ?></h2>
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
What do you mean by first category? The following code will show only the one category, but it will sort the categories by name.
<?php wp_list_categories('orderby=name&show_count=1'); ?>

Categories