2014-07-21 78 views
0

一个自定义后类型的特定类别只是固定它....问题是循环在WordPress

register_taxonomy_for_object_type('tags', 'produto'); 

被注册标记而不是类别....固定:

<?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; ?> 

正确的问题是如何在WordPress中循环自定义帖子类型的特定标签

我试图从wordpress上的仅一个类别循环发布。

我不知道任何关于PHP ...

这里是我的代码,工作,但显示所有产品

<?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; ?> 

我需要证明从类别ID项目2

我该怎么办?

OBS:我的网站是单页网站。 我在同一页面的不同位置显示所有帖子类型。 需要按类别过滤一些。

功能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'); 
} 
+3

雇用了一些知道PHP和Wordpress的人。 – TJHeuvel

+0

使用'$ new_query = new WP_Query('post_type = produto&post_per_page = -1&cat = 2');' –

+0

请参阅:http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters –

回答

0

定义它通过使用cat(或product_cat产品)元素(也使用$args阵列为清楚起见)例如:

$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; ?> 
+0

尝试过,但产品只是不出现 – user3797666

+0

@ user3797666你使用WooCommerce插件?你有'producto'应该不是'产品'吗? – Kivylius

+0

produto是ptBR语言。也许是分类学的东西? – user3797666

-1

,如果你想在类别模板上只显示一个类别,您可以使用

此代码

query_posts('cat=6');// while 6 is category id you want to show 

之前这段代码在你的wp模板

if (have_posts()) : 
        while (have_posts()) : the_post(); 
+0

Downvote!你是新来的wordpress,如果有一件事我可以教你,你应该**绝不**使用'query_posts'n,我的意思是**从不**。使用'WP_Query'或'pre_get_posts' –

+0

那么怎么能做到这一点,导致它上次工作时,我开发了一个代码 – user290895

+0

'query_posts'打破主查询,并在大多数情况下失败的分页。即使是codex状态**不使用**'query_posts'。看到另外两个答案如何使用'WP_Query' –

1

尝试在WP查询自定义分类过滤该使用税查询

// 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的查询更多的税参考 https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

0

我使用像这样的定制分类过滤器

活动是我的自定义帖子类型,特色活动是活动类别slug ...

它的工作非常适合我,希望它有助于:)

$loop = new WP_Query(array( 
         'post_type' => 'event','tax_query' => array(
                  array(
                  'taxonomy' => 'event-categories', 
                  'field' => 'slug', 
                  'terms' => 'featured-events', 
                  ) 
                ))); 
0

您正在使用custom taxonomy,所以你不能使用类的说法,你应该使用分类查询https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters 所以,你的代码应该是

<?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; ?>