This code inserting main and sub categories.
<?php
foreach((get_the_category()) as $category){
$cat_link = get_category_link($category->cat_ID);
echo ''.$category->name.'';
}
?>
i want just main one category name.
Related
I'm trying to display Sub categories on main category page.
For example i have a category named as : live tv
And it is consist of some sub categories like : sports tv, cartoon tv, entertainment.
I want to display these sub categories with images inside content area of main category.
I was able to assign images to categories using plugin.
I have tried this code in category.php
<?php
if (is_category()) {
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&show_count=0
&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.
"&echo=0"); else
$this_category = wp_list_categories('orderby=id&depth=1&show_count=0
&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.
"&echo=0");
if ($this_category) { ?>
<ul>
<?php echo $this_category; ?>
</ul>
<?php } ?>
But that code isn't showing anything on my category page.
you can also get sub category as:
<?php global $wpdb;$prefix=$wpdb->prefix;
$subcateogyr_list=$wpdb->get_results("Select * from ".$prefix."term_taxonomy WHERE parent='parent_category_id'");
foreach($subcateogyr_list as $subcat
echo $subcat_name=$wpdb->get_var("select name from ".$prefix."wp_terms where term_taxonomy_id='$subcat['term_id']'");
}
?>
I have been struggling to extract the parent category posts from Wordpress. When i am trying to display the only parent category posts the wordpress query displaying the sub-categories posts as well.
How can omit the sub-category posts. please help!!..
<?
// Get the post ID
$id = get_the_ID();
//get all the categories ( this function will return all categories for a post in an array)
$category= get_the_category( $id );
if ($category->category_parent == 0) {
//extract the first category from the array
$catID = $category[0]->cat_ID;
//Assign the category ID into the query
$verticalNavigationSwitcher = "cat=$catID&orderby=ID&order=ASC";
}
$result = new WP_Query($verticalNavigationSwitcher);
//$featuredPosts->query('showposts=5&cat=3');
while ($result->have_posts()) : $result->the_post();
?>
<li><a href='<?php the_permalink() ?>'><span><?php the_title(); ?></span></a></li>
<?php
endwhile;
wp_reset_postdata();
?>
Try using this query:
$verticalNavigationSwitcher = "category__in=$catID&orderby=ID&order=ASC";
I have 7 Categories (parents), and every category has 15 sub-categories.
When I select some category (parent), I want to display only sub-categories (children) of that particular parent category (parent).
After I click on sub-category (child) then it should display its posts only.
I have a fron_page.php and category.php.
How can I write this to first show sub-categories separately, then post of that sub-category separately in new file, which user want to see.
This code should help you:
<ul>
<?php
$cats = get_the_category();
$mycat = $cats->cat_ID;
wp_list_categories('orderby=id&child_of='.$mycat);
?>
</ul>
OR
<?php
if (is_category()) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>')
{
echo '<ul>'.$this_category.'</ul>';
}
}
?>
Let me know please.
Good luck! :)
1) Showing only Subcategories:
<?php
// if the page visitor views is a category page
if (is_category())
{
$cur_cat = get_query_var('cat');
if ($cur_cat)
{
$new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat . '&depth=1&title_li=&&show_count=1&hide_empty=0');
echo '<ul>' . $new_cats . '</ul>';
}
}
?>
2) Showing All Top Categories:
<?php
wp_list_categories('depth=1&title_li=&exclude=1&show_count=1&hide_empty=0');
?>
3) Showing All Top Categories + Subcategories like a Tree menu:
Use plugin, called FoCal
4) view this topic
http://wpworks.wordpress.com/2011/01/13/displaying-categories-and-subcategories-tree-on-wordpress/
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'); ?>
I am conceptualizing a new Magento site which will have products that are included in several categories. What I am wondering is if I can display all categories a product is in on the product detail page. I know that it is possible to get the category, but is it possible to display a list of all categories which a product belongs to?
For example, a shirt may be included in the Shirts category, as well as in Designers and Summer. Ideally, I would like to be able to display the following:
More from:
Men > Shirts
Men > Designers > Barnabé Hardy
Men > Summer
This will get you the data you are looking for such as the category's name, URL, etc:
$currentCatIds = $_product->getCategoryIds();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('name')
->addAttributeToSelect('url')
->addAttributeToFilter('entity_id', $currentCatIds)
->addIsActiveFilter();
then just iterate over the collection e.g.
foreach($categoryCollection as $cat){
echo $cat->getName().' '.$cat->getUrl();
}
Simple.
$_categories = $_product->getCategoryCollection()
foreach ($_categories as $_category)
//do something with $_category
You can use the following code to display all categories related to the selected product in the product detail page.
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
< <?php echo $_category->getName() ?>
<?php endforeach; ?>