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>
Related
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>';
}
?>
I am trying to redirect a user to previous category page when he clicks on Add to Cart on product page. But on redirect I am seeing a blank product_cat attribute. i.e. example.com/?product_cat=
However if I echo it to woocommerce_product_thumbnail it shows the link perfectly. i.e. example.com/?product_cat=shoes
add_filter ('add_to_cart_redirect', 'redirect_to_previousCat');
//add_filter ('woocommerce_product_thumbnails', 'redirect_to_previousCat');
function redirect_to_previousCat() {
global $woocommerce, $post;
$product_cat_slug;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_slug = $term->slug;
break;
}
$url=get_site_url().'?product_cat='.$product_cat_slug;
return $url;
}
As I mentioned in the comments, the global $post is not set up yet when the add_to_cart_action() method runs on the init hook.
Instead, I suggest you follow Wootheme's lead and get the product ID from the $_REQUEST global.
add_filter ('add_to_cart_redirect', 'redirect_to_previousCat');
function redirect_to_previousCat( $url ) {
$product_id = absint( $_REQUEST['add-to-cart'] );
$product_cat_slug = '';
$terms = get_the_terms( $product_id, 'product_cat' );
foreach ( $terms as $term ) {
$product_cat_slug = $term->slug;
break;
}
if( $product_cat_slug ){
$url = add_query_arg( 'product_cat', $product_cat_slug, site_url() );
}
return $url;
}
Hello I've made custom post type and add categories(taxonomies) to this. So I have custom post type called portfolio with a few categories for example: websites, logo, etc. and I want to get link to this categories. I tried like this:
<?php
// Get the ID of a given category
$category_id = get_cat_ID( 'Website' );
$id = get_term_by('name', 'Website', 'portfolio_category');
// Get the URL of this category
$category_link = get_category_link( $category_id );
?>
But it doesn't work. How can I get the link to this custom post type categories.
I believe you can use WordPress's get_term_link() as below:
$terms = wp_get_post_terms( $post->ID, 'category');
foreach ($terms as $term) :
echo ''.$term->name.'';
endforeach;
you need to alter category queries via pre_get_posts for custom post type.
function wpa_cpt_in_categories( $query ){
if ( ! is_admin()
&& $query->is_category()
&& $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'portfolio' ) );
}
}
add_action( 'pre_get_posts', 'wpa_cpt_in_categories' );
You can also refer get_term_link from here
$terms = get_terms('Website');
echo '<ul>';
foreach ($terms as $term) {
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
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
}
}
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); ?>>