将类别添加到自定义帖子类型并根据类别显示

6 wordpress categories custom-post-type

我正在使用主题名称为flozo的wordpress网站.它有一个名为的自定义帖子类型work.我想根据每个类别在我的模板中显示作品.

这是代码:

<?php
$args = array( 'post_type' => 'work', 'posts_per_page' => 15 );
$loop = new WP_Query( $args );
$count = 0;
echo '<ul>';
while ( $loop->have_posts() ) : $loop->the_post();

    $count++;
    $class = ($count % 3 == 1) ? 'first' : '';

    echo '<li class="'.$class.'">';
    echo '<a href="';
    the_permalink();
    echo '">';
    echo '<div class="overlay" style="background-color:'.ot_get_option( 'main_colour' ).';"></div>';
    the_post_thumbnail('full');
    echo '</a>';

    echo '<br />';

    echo '<h2><a href="';
    the_permalink();
    echo '">';
    the_title();
    echo '</a></h2>';

    echo '<div class="entry-content">';
    echo limit_words(get_the_excerpt(), '30'); 
    echo '..</div>';
    echo '</li>';
endwhile;
echo '</ul>';
?>
Run Code Online (Sandbox Code Playgroud)

我已经添加

 $args = array( 'post_type' => 'work', 'tag_ID' => 15 ,'posts_per_page' => 15 );
Run Code Online (Sandbox Code Playgroud)

15我的类别的ID 在哪里,但它不起作用

我也试过了

<?php
$catquery = new WP_Query( 'cat=15&posts_per_page=3' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul class="last-cat">
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(); ?> <p><?php     the_title(); ?></p><span><?php echo get_the_date(); ?></span></a></li></ul>
<?php endwhile; ?>
Run Code Online (Sandbox Code Playgroud)

这也没有帮助.

编辑:

类别网址是

http://jointviews.com/wp-admin/edit-tags.php?action=edit&taxonomy=categories&tag_ID=15&post_type=work

帖子类型注册码是:

add_action('init', 'work_register');   

function work_register() {   

$labels = array( 
    'name' => _x('Work', 'post type general name'), 
    'singular_name' => _x('Work Item', 'post type singular name'), 
    'add_new' => _x('Add New', 'work item'), 
    'add_new_item' => __('Add New Work Item'), 
    'edit_item' => __('Edit Work Item'), 
    'new_item' => __('New Work Item'), 

    'view_item' => __('View Work Item'), 
    'search_items' => __('Search Work'), 
    'not_found' => __('Nothing found'), 
    'not_found_in_trash' => __('Nothing found in Trash'), 
    'parent_item_colon' => '' 
);   

$args = array( 
    'labels' => $labels, 
    'public' => true, 
    'publicly_queryable' => true, 
    'show_ui' => true, 
    'query_var' => true, 
    'menu_icon' => get_stylesheet_directory_uri() . '/article16.png', 
    'rewrite' => array( 'slug' => 'work', 'with_front'=> false ), 'capability_type' => 'post', 
    'hierarchical' => true, 
    'menu_position' => null, 
    'supports' => array('title','editor','thumbnail') 
);   

register_post_type( 'work' , $args ); 

register_taxonomy("categories", array("work"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => array( 'slug' => 'work', 'with_front'=> false )));



}
Run Code Online (Sandbox Code Playgroud)

Pie*_*sen 11

其他两个答案都不正确,特别是来自OP的答案.query_posts应该永远不会被使用,甚至在手抄本中说明,所以请阅读手抄本.此外,您永远不应该使用自定义查询替换主查询.

解决方案很简单,如下所述,这是正确的方法.

原始答案

你有几个缺点

  • 为了您的自定义后类型有一个包,你需要在设置has_archive参数true在您的自定义后注册类型参数.看到register_post_type()

  • 如果您不打算使用自定义帖子类型,请将hierarchical参数设置为false.将此设置为true会大大减慢后端,因为您的帖子会增加,因为Wordpress会尝试为每个帖子构建一个树,就像它对页面一样

  • 切勿使用自定义查询代替主查询.它总是比较麻烦,浪费资源.有关正确使用自定义查询的位置和时间的完整说明,请参阅此文章.

  • 这一点是前一个的延伸.如果需要更改主查询,请使用pre_get_posts此操作.它使用与WP_Query主查询用于WP_Query获取帖子的完全相同的参数.这在上面的链接帖子中都有解释

  • 您在自定义查询中的主要缺陷是您不了解类别,标签和自定义分类之间的差异.我已经写了一篇完整的帖子(你可以在这里阅读)并且实际上也将它输入到了codex中.您正在使用自定义分类,因此类别参数将不起作用.您需要使用tax_query自定义分类法

要解决您的问题,请执行以下步骤

  • has_achive在注册自定义帖子类型并将其设置为时,将参数添加到参数中true.如果需要,也可以在自定义帖子类型中将hierarchical参数设置false为.(不要将此设置为自定义分类,这将使您的分类法像普通标记一样运行)

  • 在此之后,通过访问"设置"下的永久链接页面并单击"更新"来刷新重写规则

  • 访问您的主页只是为了确保您的新规则已保存

  • 删除自定义查询并返回默认循环.你archive-work.php应该看起来像这样

    if( have_posts() ) {
        while( have_posts() ) {
            the_post();
    
            // Your custom markup and template tags
    
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果你需要从一个特定项显示的帖子,创建taxonomy.php,taxonomy-{$taxonomy}.phptaxonomy-{$taxonomy}-{$term}.php模板.检查模板层次结构以获取更多信息

编辑1

如果您只需要在自定义帖子类型存档术语中显示特定术语,则在完成上述操作后,使用pre_get_posts以正确方式更改主查询

add_action( 'pre_get_posts', function ( $q ) {

    if( !is_admin() && $q->is_main_query() && is_post_type_archive( 'work' ) ) {
        $q->set( 'categories', 'slides' );
    }

});
Run Code Online (Sandbox Code Playgroud)

编辑2

这是解决这个问题的代码

复制并粘贴以下代码,代替您注册帖子类型的代码.我添加了has_archive参数.我还将分类法的重写规则更改为categories.为自定义帖子类型和分类法设置相同的slug真的很麻烦.这在默认情况下不起作用,并完全抛弃一切目标

add_action( 'init', 'work_register' );   

function work_register() {   

    $labels = array( 
        'name' => _x('Work', 'post type general name'), 
        'singular_name' => _x('Work Item', 'post type singular name'), 
        'add_new' => _x('Add New', 'work item'), 
        'add_new_item' => __('Add New Work Item'), 
        'edit_item' => __('Edit Work Item'), 
        'new_item' => __('New Work Item'), 

        'view_item' => __('View Work Item'), 
        'search_items' => __('Search Work'), 
        'not_found' => __('Nothing found'), 
        'not_found_in_trash' => __('Nothing found in Trash'), 
        'parent_item_colon' => '' 
    );   

    $args = array( 
        'labels' => $labels, 
        'public' => true, 
        'publicly_queryable' => true, 
        'show_ui' => true, 
        'query_var' => true, 
        'menu_icon' => get_stylesheet_directory_uri() . '/article16.png', 
        'rewrite' => array( 'slug' => 'work', 'with_front'=> false ), 
        'capability_type' => 'post', 
        'hierarchical' => true,
        'has_archive' => true,  
        'menu_position' => null, 
        'supports' => array('title','editor','thumbnail') 
    );   

    register_post_type( 'work' , $args ); 

    register_taxonomy( 'categories', array('work'), array(
        'hierarchical' => true, 
        'label' => 'Categories', 
        'singular_label' => 'Category', 
        'rewrite' => array( 'slug' => 'categories', 'with_front'=> false )
        )
    );

    register_taxonomy_for_object_type( 'categories', 'work' ); // Better be safe than sorry
}
Run Code Online (Sandbox Code Playgroud)

在archive-work.php中,使用此代码替换您的自定义查询

<?php

$count = 0;
echo '<ul>';
while ( have_posts() ) : the_post();

    $count++;
    $class = ($count % 3 == 1) ? 'first' : '';

    echo '<li class="'.$class.'">';
    echo '<a href="';
    the_permalink();
    echo '">';
    echo '<div class="overlay" style="background-color:'.ot_get_option( 'main_colour' ).';"></div>';
    the_post_thumbnail('full');
    echo '</a>';

    echo '<br />';

    echo '<h2><a href="';
    the_permalink();
    echo '">';
    the_title();
    echo '</a></h2>';

    echo '<div class="entry-content">';
    echo limit_words(get_the_excerpt(), '30'); 
    echo '..</div>';
    echo '</li>';
endwhile;
echo '</ul>';
?>
Run Code Online (Sandbox Code Playgroud)

非常重要 - >好的,现在访问后端(管理区域)中的设置>>永久链接,然后单击保存更改.这将刷新永久链接并设置新的永久链接结构

您现在应该在访问时看到自定义帖子类型中的所有帖子

http://example.com/work/