I would like to use php to loop through a products categories and store them into an array, which will be assigned as class names for each product. For some reason my code is not working, and there are no PHP errors. Perhaps it is a wordpress issue:
$classes = array();
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
$classes[] = $term->slug;
}
<li <?php post_class( $classes ); ?>>
Essentially, I am trying to assign categories as class names to their respective product. This isn't throwing an error, but nothing loads. Anyone see any issues here?
Using array_shift to break up the array worked for me, but it does not work if $cats has mutliple classes
<?php $classes = array();
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
$cats[] = $term->slug;
}
$classes[] = implode(" ", $cats);
<li <?php post_class( $classes); ?>>
Related
I have a custom term meta field in wordpress, and I currently use this code to display it:
<?php $terms = get_the_terms($post->ID, 'camera');
foreach ($terms as $term) {
$term_id = $term->term_id;
echo get_term_meta( $term_id, 'model', true );
}?>
However, I need to display this meta data multiple times in a few pages. I tried creating a global variable, but I am having no luck.
This is what I tried:
I put this in my functions.php:
$camera = <"?php $terms = get_the_terms($post->ID, 'camera');
foreach ($terms as $term) {
$term_id = $term->term_id;
echo get_term_meta( $term_id, 'model', true );
}?">
And then I put this in my template file:
<p><?php global $camera;echo $camera;?><p/>
But it is not working.
Any ideas?
Thanks!
You define camera in functions.php. So why do you not make it one?
functions.php
<?php
function camera() {
$terms = get_the_terms($post->ID, 'camera');
$result = "";
foreach ($terms as $term) {
$term_id = $term->term_id;
$result .= get_term_meta( $term_id, 'model', true );
}
return $result;
}
Then require("functions.php"); in template.php, and you will be able to simply call camera():
template.php
<?php
require("functions.php");
... other code and html ...
<p><?php echo camera(); ?></p>
I am trying to get categories from a specific post type : member .
I am using this code
<?php
$taxonomy = 'category';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
But the problem is : when i am adding a category to member then it is also added to post type : post and when deleted it also deleted from both post type.
What can i do now?
try this way this will display categories from specific post and retrieve the terms for a post.
<?php
$postArg = array('post_type'=>'post',
'posts_per_page'=>-1,
'order'=>'desc',
);
$getPost = new wp_query($postArg);
global $post;
if($getPost->have_posts()){
echo '<ul>';
while ( $getPost->have_posts()):$getPost->the_post();
echo "<h2>".$post->post_title."</h2>";
$terms = get_the_terms($post->ID, 'category' );
foreach ($terms as $term) {
echo "<li>".$term_name = $term->name.'</li>';
}
endwhile;
echo '</ul>';
}
?>
Second way
<?php
$category = get_terms('category');//custom category name
foreach ($category as $catVal) {
echo '<h2>'.$catVal->name.'</h2>';
}
?>
Is there a better way to get the category names for a custom post type in wordpress?
<?php // get the portfolio categories
$terms = get_the_terms( $post->ID, 'filters' );
if ( $terms && ! is_wp_error( $terms ) ) :
$names = array();
$slugs = array();
foreach ( $terms as $term ) {
$names[] = $term->name;
$slugs[] = $term->slug;
}
$name_list = join( " / ", $names );
$slug_list = join( " category-", $slugs );
endif;
?>
<!-- BEGIN portfolio-item-->
<li class="portfolio-item third column category-<?php echo $slug_list; ?>" data-filter="category-<?php echo $slug_list; ?>">
<?php
$taxonomy = 'filters';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
<?php endif;?>
Or in fuctions.php place this:
function get_the_category_custompost( $id = false, $tcat = 'category' ) {
$categories = get_the_terms( $id, $tcat );
if ( ! $categories )
$categories = array();
$categories = array_values( $categories );
foreach ( array_keys( $categories ) as $key ) {
_make_cat_compat( $categories[$key] );
}
return apply_filters( 'get_the_categories', $categories );
}
and call the function as:
<?php $cat = get_the_category_custompost($post->ID, 'Your Custom Taxonomy'); ?>
My answer seems too simple, but I used this to list the categories from a wordpress plugin called DW Question Answer that has separate categories from the standard wp categories.
I am assuming that Design Wall used custom post types to create the q&a part and taxonomies to create the categories.
<ul>
<?php wp_list_categories('taxonomy=dwqa-question_category&hide_empty=0&orderby=id&title_li=');?>
</ul>
Assuming your custom taxonomy is recipegroups, i have implemented and tested this code in functions.php and i am sure that it will work in plugins too.
$recipeTerms = get_terms(array(
'taxonomy' => 'recipegroups',
));
foreach($recipeTerms as $recipeTerm){
if($recipeTerm->parent==0){
echo "<div class='termsBox'>"; // remove these div's to suit your needs..
$termLink =get_term_link( $recipeTerm );
echo "<a href='$termLink'><div class='termParent'>".$recipeTerm->name."</div></a> ";
$termChilds = get_term_children($recipeTerm->term_id, 'recipegroups' );
foreach($termChilds as $child){
$chTerm = get_term_by( 'id', $child, 'recipegroups');
$termLink =get_term_link( $chTerm );
echo "<a href='$termLink'><div class='top-cat-items'>".$chTerm->name."</div></a>";
}
echo "</div>"; // end of termsBox div
}
}
Either this is harder than it needs to be or I am just not understanding WordPress/PHP very well :( All I want to do is show the child/sub categories of a specific parent category...but only if the post is in those subcategories. Specific example:
I am building a wine reviews website and these are the categories:
BrandSubcategory 1Subcategory 2etc.
RegionSubcategory 1Subcategory 2etc.
GrapeSubcategory 1Subcategory 2etc.
The parent categories will never change, and every post will have at least 1 subcategory selected under each parent, so in the LOOP I can just list the parents by name. But I am needing to dynamically output the subcategories, something like this:
Brand: <?php list_post_subcategories('brand'); ?>
Region: <?php list_post_subcategories('region'); ?>
Grape: <?php list_post_subcategories('grape'); ?>
Is there any easy way like this? It seems like this should be a basic function in Wordpress? I've looked at the functions 'get_categories' and 'in_category' but they don't seem to be able to do this.
<?php $post_child_cat = array();
foreach((get_the_category()) as $cats) {
$args = array( 'child_of' => $cats->cat_ID );
$categories = get_categories( $args );
if( $categories ) foreach( $categories as $category ) {
echo $category->cat_name; }
} ?>
try this
I posted to Wordpress Answers to get more help and #Milo gave a great code solution:
// get top level terms
$parents = get_terms( 'category', array( 'parent' => 0 ) );
// get post categories
$categories = get_the_terms( $post->ID, 'category' );
// output top level cats and their children
foreach( $parents as $parent ):
// output parent name and link
echo '' . $parent->name . ': ';
// initialize array to hold child links
$links = array();
foreach( $categories as $category ):
if( $parent->term_id == $category->parent ):
// put link in array
$links[] = '' . $category->name . '';
endif;
endforeach;
// join and output links with separator
echo join( ', ', $links );
endforeach;
You can use array_map so you can return only the categories that you want. For example:
array_map( function( $cat ) {
if ( $cat->parent != 0 ) {
return $cat;
}
},
get_the_category()
);
function get_people_cats($taxonomy) {
$output = '';
$terms = get_terms($taxonomy);
$count = count($terms);
if ( $count > 0 ):
foreach ( $terms as $term ):
$output .= "'". $term->name ."'". '=>';
$output .= "'". $term->term_id."',";
endforeach;
endif;
return $output;
}
This function returns a list of custom taxonomies and which words are found if the function is called in a template. But I want to assign the the function values to a variable in functions.php, and it's returning nothing.
If your Taxonomies are actually creaetd with a plugin instead of creating them with code in your functions.php the result of get_terms will be empty untill the taxonomies have initiated, which is on plugin level initiation better yet, most probably using the 'init' hook, so you would have to hook your function after the hook and use it only in your theme template files (not in functions.php) basicly you do something like:
add_action('init', 'get_people_cats', 9999);
Then you would call it normaly: $cats = get_people_cats('person_category');
Hope this solves your issue (I know it took me around an hour to solve this when I ran into it).
If you're not getting terms back it's probably due to where the taxonomy is being registered. If it's being registered in an init hook then you're likely trying to print them out before the taxonomy has actually been registered.
You can test it with the following:
function get_people_cats( $taxonomy ) {
$output = '';
$terms = get_terms('category');
$count = count( $terms );
if ( $count > 0 ):
foreach ( $terms as $term ):
$output .= $term->name.'=>'.$term->term_id;
endforeach;
endif;
return $output;
}
function echo_cats() {
echo get_people_cats('taxonomy_name', array('hide_empty' => 0) );
}
add_action('wp_footer', 'echo_cats');
By hooking into wp_footer it's not being called until well after any plugins that might have registered the taxonomy.
// UPDATE //
Got it. To create an array just do this:
$terms = get_terms($taxonomy, array('hide_empty' => false) );
if( !is_wp_error( $terms ) ) {
foreach( $terms as $term ) {
$types[$term->term_id] = $term->name;
}
}
return $types;
}
That will give you an array of $term->id => $term->name -- you may want to reverse that depending on how you're using the array.
Try this, works fine in my functions.php file on a local site:
function get_people_cats( $taxonomy ) {
$output = '';
$terms = get_terms( $taxonomy );
$count = count( $terms );
if ( $count > 0 ):
foreach ( $terms as $term ):
$output .= $term->name.'=>'.$term->term_id;
endforeach;
endif;
return $output;
}
echo get_people_cats('category');