I have added a custom post type dropdown to my product categories (see below):
$args = array(
'posts_per_page' => -1,
'post_type' => 'banner',
);
$stickers = get_posts( $args );
echo '<label for="product-banner">Productbanner</label>';
echo '<select name="product-banner" class="product-banner">';
echo '<option value="">Geen banner</option>';
foreach($stickers as $sticker){
echo '<option value="'.$sticker->ID.'">'.get_the_title($sticker->ID).'</option>';
}
echo '</select>';
And to save it:
if ( isset( $_POST['product-banner'] ) ) {
$new_meta_value = $_POST['product-banner'];
$meta_key = 'product-banner';
update_term_meta( $term_id, $meta_key, $new_meta_value );
}
I am showing the sidebar on category pages, and i would like to show the featured image of my selected custom post type dropdown item in there as well.
Is there an easy solution to do this?
Thanks in advance.
EDIT :
$tax_id = get_queried_object()->term_id;
if (get_term_meta($tax_id, 'product-banner', true)) {
$banner_id = get_term_meta($tax_id, 'product-banner', true);
//echo $banner_id;
$banner_post = get_post($banner_id);
$banner_img = get_post_thumbnail_id($banner_post);
echo '<img src="'.wp_get_attachment_image_url( $banner_img, 'full' ).'"style="margin-bottom:-17px;border:1px solid #e8e8e8;" />';
//echo get_queried_object()->term_id;
}
Related
On the author archive page I want to show every taxonomy (in my case product category) in which the author has posts (in my case WooCommerce products).
To do so, I'm using the following code:
$posts = get_posts( array('post_type' => 'product', 'posts_per_page' => -1, 'author' => $author_id) );
$author_categories = array();
//loop over the posts and collect the terms
foreach ($posts as $p) {
$author_categories = wp_get_object_terms( $p->ID, 'product_cat');
if ( ! empty( $author_categories ) && ! is_wp_error( $author_categories ) ){
echo '<div class="d-flex flex-wrap">';
foreach ($author_categories as $author_category) {
//var_dump($t);
$author_category_link = get_term_link( $author_category );
$author_category_name = $author_categories[] = $author_category->name;
echo '<a href="'.esc_url( $author_category_link ).'" class="p-2 p-lg-5 bg-light text-center">';
echo $author_category_name;
echo '</a>';
}
echo '</div>';
}
}
wp_reset_postdata();
The problem is, that the product category appears multiple times. I guess for every post in that product category.
Is there any way to collect the terms first and display them only once ordered by the count of posts in the product category?
You should first collect all categories in a container array. By using the ID or the name of the category as the array key when adding categories to the array, you eliminate duplicates. Then you just loop over the resulting array to echo the categories.
// First just collect the distinct categories.
$posts = get_posts( array('post_type' => 'product', 'posts_per_page' => -1, 'author' => $author_id) );
$all_author_categories = array();
foreach ($posts as $p) {
$author_categories_for_p = wp_get_object_terms( $p->ID, 'product_cat');
if ( ! empty( $author_categories_for_p ) && ! is_wp_error( $author_categories_for_p ) ){
foreach ($author_categories_for_p as $author_category) {
$all_author_categories[$author_category->name] = $author_category;
}
}
}
// Then just loop over the collected categories and display them.
echo '<div class="d-flex flex-wrap">';
foreach($all_author_categories as $author_category) {
$author_category_link = get_term_link( $author_category );
$author_category_name = $author_categories[] = $author_category->name;
echo '<a href="'.esc_url( $author_category_link ).'" class="p-2 p-lg-5 bg-light text-center">';
echo $author_category_name;
echo '</a>';
}
echo '</div>';
Remarks:
This code does not scale well when you have a lot of posts. You should use a custom query instead.
I am pretty sure the wp_reset_postdata(); is not needed at the end, because you are not altering the global $post object.
<?php
$orderby = 'id';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty'=> 0,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '
<ul class="display-categories list-group nav navbar-nav">';
foreach ($product_categories as $key => $category) {
echo '
<li class="list-group-item">';
echo '<a href="'.get_term_link($category).'" >';
echo '<img src="'.get_term_link($category).'" />' ;
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
?>
I can't fetch the product category icons.. i want it to fetch category icons with the list of names ... please somebody help me out with this thanks
Category Icons / Images has been stored in terms meta and can be retrieved by using
$term_meta = get_woocommerce_term_meta( $category_id, 'thumbnail_id', true );
$category_icon = wp_get_attachment_url( $term_meta );
I'm making an e-commerce website with woo commerce and I have this weird issue that I cannot find a solution for.
When I log out of admin or view the page through the customizer in wordpress the categories on the home page suddenly disappear however when I log back in, they are there again.
I am using this piece of code to display the categories on the homepage.
<?php
$orderby = 'name';
$order = 'asc';
$hide_empty = true;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
foreach ($product_categories as $key => $category) {
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<a style="background-image: url(' . $image . ')" class="category-link" href="'.get_term_link( $category ).'" >';
echo '<div class="layer">';
echo '<article class="category">';
echo '<h3>' . $category->name . '</h3>';
echo '<span>Shop ' . $category->name . ' Now</span>';
echo '</article>';
echo '</div>';
echo '</a>';
}
} ?>
Any help is appreciated!
I want to get all the product data in the WooCommerce (product sku, name, price, stock count, availability and etc.) Can I do that using wp-query?
This way you can get all products via wp_query :
global $wpdb;
$all_product_data = $wpdb->get_results("SELECT ID,post_title,post_content,post_author,post_date_gmt FROM `" . $wpdb->prefix . "posts` where post_type='product' and post_status = 'publish'");
And if you want further product data then it will be like this :
$product = wc_get_product($product_id);
$product->product_type;
get_post_meta($prodyct_id, '_regular_price');
$product->get_available_variations();
Thanks for all reply.I have resolve that like billows
$full_product_list = array();
$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1));
while ($loop->have_posts()) : $loop->the_post();
$theid = get_the_ID();
$product = new WC_Product($theid);
if (get_post_type() == 'product_variation') {
// ****************** end error checking *****************
} else {
$sku = get_post_meta($theid, '_sku', true);
$selling_price = get_post_meta($theid, '_sale_price', true);
$regular_price = get_post_meta($theid, '_regular_price', true);
$description=get_the_content();
$thetitle = get_the_title();
}
// add product to array but don't add the parent of product variations
if (!empty($sku))
$full_product_list[] = array("PartyID" => (int) $party_id,"Description"=> $description,
"ExternalNumber" => $theid, "ProductName" => $thetitle, "sku" => $sku,
"RegularPrice" => $regular_price, "SellingPrice" => $selling_price,
"ExternalProductCategoryId" => $cat_id, "ExternalProductCategoryName" => $cat_name);
endwhile;
When your building out your query set the post type to be product
$query->set('post_type', 'product');
And that will only search through woocommerce products.
Also if you are creating a theme, you can take any file from the /wp-content/plugins/woocommerce/templates/ directory and put it inside of /wp-content/themes/<yourTheme>/woocommerce to override any woocommerce page.
you can write this code in page that you want to display all product information
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
global $product;
//echo $product;
echo $product->get_id();
echo $product->get_name();
echo $product->get_sale_price();
echo $product->get_regular_price();
echo $product->get_sku();
echo $product->get_low_stock_amount();
echo $product->get_review_count();
echo $product->get_short_description();
$ratting = $product->get_rating_counts();
for($i = 0 ; $i < count($ratting); $i++) {
echo $ratting[i];
}
endwhile;
wp_reset_query();
?>
This is an old question; the answers using WP_Query() are obsolete since 2018.
See my answer here.
The problem is that I cannot display the category of a post:
http://screencast.com/t/hdQjpSV0Q
I have the following code in a function.php file:
// GET FEATURED IMAGE
function ST4_get_featured_image($post_ID) {
$custom_meta = get_post_custom(get_the_ID());
return $custom_meta["lumen_portfolio_preview_image_image"][0];
}
add_filter('manage_posts_columns', 'ST4_columns_head');
add_action('manage_posts_custom_column', ('ST4_columns_content'), 10, 2);
// ADD NEW COLUMN
function ST4_columns_head($defaults) {
$defaults['featured_image'] = 'Featured Image';
$defaults['categories_portfolio'] = 'Category';
return $defaults;
}
// SHOW THE FEATURED IMAGE
function ST4_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = ST4_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img style="width:300px;height:200px;" src="' . $post_featured_image . '" />';
}
}
elseif ($column_name == 'categories_portfolio') {
$terms = get_the_terms( $post->ID , 'category' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '' . $term->name . '';
}
}
}
I have empty results, NULL, but I want to have the post category field. I used many wordpress functions, but no success.
I've noticed that your using the $post->ID as your parameter in your get_the_terms function, anyways I assume that the problem is you can't retrieve the category of the post, I would recommend using the get_the_category function in place of the get_the_terms in your code:
// SHOW THE FEATURED IMAGE
function ST4_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = ST4_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img style="width:300px;height:200px;" src="' . $post_featured_image . '" />';
}
}
elseif ($column_name == 'categories_portfolio') {
//replace this $terms = get_the_terms( $post->ID , 'category' );
$terms = get_the_category($post_ID); //should you be using this instead of $post->ID?
foreach ( $terms as $term ) {
//replace this $term_link = get_term_link( $term, 'category' );
$term_link = get_category_link($term->term_id);
if( is_wp_error( $term_link ) )
continue;
echo '' . $term->name . '';
}
}
}
You can do a var_dump($terms) to further see the values being returned by this function, hope it helps, cheers!
I think you are using custom post type "Portfolio" as shown in attached screenshot.
Use wp_get_post_terms rather than get_the_terms
Update following code:
$terms = get_the_terms( $post->ID , 'category' );
to
$terms = wp_get_post_terms( $post->ID , 'category', array("fields" => "all") );
Note that 2nd parameter is the taxonomy name you are using while registering taxonomy.
See an example below where 'testimonials-cat' is used to register taxonomy.
register_taxonomy('testimonials-cat', 'testimonials', $args);
Your code for above taxonomy will be:
$terms = wp_get_post_terms($post->id, 'testimonials-cat', array("fields" => "all"));