WordPress permalink to product - php

I am listing out product categories on my website but for some reason the permalink to the product inside the loop is just leading the user to a blog post and not to the product or category etc:
<div class="mobile-show">
<?php
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms( 'product_cat', $args );
foreach( $product_categories as $cat ) { ?>
<?php echo '<ul class="cat_list_mobile">'; ?>
<a href="<?php echo get_permalink(); ?>">
<?php echo '<li><div class="col-group-2">' . $cat->name . '</div><div class="col-group-2 text-right"><i class="fa fa-chevron-right"></i></div></li>
</a>
</ul>';
}
?>
</div>
It is displaying the categories as expected, maybe I am missing something here?

If you need to get permalink of the product category then use get_category_link( $category_id );
More in the codex: https://codex.wordpress.org/Function_Reference/get_category_link

Related

Display Woocommerce product categories with child and product number in grid

I have to display product categories grid, where a single column will look like this:
child category
main category
number of products
I am trying to do this with the following code. Instead of "main category" I get the "child" name, which is the same in all cases.
<?php
$taxonomy = 'product_cat';
$parent_cat_ids = get_terms( $taxonomy, array(
'parent' => 0,
'hide_empty' => false,
'fields' => 'ids'
) );
$subcategories = get_terms( $taxonomy, array(
'exclude' => $parent_cat_ids,
'orderby' => 'name',
'order' => 'asc',
'hide_empty' => false,
) );
$category_id = $cat->term_id;
if( ! empty( $subcategories ) ){
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo '<li>
<h4><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</h4>
<a href="'. get_term_link($subcategory) .'" >' . $subcategory->name.'</a>
</li>';
echo ' ('.$cat->count.')';
}
echo '</ul>';
}
?>

WordPress get_terms() function not display custom taxonomy categories for woocommerce

The following code I wrote is supposed to show all the product categories of a wordpress ecommerce website.
<?php $categories = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => 'false',
'numberposts' => -1)
);
?>
<?php var_dump($categories); ?>
<?php foreach( $categories as $category ): ?>
<h4 class="shop-category-name d-inline"><?php echo $category->name; ?></h4>
<?php endforeach; ?>
I'm using it inside a woocommerce hook that is responsible to render the contents before the main shop page, the woocommerce_before_main_content.
I'm not able to get the categories, I will see only one category and the others are not listed. I'm not sure about, but maybe this can be something related to the fact that I'm using the function inside a woocommerce hook? I had a similar issue with the shop page featured image, I was not able to display it because of this motivation and I have modified the code to use the wc_get_page_ID('pag name').
Is there a fix ?
Try to use it like this in the function, you used for woocommerce hook woocommerce_before_main_content
add_action( 'woocommerce_before_main_content', 'woo_cats', 20, 0 );
function woo_cats(){
$cat_args = array(
'orderby' => 'name',
'order' => 'asc',
'hide_empty' => false,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '<ul>';
foreach ($product_categories as $key => $category) {
echo '<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
}

How to display all available custom post categories in WordPress?

How to display all categories of a custom post type on home screen without listing the items.
I already created the custom post type and it's categories, now I need to display all the categories on my home page as links to the each category page. Can someone help please?
You can use now get_categories
Here is an example of code:
<?php
$args = array(
'taxonomy' => 'Your Taxonomy Name',
'hide_empty' => 0,
'orderby' => 'name'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link($cat->slug); ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Remember write your taxonomy name as you registered, in here 'Your Taxonomy Name'
e.g. product_cat, blog_cat etc
Hope this will help you.
$cat_args = array(
'taxonomy' => 'your-custom-post', // your custom post type
);
$custom_terms = get_categories($cat_args);
echo print_r($custom_terms);
<?php
$terms = get_terms( 'taxonamy_name', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach($terms as $term)
{
echo $term->name;
}?>
</ul>
</div>
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;

WooCommerce display product categories in product title

I have a Wordpress (version 4.2.2) eCommerce site running WooCommerce (version 2.3.8).
On my individual product page I wish to set the title of the product to also include the custom categories I have created in WooCommerce and that this product belongs to.
I find the following file (wp-content/themes/mytheme/woocommerce/single-product/title.php) that relates to the title of the individual product and edit it as below to try and include the categories that this product belongs to in the title as well.
With the code below I manage to display the categories, but the problem is that I am displaying ALL categories, and not just the categories that this product belongs to.
How do I limit the categories returned to only the categories that the product belongs to please?
<?php
if ( ! defined( 'ABSPATH' ) )
exit; // Exit if accessed directly
?>
<!-- Original Product Title START -->
<h1 itemprop="name" class="product-title entry-title">
<?php the_title(); ?>
</h1>
<!-- Original Product Title END -->
<!-- New Product Title START -->
<h1 itemprop="name" class="product-title entry-title">
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{
if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
?>
<?php
echo '<br />'. $cat->name .'';
?>
<?php
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats)
{
foreach($sub_cats as $sub_category)
{
echo '<br/>'. $sub_category->name .'';
echo apply_filters( 'woocommerce_subcategory_count_html', ' <span class="cat-count">' . $sub_category->count . '</span>', $category );
}
}
?>
<?php
}
}
?>
</h1>
<!-- New Product Title END -->
you can simply get all the categories assign to product by using get_the_terms
$terms = get_the_terms( get_the_ID(), 'product_cat' );
foreach ($terms as $term) {
echo '<h1 itemprop="name" class="product-title entry-title">'.$term->name.'</h1>';
}
Use the get_categories function to retrieve categories name :-
You can use this code to display product categories name -
<?php global $post, $product;
$categ = $product->get_categories();
echo $categ; ?>

Loop through categories and create tab for each Wordpress and Bootstrap 3

I'm trying to create a page that holds all categories of my custom post type as tabs, with a tab content.
I am able to display all the category names as tabs, but i need to run a query in each tab content area to the corresponding category.
So when I click on tab named "1" the tab content area should only show posts from the category belonging to the tab named "1".
My code so far:
<?php
echo '<ul class="nav nav-tabs" role="tablist">';
$args = array(
'hide_empty'=> 1,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<li><a href="#' . $category->name.'" role="tab" data-toggle="tab">' .
$category->name.'</a></li>';
$cat_name = $category->name;
}
echo '</ul>';
echo '<div class="tab-content">';
foreach($categories as $category) {
echo '<div class="tab-pane" id="' . $category->name.'">';
?>
<?php
$the_query = new WP_Query(array(
'post_type' => 'acme_product',
'posts_per_page' => 100,
'category_name' => $cat_name
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<h1><?php the_title(); ?></h1>
<?php
endwhile;
?>
<?php }
echo '</div>';
?>
The problems is that each content area displays all post of every category.
What i want to achieve: Link
Any suggestions?
'category_name' parameter takes value as category slug. So you should use category slug in place of category name in the query.
$cat_slug = $category->slug;
$the_query = new WP_Query(array(
'post_type' => 'acme_product',
'posts_per_page' => 100,
'category_name' => $cat_slug
));
i asked for this problem in previous time but after some attempt for solving my problem i solved it by this perfect cod that work 100%100 with me
<?php
echo '<ul class="nav nav-tabs" role="tablist">';
$args = array(
'hide_empty'=> 1,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo
'<li>
<a href="#'.$category->slug.'" role="tab" data-toggle="tab">
'.$category->name.'
</a>
</li>';
}
echo '</ul>';
echo '<div class="tab-content">';
foreach($categories as $category) {
echo '<div class="tab-pane" id="' . $category->slug.'">';
$the_query = new WP_Query(array(
'post_type' => 'acme_product',
'posts_per_page' => 100,
'category_name' => $category->slug
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<h1>';
the_title();
echo '</h1>';
endwhile;
}
echo '</div>';
?>

Categories