I'm trying to show a menu with all subcategories of a current parent category, but not the parent category. Looks simple, but I'cant remove the parent names, or show subcategories in a single post.
This is the code that I'm currently using
<?php if (is_front_page() or is_single() ) { ?>
<?php } else { ?>
<?php
if (is_category() ) {
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&title_li=&use_desc_for_title=1');
else
$this_category = wp_list_categories('orderby=id&depth=1&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 } ?>
<?php } ?>
you can use below code in where you want to display child categories
$args = array('child_of' => $this_category->cat_ID);
$categories = get_categories( $args );
echo "<ul>";
foreach($categories as $category) {
echo "<li>".$category->name."</li>";
}
echo "</ul>"
Tested and it works well.
You can get the all the subcategories of parent one using get_terms($taxonomies, $args);
First of all declare your taxonomies like below:
$taxonomies = array(
'category',
);
Now you just need to pass $args parameter in get_terms function like below;
$args = array(
'parent' => $parent_term_id, // You can use parent id here
);
$terms = get_terms($taxonomies, $args);
This way you will get the all subcategories of applied $parent_term_id in $args
Hope this will help you. Let me know if you have any doubts.
Related
I want to add a taxonomy in my div with the different other fields, but I'm new to wordpress and O don't find how doing this, if someone can help me please ?
<?php/* Template Name: Archive Projets */ ?>
<?php get_header(); ?>
<?php
$posts = get_posts(array(
'posts_per_page' => 4,
'post_type' => 'projects'
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<div>
<?php the_title(); ?>
<div><?php the_field('url')?></div>
<div><?php the_field('')?></div> /* HERE I WANT TAXONOMY */
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Here's a code that will get the taxonomies of the current post and display them in a list of insert this code under <div><?php the_field('url')?></div>
// Get all terms
$terms = get_the_terms( $post->ID , array( 'Taxonomy_name') );
// Dispaly the taxonomies, if there one or more.
foreach ( $terms as $term ) {
echo '<div>' . $term->name . '</div></br>';
}
Make sure to change the 'Taxonomy_name' with your taxonomy name. let me know if I got you wrong or if you need any help with the code.
A quick google search brings up get_terms().
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
foreach($terms as $current_term) {
//You can now loop through them and get the ones you want, display them all, or whatever else it is you want to do. Note: each $current_term is an object of type WP_Term (or error if there are no results).
}
I need a shortcode that lists all posts of a certain category.
I found this php code that works on page templates but as soon as I add it in shortcodes it doesn't work (and I really need it in shortcode format):
<ul>
<?php
$catPost = get_posts(get_cat_ID("31")); //change this
foreach ($catPost as $post) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;?>
</ul>
So how can I do it?
Here's an updated and tested version of Amin. T's code. Add to your functions.php file.
/*
* Output a simple unordered list of posts in a particular category id
* Usage e.g.: [posts_in_category cat="3"]
*/
function posts_in_category_func( $atts ) {
$category_id = $atts['cat'];
$args = array( 'category' => $category_id, 'post_type' => 'post' );
$cat_posts = get_posts($args);
$markup = "<ul>";
foreach ($cat_posts as $post) {
$markup .= "<li><a href='" . get_permalink($post->ID) . "'>" . $post->post_title . "</a></li>";
}
$markup .= "</ul>";
return $markup;
}
add_shortcode( 'posts_in_category', 'posts_in_category_func' );
It should be something like this (add this to your functions.php file)
function posts_in_category_func( $atts ) {
$category_id = $atts['cat'];
?>
<ul>
<?php
$args = array( 'category' => $category_id, 'post_type' => 'post' );
$catPost = get_posts($args);
foreach ($catPost as $post) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;?>
</ul>
<?
}
add_shortcode( 'posts_in_category', 'posts_in_category_func' );
You can call it like that [posts_in_category cat=1]
I have next code:
<?php
global $post;
$args = array( 'taxonomy' => 'product_cat');
$terms = get_the_terms($category->slug,'product_cat', $args);
$count = count($terms);
if ($count > 0) {
foreach ($terms as $term) {
echo '<div style="direction:rtl;">';
echo $term->description;
echo '</div>';
}
}
?>
The code will display category description. The problem - on sub-category it will display the sub-category description + the parent description.
How i can display the description separate: in parent - the parent description, and on sub - only the sub description?
Try this and let me know if it helped you
add_action( 'woocommerce_after_subcategory_title', 'custom_add_product_description',
12);
function custom_add_product_description ($category) {
$cat_id = $category->term_id;
$prod_term = get_term($cat_id,'product_cat');
$description= $prod_term->description;
echo '<div>'.$description.'</div>';
}
The answer:
<?php
global $post;
$terms = get_the_terms( 'product_cat',$post->ID);
echo '<div style="direction:rtl;">';
echo category_description( get_category_by_slug($terms)->term_id);
echo '</div>';
?>
You can use this code to display the product category description -
<?php global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description; ?>
Anajanas answer works very well, It can also be modified to do what I needed
which was to display a ACF custom field of a woocoommerce product child category on the shop catgegory page.
This is my code to add in functions.php
12);
function xyz ($category) {
$cat_id = $category->term_id;
$prod_term = get_term($cat_id,'product_cat');
echo "<div class='designercatname'>".get_field('designer_name', $prod_term )."</div>";
}```
I have below code here. It's display that I want category names and description as well. Now I need to display post that they have inside their categories. How I do it?
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
'child_of' => 10, //Child From Boxes Category
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<div class="one_fourth">';
echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt="" class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
$post = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 10 );
$posts_array = get_posts( $post );
echo '</div>';
}
?>
If there have any other way to get child category post and display child category name and posts in loop. Please let me to know here.
After I solved this. I think this will help for you.
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
'child_of' => 5, //Child From Boxes Category
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<div style="width:20%;float:left;">';
echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt="" class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
//echo '<br />';
$args2= array("orderby"=>'name', "category" => $cat->cat_ID); // Get Post from each Sub-Category
$posts_in_category = get_posts($args2);
foreach($posts_in_category as $current_post) {
echo '<span>';
?>
<li type='none' style='list-style-type: none !important;'><?='+ '.$current_post->post_title;?></li>
<?php
echo '</span>';
}
echo '</div>';
}
?>
Write for get all parent category
Now do a foreach loop for them and get their child category.
Now under above foreach write another foreach using this get post for child category
$parent_cats = get_categories($args);
foreach ( $parent_cats as $parent_cat) {
$child_cats = some wp functions to get child cats of current parent category
foreach ( $child_cats as $child_cat ) {
$child_cat_post = get the post of child category
}
}
Helpful links:
http://codex.wordpress.org/Function_Reference/get_categories
http://codex.wordpress.org/Template_Tags/get_posts
Wordpress has <?php wp_dropdown_categories( $args ); ?>
Example Usage (Dropdown without a Submit Button using JavaScript)
<li id="categories">
<h2><?php _e( 'Posts by Category' ); ?></h2>
<?php wp_dropdown_categories( 'show_option_none=Select category' ); ?>
<script type="text/javascript">
<!--
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
-->
</script>
</li>
Other examples can be found on the Codex site - https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
I'm trying to make a list of my recent posts that also show what category they are in right now I have
<?php $args = array( 'numberposts' => 30) ;
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>
<a href="'.get_permalink($recent["ID"]).'" title="Look'.esc_attr($recent["post_title"]).'" > '.$recent["post_title"].'</a> </li> '; } ?>
this displays the post but I would like it to also display the category name.
Any help would be great,
Thanks
$cats = get_the_category($recent["ID"]);
$cat_name = $cats[0]->name; // for the first category
You can try this inside the loop (if you have multiple categories)
$cats = get_the_category($recent["ID"]);
foreach($cats as $cat)
{
echo $cat->name." ";
}
I was able to get this to work using the following.
$cats[0]->name." "
So in the recent posts loop you can use it like this:
$args = array('numberposts' => 5, 'category' => '4,5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$cats = get_the_category($recent["ID"]);
echo '<li>' . $cats[0]->name." " . $recent["post_title"].' </li> ';
}
I was able to get a list that displays the category then the title by using the following.
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=30');?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<?php the_category(" "); ?>- <?php the_title(); ?>
<?php endwhile; ?><?php wp_reset_query()?>
I was never able to get the following code to work inside my original code It would always show up as "array" or Null (i'm guessing I just did not know the proper way to write it in) I was able to get it to show the category if I created a single post and just wanted to show the category with nothing else.
$cats = get_the_category($recent["ID"]);
foreach($cats as $cat){
echo $cat->name." "; }
I used the answer from Jaco, but
$cats[0]->name
gave me the first category from the array on each post. The adjustment I made was to use an incremental operator in my code and all is well.
A much simplified example:
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$i = 0;
$cats = get_the_category($recent["ID"]);
echo $cats[$i]->name;
$i++;
}
wp_reset_query();