【问题标题】:Loop specific category of a custom post type in wordpress在 wordpress 中循环自定义帖子类型的特定类别
【发布时间】:2014-07-21 13:22:37
【问题描述】:

刚刚修好了....问题出在

    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' );
}

【问题讨论】:

  • 聘请懂 PHP 和 Wordpress 的人。
  • 使用$new_query = new WP_Query('post_type=produto&amp;post_per_page=-1&amp;cat=2');
  • 它只是没有用......也许是分类法

标签: php wordpress


【解决方案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

【讨论】:

    【解决方案2】:

    您可以在 WP_Query 中使用类别参数。因此,您可以将 WP_Query 更改为:

    WP_Query('post_type=produto&post_per_page=-1&cat=4');
    

    其中 cat=4 是类别 ID。

    您可以在此处查看其他定义方式http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    【讨论】:

      【解决方案3】:

      通过使用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; ?>
      

      【讨论】:

      • @user3797666 你在使用 WooCommerce 插件吗?你有producto,不应该是product吗?
      • 产品使用 ptBR 语言。也许有分类学的东西?
      • @user3797666 你在使用 WooCommerce 吗?
      • 取出cat并在循环中添加这个html:&lt;p&gt;Categories: &lt;?php the_category(' '); ?&gt;&lt;/p&gt;以查看产品是否分类。
      • 但我在 wp-admin 中创建了类别并与产品相关联...可能是因为产品是自定义帖子类型?
      【解决方案4】:

      我使用这样的自定义分类过滤器

      Event 是我的自定义帖子类型,Featured-events 是事件类别 slug...

      它非常适合我,希望对我有帮助:)

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

      【讨论】:

        【解决方案5】:

        您正在使用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; ?>
        

        【讨论】:

          【解决方案6】:

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

          这段代码

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

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

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

          【讨论】:

          • 投反对票!你是 wordpress 的新手,如果我能教你一件事,你应该从不使用query_postsn,我的意思是从不。使用WP_Querypre_get_posts
          • 那怎么办,因为上次我开发代码时它正在工作
          • query_posts 会破坏主查询,并且在大多数情况下分页失败。甚至法典状态为不使用 query_posts。看其他两个答案如何使用WP_Query
          • 那么如果我是对的,我们可以使用 wp_reset_query();以 query_posts() 结尾
          • 不要使用query_posts。期间
          猜你喜欢
          • 2015-11-09
          • 2021-04-19
          • 2011-06-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多