Get term of post belong - php

I had this query:
$args=array(
'post_type' => array('product'),
'order' => 'DESC',
'orderby' => 'rand',
'showposts' => 24
);
query_posts( $args );
if(have_posts()) : while(have_posts()) : the_post();
?>
<div class="col-xs-12 col-s-6 col-sm-4 col-md-3 our-products">
<a class="products-img" href="<?php the_permalink();?>"><?php if ( has_post_thumbnail() ) {the_post_thumbnail('product-size',array('class' => 'img-responsive'));}?></a>
<a class="products-tit" href="<?php the_permalink();?>">
<h2><b><?php the_title();?></b></h2>
</a>
</div>
<?php
endwhile; endif;
wp_reset_query();
?>
I need to get tax term of product belong to it.
Example:
I had Category: Products -> Men fashion/Women fashion -> T-shirt -> product A.
How can i get the term of T-shirt which product A belong to it?

You can use WordPress get_the_terms() function for this purpose.
Here is the example:
$_terms = get_the_terms( $post->ID , 'Your Taxonomy' );
In "Your Taxonomy" you put the name of the taxonomy that you just registered e.g. product-category
So just like this:
$_terms = get_the_terms( $post->ID , 'product-category' );
And then
<?php
foreach($_terms as $_term) {
?>
<a href="<?php echo get_term_link($_term->slug, 'product-category') ?>">
<?php echo $_term->name ?>
</a>
<?php
}
?>
This function get_term_link() is use for term link.
Hope this will help you.

Related

Products url out side WooCommerce shop page

I'm trying to get WooCommerce products loop on home page, everything works fine like product title, products id, product image but the product permalink is not working.... getting the home URL by get_permalink()
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 6,
'post_status' => 'publish',
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => 0
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<div class="product-image">
<?php the_post_thumbnail(); ?>
</div>
<div class="product-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="product-id">
<h6><?php echo $product->get_id();; ?></h6>
</div>
Add to cart
<?php
endwhile;
wp_reset_query();
?>
But Add to cart(permalink) is not working, it's not getting product url, have tried different methods:
1. <a href="<?php echo esc_url( $product->get_product_url() ) ?>">
<?php
$productId = $product->get_id();
$productUrl = get_permalink($productId);
?>
2. <a href="<?php echo $productUrl; ?>">
But none of above worked.
More precisely you can use the add to cart URL.
$product_addtocart_url = $product->add_to_cart_url();
For getting the permalink, use below code.
$product_perma_link = $product->get_permalink();
You can use this product class method.
/**
* Product permalink.
*
* #return string
*/
public function get_permalink() {
return get_permalink( $this->get_id() );
}

How do I prevent overlaps for 'get related content' in a Masonry Grid?

I have a working '$related = get_posts' masonry on a single.php page. I also added a hover overlay, so that when the user hovers on the thumbnail, a transparent overlay appears as well as the descriptions (title name, category and nickname).
The problem I am facing is that when I hover on one related post thumbnail, the overlay appears for every post (the overlay is stretched, it is not appearing individually). I've also attempted to call out the descriptions, but it's only calling the current post I am viewing (e.x. the current single.php header says Snow, when I hover the first related post, it also calls out the description for Snow), however, if you click on the first related post thumbnail, it takes you to a different article (it is not calling the description for the different article).
<div class="related">
<h3>Related</h3>
<div class="js-masonry">
<div class="overlay">
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'orderby' => 'rand', 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) { setup_postdata($post); ?>
<?php the_post_thumbnail(array(300,300)); ?>
<?php } wp_reset_postdata(); ?>
<div class="posts">
<?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>
<h1><?php the_title();?></h1>
————
<h4><?php the_author();?></h4>
</div>
</div>
</div>
</div>
As the title says, how do I pull the correct description and overlay for one post via hover for ' $related = get_posts' on the single.php page in WordPress?
I resolved the issue by reorganizing the code correctly.
<div class="js-masonry">
<?php $args = array(
'category__in' => wp_get_post_categories( get_queried_object_id() ),
'posts_per_page' => 3,
'orderby' => 'rand',
'post__not_in' => array( get_queried_object_id() )
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="item-masonry overlay">
<a href="<?php the_permalink();?>">
<div class="posts">
<?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>
<h1><?php the_title();?></h1>
————
<h4><?php the_author();?></h4>
</div>
<?php the_post_thumbnail(array(300,300)); ?>
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>

Restricting posts to a specifc category and subcategory

On this site: http://www.palmbeachwoman.com/stories/ I used a programmer to customize the template. His code for this page is:
<?php
$args = array( 'numberposts' => 18, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
echo '<ul id="latest_stories">';
foreach ($postslist as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
<span class="s_thumb"> <?php the_post_thumbnail( 'full' ); ?> </span>
<span class="s_title"> <?php the_title(); ?> </span></a>
<span class="s_cotegories">More Stories from <?php the_category( ', ' ); ?></span></li>
<?php endforeach; ?>
The posts were supposed to be restricted to a specific category (#82) and any subcategories within it. I know how to do this with the traditional post display: http://alijafarian.com/how-to-display-wordpress-posts-for-a-specific-category/ but since I didn't write this code, I'm not sure how to modify it.
You can set the category in the array past to get_posts
$args = array( 'numberposts' => 18, 'post_status'=>"publish",'post_type'=>"post",
'orderby'=>"post_date", category=>82);

How do I create the category pages?

I'm doing something similar. I have a recipe blog and I will have different categories such as breakfast, dessert, lunch, etc... But I don't know how to link them the category pages. For example, If the users click "see all" for the breakfast category, it will show all breakfast post from the breakfast category. So that means it will link to the main breakfast category page. For each category i will show 10 recent post so the users is not seeing all post. So they have the option to see all the post if they click the "see all" link. It will link them to the main category page with all breakfast post. Of course if they click the dessert "see all" link, it will link them to the dessert category page which they can see all dessert posts.
This is my current code:
(but what php code should i put for the "see all" link? IF you take a look at my "see all" a tag, it's # right now. It's not linking to somewhere, but how do i make it dynamic to link to the categories pages. So for example if i'm in the dessert section, if i click "see all" it will link to the dessert category. )
Please also take a look at my design
<?php
get_header();
?>
<!-- recipe -->
<section class="recipe-wrap">
<?php
/*
* Loop through Categories and Display Posts within
*/
$post_type = 'recipe';
$category_link = get_category_link($cat->cat_ID);
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) : ?>
<div class="recipe-category owl-carousel-slide">
<div class="row">
<h2><?php echo $term->name; ?>see all</h2>
<div class="recipe-category-carousel owl-carousel owl-theme">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => 10, //show all posts
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<div class="item recipe-box">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo(types_render_field('artwork', array('raw' => true) )); ?>">
<p><?php the_title(); ?></p>
</a>
</div>
<?php endwhile; endif; ?>
</div>
</section>
<?php endforeach;
endforeach; ?>
</div>
</div>
</div>
</section>
<!-- /recipe -->
<?php
get_footer();
?>
Please see the example that I provided, so you know what i'm talking about.
I have attach some images and link.
This is the example of the links.
This is the blog link
https://iamsteve.me/blog
Design Category link
https://iamsteve.me/blog/category/design
An example what I want
If you try to link to each category page, you can use get_term_link() like so:
$category_page_link = get_term_link($term);
<h2><?php echo $term->name; ?>see all</h2>
The above goes inside the second foreach loop.
EDIT:
This is the full code, I put the function get_term_link() where it should be. Also you had some problems with opening/closing html tags:
<?php get_header(); ?>
<?php
/*
* Loop through Categories and Display Posts within
*/
$post_type = 'recipe';
$category_link = get_category_link($cat->cat_ID);
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
?>
<section class="recipe-wrap">
<?php foreach ( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$term_page_link = get_term_link($term); ?>
<div class="recipe-category owl-carousel-slide">
<div class="row">
<h2><?php echo $term->name; ?>see all</h2>
<div class="recipe-category-carousel owl-carousel owl-theme">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => 10, //show all posts
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query( $args );
if ( $posts->have_posts() ):
while ( $posts->have_posts() ) : $posts->the_post(); ?>
<div class="item recipe-box">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo( types_render_field( 'artwork', array( 'raw' => true ) ) ); ?>">
<p><?php the_title(); ?></p>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- .owl-theme -->
</div><!-- .row -->
</div><!-- owl-carousel-slide -->
<?php endforeach; // terms loop ?>
<?php endforeach; // taxonomies loop ?>
</section><!-- /recipe -->
<?php get_footer(); ?>
Hope that helps.

Get categories for a single post in a custom post type

I'm trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. This list will eventually serve as a class for a div ($CATEGORYSLUGSWILLEVENTUALLYGOHERE).
I've found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. Here's what I have so far:
<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>
<?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>
<div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">
<div class="port-item-home">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
And here's what I've tried so far to get the category list:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'taxonomy' => 'project-type'
);
$categories = get_categories( $args );
echo '<p> '.print_r(array_values($categories)).'something</p>'
?>
I have it returning the array - but the array is showing that it'll display all categories instead of the ones pertaining to that specific post.
I also tried:
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'project-type';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?>
And that also displays all categories instead of the ones pertaining to the post.
Any suggestions??
Got it! Found this article that helped me out:
https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type
<!-- The Query -->
<?php
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'menu_order' );
$custom_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
$terms_slugs_string = '';
$terms = get_the_terms( $post->ID, 'my_post_type' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term_slugs_array = array();
foreach ( $terms as $term ) {
$term_slugs_array[] = $term->slug;
}
$terms_slugs_string = join( " ", $term_slugs_array );
}
?>
<div class="box<?php echo $terms_slugs_string ?>">
<div class="port-item-home">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
</a>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; ?>

Categories