Customizing WordPress Categories List - php

Having a few issues in customizing a WordPress categories list and would appreciate any examples of how I can achieve the following:
I have created a WordPress categories list that shows the count for all categories. However, I am unable to show the count for show_option_all ie show the total number of posts for all categories combined in the same way that all the other categories display the post count?
How can I add a separator between each category list item ie between each li?
If category list appears on a particular page, is their a way of giving a specific category within the list, an active state?
Please see existing code below:
<ul>
<?php
$categories = wp_list_categories('title_li=&show_count=1&echo=0&child_of=18&show_option_all=ALL');
$categories = preg_replace('/<\/a> \(([0-9]+)\)/', ' <span class="count"><sup>\\1</sup></span></a>', $categories);
echo $categories;
?>
</ul>
Any help would be appreciated.
EDIT: Ended up using the code below which achieved the desired results:
<ul>
<?php
$categories = wp_list_categories('title_li=&show_count=1&echo=0&child_of=18');
$categories = preg_replace('/<\/a> \(([0-9]+)\)/', '</a> <span class="count"><sup>\\1</sup></span></li><span class="separator">/</span>', $categories);
echo $categories;
?>
</ul>

Use Like
<?php
$terms = get_terms('category'); /*Name Of category*/
foreach ( $terms as $term ) {
echo $term->name.'<br/> '.$term->count.'<br/>';
}
?>
Output

Related

How to show post counts of different categories by shortcode in wordpress

i want to display multiple categories with their post counts with help of short code without plugin.
i found many functions code here but didn't work with multiple categories with shortcode.
how to create function that is use to display all categories post counts using shortcode.
Example:
category 1: 20 posts
category 2: 10 posts
category 3: 15 posts
the display results 20,10 or 15 should be shown by help of shortcode.
This is a basic way to show a list of categories with their post counts. You could use other objects to link to the category pages etc. From what you described you just need a list, but this is the foundation.
function category_post_count() {
ob_start(); //php output buffer
$categories = get_categories(); ?>
<ul>
<?php foreach($categories as $cat) { ?>
<li><?php echo $cat->name . ': ' . $cat->count; . ' posts '; ?></li>
<?php } ?>
</ul>
<?php $endBuffer = ob_get_clean();
return $endBuffer;
}
add_shortcode( 'category_post_count', 'category_post_count' );
Then all you need to do is add this to the area where you want it to show up on the page...
[category_post_count]

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/

Can I show a list of contacts within category, on the 'List all contact categories' page in Joomla?

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);
}

Remove parent item in a sub category list (wp_list_categories)

UPDATE:
Nevermind, stupid error: it wasn't actually showing the parent title, it was a sub category with the same name.
I have the following list to display the child categories of the current category and I'd like to remove or hide the parent item, showing only the children. Is it possible without writing a function in functions.php?
HTML:
<div class="tabs">
<ul>
<?php $this_cat = (get_query_var('cat')) ? get_query_var('cat') : 1; ?>
<?php $this_category = get_category($this_cat);
if ( $this_category->parent ) { $this_cat = $this_category->parent; } ?>
<?php wp_list_categories('child_of=' . $this_cat . '&title_li='); ?>
</ul>
</div>
RESULT:
"issue of the year" is the parent which I want to remove
Thanks,
If you have id of category parent ,you can use get_term_children
Ex:
$chlidrenCategories = get_term_children( categoryParentId ,'Category');
Nevermind, stupid error: it wasn't actually showing the parent title, it was a sub category with the same name.

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..

Categories