I really need your help. i just created a WordPress page template that display all the post but my problem is the display of the custom parent taxonomy/category and it's children. My post look like this.
<table>
<tr>
<td>Title</td>
<td>Parent Category</td>
<td>Sub Category</td>
<td>Excerpt</td>
</tr>
<tr>
<td>a title</td>
<td>USA custom category</td>
<td>Hawaii uder USA</td>
<td>this is a sample description.</td>
</tr>
</table>
My only problem is to display the sub category. Anyone can help me?
This is my code on how i display the parent custom category:
<?php
$term_list = '';
$terms = get_the_terms( $post->ID, 'origincity' );
foreach( $terms as $term ) {
$parent_term = get_term( $term->parent, 'origincity' );
$term_list .= $parent_term->name ;
}
echo $term_list;
?>
I tried to display the sub category by this code :
<?php $terms2 = wp_get_post_terms($post->ID, 'origincity', array("fields" => "all"));
foreach ($terms2 as $term1) {
echo $term1->name.'<br>';
} ?>
but it also return the parent. :(
Your help is highly appreciated. Thanks.
I dont suggest using your method you are currently using because you don't have enough control over what's showing.
In future use get_posts();
It's really simple to use! For example:
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
As you can see you can set how many posts, the category and many more arguments for the posts displayed.
Find out more about the arguments here http://codex.wordpress.org/Template_Tags/get_posts
Already solve the problem by this code:
function print_taxonomic_ranks( $terms ){
// set id variables to 0 for easy check
$order_id = $family_id = $subfamily_id = 0;
// get family
foreach ( $terms as $term ) {
if ( $family_id || $order_id != $term->parent )
continue;
$family_id = $term->term_id;
$family = $term->name;
}
// get subfamily
foreach ( $terms as $term ) {
if ( $subfamily_id || $family_id != $term->parent )
continue;
$subfamily_id = $term->term_id;
$subfamily = $term->name;
}
// output
echo "$subfamily";
}
Thanks by the way.
Related
I am trying to pull a posts custom taxonomy into the page. The categories have subcategories and I am struggling to workout how to get the subcategories listed in hierachical way. Currently I am using the below which is showing the categories properly, but they are coming in all jumbled.
$terms = get_the_terms( $post_id, 'listing_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$categories = array();
foreach ( $terms as $term ) {
$categories[] = $term->name;
}
$categories_list = join( ", ", $categories ); ?>
<tr>
<td><strong>Categories: </strong></td>
<td><?php esc_html_e( $categories_list ) ?></td>
</tr>
<?php endif; ?>
I have attempted numerous options but they all seem to be showing all of the categories and subcategories within that custom taxonomy, not only the categories relating to this post.
Any help or guidance would greatly appreciated.
you should get parent taxonomy and then for every parent get their children
with a loop(foreach).
1- Find the parent taxonomies by this query:
$args = array (
'taxonomy' => 'listing_category',
'parent' => 0,
'hide_empty' => false
);
$allTaxParent = get_categories($args);
2- Now create a function for get children from parent:
function getChildren( $termId=0, $taxonomy='category' ) {
$children = get_categories(
[
'child_of' => $termId,
'taxonomy' => $taxonomy,
'hide_empty' => false
]
);
return ($children);
}
3- Now you have taxonomies in hierachical way:
foreach ( $allTaxParent as $tax ) {
$children = getChildren($tax->term_id, $tax->taxonomy);
echo $tax->name . '>';
foreach ($children as $child){
echo $child->name . '>';
}
echo '<br>';
}
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.
i have code in category.php
<?php $getcatid = get_query_var('cat'); ?>
<?php $args = array('child_of' => ''.$getcatid.'', 'parent' => ''.$getcatid.'', 'hide_empty' => 0);?>
<?php $categories = get_categories($args); foreach ($categories as $cat) { ?>
<?php $getid = $cat->cat_ID; ?>
how to check quantity subcategory value as:
if $categories < 4 ($getcatid had smaller 4 sub) echo code loop
else (if $categories > 4 ) echo code loop
Any idea. Thanks
If you retrieve your categories with get_categories you can easily run count() on the result, to check how many entries there are. You can then use that number for your comparison.
Probably like this (untested):
<?php
// stripped your code out
$categories = get_categories($args);
if(count($categories) > 4) { }
else { }
?>
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();
I'm trying to target the last item in a foreach loop but it's acting a little strange.
I've got two items in the loop, I've tried the following:
Not set a +/- for the count, this targets the first item.
Using -1 as the count also targets the first item.
Using +1 as the count doesn't target any items until you add a 3rd
item, then it works as intended.
Here's the code, can anyone help?
<?php $pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
foreach($pages as $key => $post)
{
setup_postdata($post);
$fields = get_fields();
?>
<div class="event<?php if( $key == (count( $pages ) +1) ) echo 'last'; ?>">
</div>
<?php } wp_reset_query(); ?>
This is the way to do it:
<?php
$pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
// keep a record of the number of pages -1
// in order to compare against 0 indexed array key
$pagesNo = count($pages)-1;
foreach($pages as $key => $post)
{
setup_postdata($post);
$fields = get_fields();
?>
<div class="event<?php if( $key == $pagesNo ) echo 'last'; ?>"></div>
<?php } wp_reset_query(); ?>
When you need to count, a for or while loop might be a better than foreach.