简体   繁体   English

如何在 Wordpress 中按类别过滤 archive-{custom-post-type}.php?

[英]How to filter archive-{custom-post-type}.php by category in Wordpress?

I created a custom post type called, "Portfolio," using a plugin called Custom Post Type UI v0.7.1 .我使用名为Custom Post Type UI v0.7.1的插件创建了一个名为“Portfolio”的自定义帖子类型 I have created several categories for it, such as Logos, Packaging, etc.我为它创建了几个类别,例如徽标、包装等。

I need to use archive.php to filter by category.我需要使用 archive.php 按类别过滤。

Right now I have an archive-portfolio.php that includes this code:现在我有一个包含以下代码的archive-portfolio.php

<?php $wp_query = null; $wp_query = $temp;?>
<?php $temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query(); ?>
<?php $wp_query->query("post_type=portfolio&". $catinclude ."&paged=".$paged.'&showposts=20'); ?>

<ul>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
        echo '<li><a href="'; the_permalink(); echo '">';
        echo '<strong>'; the_title();
        echo '</strong>';
        echo '</a></li>';
    ?>
    <?php endwhile; ?>
</ul>

I've tried using URLs like /?category_name=logos and /?cat=logos but none of that has worked;我试过使用像/?category_name=logos/?cat=logos这样的 URL,但没有一个奏效; it just displays all portfolio items regardless of category.它只显示所有投资组合项目,而不考虑类别。

The Portfolio custom post type has " Archive " and " Hierarchical " enabled.投资组合自定义帖子类型启用了“存档”和“分层”。

For built-in taxonomies, it has categories and tags enabled as well.对于内置分类法,它还启用了类别和标签。

Any ideas?有任何想法吗?

I would use query_posts like this below, you can probably also simplify the code:我会像下面这样使用 query_posts,你也可以简化代码:

<?php query_posts( array( 'post_type' => 'portfolio', 'showposts' => 10, 'orderby' => 'date', 'order' => 'desc')); ?>

<ul>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li>
         <a href="<?php the_permalink();?>">
             <strong><?php the_title(); ?></strong>
         </a>
    </li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

You can find the parameters that query_posts accepts in the WordPress Codex .您可以在WordPress Codex 中找到query_posts接受的参数。

I actually ended up getting it to work with this:我实际上最终让它与这个一起工作:

            <?php 
            $wp_query = null; $wp_query = $temp;
            $temp = $wp_query;
            $wp_query= null;
            $wp_query = new WP_Query();
            $wp_query->query("post_type=portfolio&category_name=" . $_GET["category"] . "&". $catinclude ."&paged=".$paged.'&showposts=20'); 
            if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
            ...
            <?php endwhile; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WordPress 自定义帖子类型存档-<post type> .php 不工作 - WordPress custom post type archive-<post type>.php not working 如何在WordPress中实现URL结构,例如“网站名称/存档页面/类别(或自定义分类法/自定义帖子类型)”? - How to achieve URL structure like “website-name/archive-page/category(or custom taxonomy)/custom-post-type” in WordPress? 未扩展的Archive.php显示帖子,而不是存档-{Custom Post Type} - Unexpexted Archive.php show posts instead of archive-{Custom Post Type} 更改wordpress自定义帖子类型的永久链接 - Changing wordpress custom-post-type permalink 如何:Wordpress中自定义帖子类型的自定义类别存档页面 - How to: Custom Category Archive Page for custom post type in wordpress archive- {post_type} .php更改子弹时中断 - archive-{post_type}.php breaking when slug is changed 使用产品类别过滤自定义帖子类型存档 - Filter custom post type archive with product category 您知道如何仅为custom-post-type创建类别吗? - Do you know how to create a category only for the custom-post-type? 无法显示custom-post-type -wordpress帖子的详细信息 - cant show details of a post of custom-post-type -wordpress 如何获取在Wordpress中按类别过滤的自定义帖子类型的永久链接? - How to get permalink of custom post type that filter by category in Wordpress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM