I am trying to get print names of custom taxonomy that i have created for products.
function create_product_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x('product_categories', 'taxonomy general name'),
'singular_name' => _x('Product', 'taxonomy singular name'),
'search_items' => __('Search Product Category'),
'all_items' => __('All Product Categorie(s)'),
'parent_item' => __('Parent Product Category'),
'parent_item_colon' => __('Parent Product Category:'),
'edit_item' => __('Edit Product Category'),
'update_item' => __('Update Product Category'),
'add_new_item' => __('Add New'),
'new_item_name' => __('New Product Name'),
'menu_name' => __('Product Categories'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'product_categories', 'with_front' => true));
register_taxonomy('product_categories', array('products'), $args);
i have added data through the wordpress admin panel. Now i want to Display the names of the categories in product.php file.
function getLatestProducts()
{
$args = array(
'post_status' => 'publish',
'post_type' => 'products',
'posts_per_page' => 12,
'order' => 'ASC'
);
$result = '<div class="col-sm-3">';
$loop = new WP_Query($args);
$i=0;
while ($loop->have_posts())
{
$loop->the_post();
$clink=get_permalink($post->ID);
$desc=get_the_excerpt();
$categories = get_terms( 'product_categories');
$desc = strip_tags(str_replace(array("<p>", "</p>"), "", $desc));
$the_imgurl = get_post_custom_values('_cus_n__image');
$theimage=$the_imgurl[0];
$the_locurl = get_post_custom_values('_cus_n__location');
$theloc=$the_locurl[0];
echo $categories;
$result .='<div class="product-warp">';
$result .='<div class="product"> <img src="/wp-content/themes/cake/images/pro1.jpg" title="" alt=""> </div>';
$result .='<div class="product-name">';
$result .='<h5>'.$categories.'</h5>';
$result .='</div>';
$result .='</div>';
$i++;
}
$result .= '</div>';
if($i > 0){
return $result;
} else {
return "";
}
}
it is just printing this arrayarrayarrayarrayarrayarray
Ok bro you can use get_terms function for this purpose. Here is the example:
First Part
<?php
$args = array(
'orderby' => 'name'
);
$terms = get_terms('product_categories', $args);
foreach($terms as $term) {
?>
<a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
<?php echo $term->name; ?>
</a>
<?php
}
?>
I only give you an example. You can paste my code where you want.
Second Part
Now use the WordPress Taxonomy Template for that when user click on one of your category and next page show all the related products of clicked category and also you must read this.
If you read taxonomy Template link then we go to next step.
Now you create a file taxonomy-product_categories.php in your theme root folder.
This create template for you taxonomy.
Now in this file here is the complete code:
<?php
get_header();
$slug = get_queried_object()->slug; // get clicked category slug
$name = get_queried_object()->name; // get clicked category name
$tax_post_args = array(
'post_type' => 'products', // your post type
'posts_per_page' => 999,
'orderby' => 'id',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_categories', // your taxonomy
'field' => 'slug',
'terms' => $slug
)
)
);
$tax_post_qry = new WP_Query($tax_post_args);
if($tax_post_qry->have_posts()) :
while($tax_post_qry->have_posts()) :
$tax_post_qry->the_post();
the_title();
the_content();
endwhile;
endif;
get_footer();
?>
Once again I told you that I give you only a code you can merge this code in your theme.
Hope this'll help you.
Related
I have created a custom post type in functions.php with this code.
function create_recipes() {
register_post_type('recipe', [
'public' => true,
'show_in_rest' => true,
'labels' => [
'name' => 'Recipes',
'add_new_item' => 'Add New Recipe',
'edit_item' => 'Edit Recipe',
'all_items' => 'All Recipes',
'singular_name' => 'Recipe',
],
'supports' => ['title', 'editor'],
'rewrite' => ['slug' => 'recipes'],
'menu_icon' => 'dashicons-media-archive',
'has_archive' => true,
'taxonomies' => array('category'),
'supports' => array( 'title', 'editor', 'author', 'thumbnail' ),
]);
}
add_action('init', 'create_recipes');
And now I am trying to get/show all the posts on my frontend that I've created that have different categories here
<?php
$recipes = new WP_Query(['post_type' => 'recipe', 'category' => '01']);
while ($recipes->have_posts()):
$recipes->the_post();
?>
<div class="sub-column">
<div class="sub-cat">
<?php the_category(); ?>
</div>
<a>
<div class="sub-thumbnail">
<?php echo the_post_thumbnail(); ?>
</div>
</a>
<div class="sub-title">
<h4><?php the_title(); ?></h4>
</div>
</div>
<?php endwhile;
?>
But I cant get it to work. Now I get all the different categories which is good but the posts that have the same category should be printent directly after and not with the same category name above.
You need to use the "get_cat_name( $category_id )" rather then "the_category()"
So in the function get_cat_name(), you need to pass the same category id which you are passing in the wp_query.
Here is your code and if you are wishing to show all the posts then you do not require to pass any category all you need to pass is the post_per_page
// WP_Query arguments
$args = array(
'post_type' => array( 'recipes' ),
'post_status' => array( 'publish' ),
'posts_per_page' => '-1',
'order' => 'DESC',
'orderby' => 'id',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
echo get_the_title();
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
If you wish to show the specific post of the respective category then you can use slug, term_id, etc and you need to inject the tax_query in your argument here is the sample code based on slug
// WP_Query arguments
$args = array(
'post_type' => array( 'recipes' ),
'post_status' => array( 'publish' ),
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'category', // taxonomy slug
'field' => 'slug', //do not change this if you wisht to fetch from slug
'terms' => 'bob' //slug name
)
)
'order' => 'DESC',
'orderby' => 'id',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
echo get_the_title();
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
i have created custom post type for testimonials in wordpress.i have added 89 testimonials and wants to display 2 which i wants to displany in home page .
so wanted to created shortcode which will display testimonials according to their Post ID.
can anyone please tell me the code for shortcode.
Below i am showing code i had written to create custom post type for testimonial . Pls tell me the code to create shortcode like this:-[testimonial posts_per_page="5" testimonial_id="123,145"]
function custom_post_testimonial_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name'=> _x( 'Testimonials', 'Post Type General Name', 'walker_theme' ),
'singular_name'=> _x( 'Testimonial', 'Post Type Singular Name', 'walker_theme' ),
'menu_name'=> __( 'Testimonials', 'walker_theme' ),
'parent_item_colon' => __( 'Testimonial', 'walker_theme' ),
'all_items' => __( 'All Testimonials', 'walker_theme' ),
'view_item' => __( 'View Testimonial', 'walker_theme' ),
'add_new_item' => __( 'Add New Testimonial','walker_theme' ),
'add_new' => __( 'Add New', 'walker_theme' ),
'edit_item' => __( 'Edit Testimonial','walker_theme' ),
'update_item' => __( 'Update Testimonial','walker_theme' ),
'search_items' => __( 'Search Testimonial', 'walker_theme' ),
'not_found' => __( 'Not Found', 'walker_theme' ),
'not_found_in_trash' => __( 'Not found in Trash','walker_theme' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'testimonials', 'walker_theme' ),
'description' => __( 'Home page testimonials', 'walker_theme' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'author','thumbnail', 'tags'),
// You can associate this CPT with a taxonomy or custom
taxonomy.
'taxonomies' => array( 'genres', 'post_tag' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type( 'testimonials', $args );
}
add_action( 'init', 'custom_post_testimonial_type', 0 );
Use the shortcode attributes with known attributes and fills in defaults when needed.
function testimonials($atts) {
$a = shortcode_atts( array(
'posts_per_page' => '',
'testimonial_id' => ''
), $atts );
$testimonials = '';
$post_in = esc_attr($a['testimonial_id']);
$posts_per_page = esc_attr($a['posts_per_page']);
$post_artay = explode(',', $post_in);
$args = array(
'post__in' => $post_artay,
'posts_per_page' => $posts_per_page,
'post_type' => 'testimonials',
'order_by' => 'post__in',
);
// the query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$testimonials.= '<div class="title">'.get_the_title().'</div>';
$testimonials.= '<div class="content">'.get_the_content().'</div>';
$testimonials.='<div class="date">'.get_the_date().'</div>';
$testimonials.='<div class="author">'.get_the_author().'</div>';
endwhile;
endif;
return $testimonials;
}
add_shortcode('testimonial', 'testimonials' );
use the shortcode like.
[testimonial posts_per_page="5" testimonial_id="29,23"]
Hi let's add this code in the theme's functions.php file.
add_shortcode( 'testimonial', 'testimonial_shortcode_callback' );
function testimonial_shortcode_callback( $atts ) {
ob_start();
extract( shortcode_atts( array(
'posts_per_page' => 5,
'testimonial_id' => '',
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => 'testimonials',
'posts_per_page' => $posts_per_page,
);
if ( ! empty( $testimonial_id ) ) {
$options['post__in'] = array_map( 'trim', explode( ',', $testimonial_id ) );
}
$testimonial_query = new WP_Query( $options );
// run the loop based on the query
if ( $testimonial_query->have_posts() ) :
?>
<ul class="testimonial-listing">
<?php
while ( $testimonial_query->have_posts() ) : $testimonial_query->the_post();
?>
<li id="testimonial-<?php the_ID(); ?>">
<?php the_content(); ?>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
<?php
$testimonial_output = ob_get_clean();
return $testimonial_output;
endif;
}
Than use this shortcode as a example [testimonial posts_per_page="2" testimonial_id="123,145"]
I am trying to create my own theme in WordPress. I converted the PSD file to HTML and now I want to make it dynamic with WordPress, I used the mix it up plugin as you know it's not a WordPress plugin so I tried to make it dynamic myself, I used this tutorial which provides codes and video for this issue.
as you can see in the pic, it must show my category but show 'array' and I cant find why?
here is my code
first I create post type named book with taxonomy named books_category
function p2p2_register_book(){
$labels = array(
'name' => _x( 'کتابخانه', 'books'),
'singular_name' => _x( 'کتاب', 'book' ),
'add_new' => _x( 'افزودن کتاب', '' ),
'add_new_item' => __( 'افزودن کتاب جدید' ),
'edit_item' => __( 'ویرایش کتاب' ),
'new_item' => __( 'کتاب جدید' ),
'all_items' => __( 'همه کتاب ها' ),
'search_items' => __( 'جست و جو کتاب' ),
'not_found' => _( 'کتاب یافت نشد' ),
'not_found_in_trash' => __( 'کتاب در زباله دان یافت نشد' ),
'parent_item_colon' => '',
'menu_name' => 'کتابخانه'
);
$args=array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'menu_position' => 2,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'page',
'has_archive' => true,
// 'hierarchical' => false,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ,'custom-fields'),
// 'taxonomies' => array( 'Books_category' ),
);
register_post_type('Book',$args);
}
add_action('init', 'p2p2_register_book');
// ============================== Adding post toxonomy to library post ================
function wp_library_post_taxonomy() {
register_taxonomy(
'books_category', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'book', //post type name
array(
'hierarchical' => true,
'label' => 'book Category', //Display name
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'books_category', // This controls the base slug that will display before each term
'with_front' => true // Don't display the category base before
)
)
);
}
add_action( 'init', 'wp_library_post_taxonomy');
include_once( 'mixitup-plug.php' );
add_theme_support('post_thumbnails');
add_image_size('Books_small_image',150,200,true);
then as the tutorial said I added this code for make it dynamic
<?php
function books_shortcode($atts){
extract( shortcode_atts( array(
'category' => ''
), $atts, '' ));
$q= new WP_Query(
array('posts_per_page'=> 30, 'post_type' => 'Book')
);
//library taxonomy query
global $paged;
global $post;
$args = array(
'post_type' => 'Book',
'paged' => $paged,
' posts_per_page' => -1,
);
$Books = new WP_Query($args);
if(is_array($Books->posts) && !empty($Books->posts)) {
foreach($Books->posts as $Books_post) {
$Books_taxs = wp_get_post_terms($Books_post->ID, 'Books_category', array("fields" => "all"));
if(is_array($Books_taxs) && !empty($Books_taxs)) {
foreach($Books_taxs as $Books_tax) {
$Books_taxs[$Books_tax->slug] = $Books_tax->name;
}
}
}
}
?>
<!--Category Filter-->
<div class="filter-book">
<ul>
<li class="filter active" data-filter="all">همه</li>
<?php foreach($Books_taxs as $Books_tax_slug => $Books_tax_name): ?>
<li class="filter" data-filter=".<?php echo $Books_tax_slug; ?>"><?php echo $Books_tax_name; ?></li>
<?php endforeach; ?>
</ul></div>
<!--End-->
<?php
$list = '<div class="library-item">';
while($q->have_posts()) : $q->the_post();
$idd = get_the_ID();
$small_image_url=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'Books_small_image');
//Get Texanmy class
$item_classes = '';
$item_cats = get_the_terms($post->ID, 'Books_category');
if($item_cats):
foreach($item_cats as $item_cat) {
$item_classes .= $item_cat->slug . ' ';
}
endif;
$single_link =
$list .= '
<div class="mix single-library cycology '.$item_classes.'">
<div>
<img src="'.$small_image_url[0].'">
<p class="text-center">کتاب جیبی موفقیت یک</p>
</div>
</div>
<div class="mix '.$item_classes.'" >'.get_the_content().'</div>
';
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}
add_shortcode('Books', 'Books_shortcode');
?>
it must be some problem with this part of my code because this part show category
<div class="filter-book">
<ul>
<li class="filter active" data-filter="all">همه</li>
<?php foreach($Books_taxs as $Books_tax_slug => $Books_tax_name): ?>
<li class="filter" data-filter=".<?php echo $Books_tax_slug; ?>"><?php echo $Books_tax_name; ?></li>
<?php endforeach; ?>
</ul></div>
Please check you have used echo array function
<li class="filter" data-filter=".<?php echo $Books_tax_slug; ?>"><?php echo $Books_tax_name; ?></li>
You can check it
<?php
# Dynamic Portfolio With Shortcode
function portfolio_shortcode($atts){
extract( shortcode_atts( array(
'category' => ''
), $atts, '' ) );
$q = new WP_Query(
array('posts_per_page' => 50, 'post_type' => 'portfolios')
);
//Portfolio taxanomy query
global $paged;
global $post;
$args = array(
'post_type' => 'portfolios',
'paged' => $paged,
'posts_per_page' => -1,
);
$portfolio = new WP_Query($args);
if(is_array($portfolio->posts) && !empty($portfolio->posts)) {
foreach($portfolio->posts as $gallery_post) {
$post_taxs = wp_get_post_terms($gallery_post->ID, 'portfolio_category', array("fields" => "all"));
if(is_array($post_taxs) && !empty($post_taxs)) {
foreach($post_taxs as $post_tax) {
$portfolio_taxs[$post_tax->slug] = $post_tax->name;
}
}
}
}
?>
<!--Category Filter-->
<div class="portfolio_button_area fix">
<button class="filter portfolio_button active" data-filter="all">Show All</button>
<?php foreach($portfolio_taxs as $portfolio_tax_slug => $portfolio_tax_name): ?>
<button class="filter portfolio_button" data-filter=".<?php echo $portfolio_tax_slug; ?>"><?php echo $portfolio_tax_name; ?></button>
<?php endforeach; ?>
</div>
<!--End-->
<?php
$list = '<div id="Container">';
while($q->have_posts()) : $q->the_post();
$idd = get_the_ID();
//Get Texanmy class
$item_classes = '';
$item_cats = get_the_terms($post->ID, 'portfolio_category');
if($item_cats):
foreach($item_cats as $item_cat) {
$item_classes .= $item_cat->slug . ' ';
}
endif;
$single_link =
$list .= '
<div class="mix '.$item_classes.'" >'.get_the_content().'</div>
';
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}
add_shortcode('portfolio', 'portfolio_shortcode');
?>
code reference and more http://www.wp-tutorials.com/how-to-dynamic-mixitup-or-isotope-in-wordpress-step-by-step
I find the answer it's word problem. I use Book but call book so it didn't work.
I'm trying to display a taxonomy description based on which taxonomy the page is set to display (set with a variable)
As it is, I'm only seeing the first description, regardless of which category variable is set.
Here's how I've set it up:
I've built a custom post type called 'product' and set up a taxonomy called'product_type'.
//Register product post type
add_action('init', 'product_register');
function product_register() {
$labels = array(
'name' => ('Products'),
'singular_name' => ('Product'),
'add_new' => ('Add New'),
'add_new_item' => ('Add New Product'),
'edit_item' => ('Edit Product'),
'new_item' => ('New Product'),
'view_item' => ('View Product'),
'search_items' => ('Search'),
'not_found' => ('Nothing found'),
'not_found_in_trash' => ('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'menu_icon' => 'dashicons-tag',
'public' => true,
'has_archive' => true,
'supports' => array('title', 'revisions', 'editor','thumbnail'),
'capability_type' => 'post',
'rewrite' => array("slug" => "product"), // Permalinks format
);
register_taxonomy('product_type', array('product'), array(
'hierarchical' => true,
'label' => 'Product Type',
'singular_label' => 'Type',
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => true)
);
register_post_type( 'product' , $args );
}
In the wordpress back end, I've loaded this with a few terms and have also added in descriptions.
archive-products.php works like a landing page, but will accept information from a variable called $type to filter the content.
if (isset($_GET['type']) || !empty($_GET['type']) ) {
$filtered = true;
$type = $_GET['type'];
}
With that information, I can set up a WP_Query for items that contain that taxonomy term.
if ($filtered == true) {
$type = explode(' ', $_GET['type']);
$taxonomy = array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => $type
)
);
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'DEC',
'orderby' => 'title',
'offset' => '0',
'tax_query' => $taxonomy
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
$terms = get_the_terms( $post->ID, 'product_type' );
if($terms) {
foreach( $terms as $term ) {
echo $term->description; //always shows first term description, but should display description based on $type
}
}
while($the_query->have_posts()):$the_query->the_post();
the_title();
the_content();
endwhile;
else: //nothing to show here
endif;} else {
// non-filtered page design
}
The query works great but, as I mentioned earlier, it's only pulling the first taxonomy description.
This is the part that is meant to handle that:
$terms = get_the_terms( $post->ID, 'product_type' );
if($terms) {
foreach( $terms as $term ) {
echo $term->description;
}
}
I had guessed that it needed to be inside the query to land on the correct description, but it doesn't seem to make a difference.
I have also tried this:
$term = get_term_by( $post->ID, $type, 'product_type' );
echo $term->description;
but that doesn't return anything.
I'm out of ideas. I haven't found anything via google or by searching this site.
Any advice is appreciated, thanks in advance.
Just fixed it.... the problem was in
register_taxonomy_for_object_type( 'tags', 'produto' );
was registering tags instead of categories.... fixed with:
<?php
$tag = 'taeq';
$args = array('post_type' => 'produto', 'posts_per_page' => -1, 'produto_category' => $tag);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
<li>
<img src="<?php the_field('produto_img'); ?>" alt="<?php the_title(); ?>" />
<span><?php the_title(); ?></span>
<span><?php the_field("produto_desc"); ?></span>
<i class="border"></i>
</li>
<?php endwhile; ?>
The correct question was how to Loop specific tag of a custom post type in wordpress
I'm trying to loop posts from only one category on wordpress.
I don't know nothing about PHP...
Here is the code I have, working, but displaying all the products
<?php
$new_query = new WP_Query('post_type=produto&post_per_page=-1');
while($new_query -> have_posts()) : $new_query -> the_post();
?>
<li>
<img src="<?php the_field("produto_img"); ?>" alt="<?php the_title(); ?>" />
<span><?php the_title(); ?></span>
<span><?php the_field("produto_desc"); ?></span>
<i class="border"></i>
</li>
<?php endwhile; ?>
I need show items from category ID 2.
what should I do?
OBS: My site is a singlepage website.
I'm displaying all the posts types in differents places of the same page.
need filter some by category.
functions php:
add_action( 'init', 'create_post_type_produto' );
function create_post_type_produto() {
$labels = array(
'name' => _x('Produtos', 'post type general name'),
'singular_name' => _x('Produtos', 'post type singular name'),
'add_new' => _x('Adicionar novo', 'produto'),
'add_new_item' => __('Adicionar novo produto'),
'edit_item' => __('Editar produto'),
'new_item' => __('Novo produto'),
'all_items' => __('Todos os produtos'),
'view_item' => __('Ver produtos'),
'search_items' => __('Procurar produtos'),
'not_found' => __('Nenhum produto encontrado'),
'not_found_in_trash' => __('Nenhum produto encontrado na lixeira.'),
'parent_item_colon' => '',
'menu_name' => 'Produtos'
);
register_post_type( 'produto', array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'has_archive' => 'produtos',
'rewrite' => array(
'slug' => 'produtos',
'with_front' => false,
),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title')
)
);
register_taxonomy( 'produto_category', array( 'produto' ), array(
'hierarchical' => true,
'label' => __( 'Categoria do produto' ),
'labels' => array( // Labels customizadas
'name' => _x( 'Categorias', 'taxonomy general name' ),
'singular_name' => _x( 'Categoria', 'taxonomy singular name' ),
'search_items' => __( 'Procurar categorias' ),
'all_items' => __( 'Todas categorias' ),
'parent_item' => __( 'Categoria pai' ),
'parent_item_colon' => __( 'Categoria pai:' ),
'edit_item' => __( 'Editar categoria' ),
'update_item' => __( 'Atualizar categoria' ),
'add_new_item' => __( 'Adicionar nova categoria' ),
'new_item_name' => __( 'Nome da nova categoria' ),
'menu_name' => __( 'Categoria' ),
),
'show_ui' => true,
'show_in_tag_cloud' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'produtos/categorias',
'with_front' => false,
),)
);
register_taxonomy_for_object_type( 'tags', 'produto' );
}
Try this using tax query for custom taxonomies filter in wp query
// using category slug
$args = array(
'post_type' => 'produto',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'produto_category',
'field' => 'slug', // term_id, slug
'terms' => 'taeq',
),
)
);
// using category id
/* $args = array(
'post_type' => 'produto',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'produto_category',
'field' => 'term_id', // term_id, slug
'terms' => 5,
),
)
);
*/
$loop = new WP_Query($args);
Wp query more tax reference
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
You can use the category parameter in the WP_Query. So you can change your WP_Query to:
WP_Query('post_type=produto&post_per_page=-1&cat=4');
Where cat=4 is the category id.
You can see other ways to define it here http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
By using the cat (or product_cat for products) element (also use $args array for clarity) example:
$cat = 2; // The product category you want to display
$args = array('post_type' => 'produto', 'posts_per_page' => -1, 'cat' => $cat);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
<li>
<img src="<?php the_field('produto_img'); ?>" alt="<?php the_title(); ?>" />
<span><?php the_title(); ?></span>
<span><?php the_field("produto_desc"); ?></span>
<i class="border"></i>
</li>
<?php endwhile; ?>
i use custom taxonomy filter like this
Event is my custom post type and featured-events is event category slug...
it works perfect for me, hope it helps :)
$loop = new WP_Query( array(
'post_type' => 'event','tax_query' => array(
array(
'taxonomy' => 'event-categories',
'field' => 'slug',
'terms' => 'featured-events',
)
)) );
You are using custom taxonomy so you cannot use the argument for category, you should use Taxonomy querying https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
So your code should be
<?php
$args = array(
'post_type' => 'produto',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'produto_category',
'field' => 'slug', // search by slug name, you may change to use ID
'terms' => 'taeq', // value of the slug for taxonomy, in term using ID, you should using integer type casting (int) $value
),
)
);
$new_query = new WP_Query($args);
while($new_query -> have_posts()) : $new_query -> the_post();
?>
<li>
<img src="<?php the_field("produto_img"); ?>" alt="<?php the_title(); ?>" />
<span><?php the_title(); ?></span>
<span><?php the_field("produto_desc"); ?></span>
<i class="border"></i>
</li>
<?php endwhile; ?>
if you are trying to show only one category on category template u can use
this code
query_posts('cat=6');// while 6 is category id you want to show
before this code in your wp template
if ( have_posts() ) :
while ( have_posts() ) : the_post();