gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:56:22 25 4
gpt4 key购买 nike

我正在一个主题名称为 的 wordpress 网站上工作flozo .它有一个名为 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>';
?>

我已经添加
 $args = array( 'post_type' => 'work', 'tag_ID' => 15 ,'posts_per_page' => 15 );

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

这也没有帮助。

编辑:

类别网址是

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



}

最佳答案

其他两个答案都不正确,特别是来自 OP 的答案。 query_posts应该从来没有使用,它甚至在法典中说明,所以请阅读法典。此外,您永远不应该用自定义查询替换主查询。

解决方案很简单,正如我在下面描述的和 这是正确的方法这样做。

原答案

你这里有几个缺陷

  • 要使您的自定义帖子类型具有存档,您需要设置 has_archive true 的参数在您的自定义帖子类型注册参数中。见 register_post_type()
  • 如果您不打算使用像页面这样的自定义帖子类型,请设置 hierarchical false 的参数.将此设置为 true 会随着帖子的增加而大大减慢您的后端速度,因为 Wordpress 会尝试为每个帖子构建一棵树,就像为页面做的一样
  • 切勿使用自定义查询代替主查询。总是比较麻烦,浪费资源。见 this post有关在何时何地正确使用自定义查询的完整说明。
  • 这一点是上一点的延伸。如果您需要更改主查询,请使用 pre_get_posts这样做。它使用与 WP_Query 完全相同的参数作为主查询使用 WP_Query获取帖子。这在上面的链接帖子
  • 中都有解释
  • 您自定义查询的主要缺陷是您缺乏了解类别、标签和自定义分类法之间的区别。我已经写了一篇完整的文章(你可以阅读 here),并且实际上也将它输入到了法典中。您正在使用自定义分类法,因此类别参数将不起作用。您需要使用 tax_query 用于自定义分类法

  • 要解决您的问题,请按照以下步骤操作
  • 添加 has_achive注册自定义帖子类型时的参数参数并将其设置为 true .如果需要,设置 hierarchical false 的参数以及您的自定义帖子类型。 (不要为您的自定义分类设置此项,这将使您的分类表现得像普通标签)
  • 在此之后,通过访问“设置”下的永久链接页面并单击“更新”来刷新您的重写规则
  • 访问您的主页只是为了确保您的新规则已保存
  • 删除您的自定义查询并返回到默认循环。您的 archive-work.php应该看起来像这样
    if( have_posts() ) {
    while( have_posts() ) {
    the_post();

    // Your custom markup and template tags

    }
    }
  • 如果您需要显示来自特定术语的帖子,请创建 taxonomy.php , taxonomy-{$taxonomy}.phptaxonomy-{$taxonomy}-{$term}.php模板。检查 Template Hierarchy更多信息

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

    });

    编辑 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
    }

    在您的 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>';
    ?>

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

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

    http://example.com/work/

    关于wordpress - 将类别添加到自定义帖子类型并根据类别显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27896131/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com