当前位置: 首页 > 知识库问答 >
问题:

Wordpress自定义分类归档页不工作

秦才
2023-03-14

我有一个自定义分类法分配给一个自定义帖子类型。我创建了一个名为“taxonomy brands.php”的模板,当我访问自定义分类法存档时会调用该模板,但是当我调用该模板中的循环时,它认为我没有任何帖子(它跳过if(have_posts())并执行else{}).我的自定义帖子类型为“has_archive”=

代码中是否有可能导致这种情况?提前感谢你的帮助。

这是我用来注册CPT的代码。

add_action( 'init', 'register_custom_post_type_apknews' );
function register_custom_post_type_apknews() {

$phone_labels = array(
'name' => _x( 'specification', 'specification' ),
'singular_name' => _x( 'specification', 'specification' ),
'add_new' => _x( 'Add New', 'specification' ),
'add_new_item' => _x( 'Add New specification', 'specification' ),
'edit_item' => _x( 'Edit specification', 'specification' ),
'new_item' => _x( 'New specification', 'specification' ),
'view_item' => _x( 'View specification', 'specification' ),
'search_items' => _x( 'Search specification', 'specification' ),
'not_found' => _x( 'No specification found', 'specification' ),
'not_found_in_trash' => _x( 'No specification found in Trash', 'specification' ),
'parent_item_colon' => _x( 'Parent specification:', 'specification' ),
'menu_name' => _x( 'specifications', 'specification' ),
);
$phone_args = array(
'labels' => $phone_labels,
'hierarchical' => false,
'description' => 'All mobile phone specifications.', // description for custom post type
'supports' => array( 'title','thumbnail'), // more support : 'excerpt','author','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats'
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => get_stylesheet_directory_uri(). '/images/specification.png',
'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true, //array( 'slug' => 'specif' ),
'capability_type' => 'post'
);

register_post_type( 'specification', $phone_args );
}

这是我用来注册自定义分类法的代码。

add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );

function create_topics_hierarchical_taxonomy() {    

$brands_labels = array(
'name' => _x( 'Brands', 'Brands' ),
'singular_name' => _x( 'Brand', 'Brand' ),
'search_items' =>  __( 'Search Brands' ),
'all_items' => __( 'All Brands' ),
'parent_item' => __( 'Parent Brand' ),
'parent_item_colon' => __( 'Parent Brand:' ),
'edit_item' => __( 'Edit Brand' ), 
'update_item' => __( 'Update Brand' ),
'add_new_item' => __( 'Add New Brand' ),
'new_item_name' => __( 'New Brand Name' ),
'menu_name' => __( 'Brands' ),
);

register_taxonomy('brands',array('specification'), array(
'hierarchical' => true,
'labels' => $brands_labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'brands' ),
));

}

最后是taxonomy-brands.php的代码。

    <?php
    $args = array(
        'post_type' => 'specification',
        'tax_query' => array(
            array(
                'taxonomy' => 'brands',
            )
        )
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
      <!--My stuff-->
        <div class="fix single_content floatleft">
            <a href="<?php the_permalink();?>"><?php the_post_thumbnail('specification-home-feature-img', array('alt' => get_the_title())); ?></a>
            <h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
            <p>Tk: 8900 BDT</p>
        </div>
       <!--My stuff-->
      <?php endwhile; ?>
      <?php wp_reset_postdata(); ?>
    <?php else:  ?>
      <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

共有1个答案

西门安歌
2023-03-14

我认为问题在于你的查询,你需要添加术语才能工作。

$args = array(
'post_type' => 'post',
'tax_query' => array(
    array(
        'taxonomy' => 'brands',
        'field' => 'slug',
        'terms' => array('brand1','brand2'....)
    )
)
);
 类似资料:
  • 我创建了一个归档页面,我的'事件'自定义帖子猿archive-event.php.我使用的内容从标准archive.php模板,它列出了我的4个事件的顺序,他们被张贴。 下一步是按自定义字段event_date更改订单。我这样做没有问题。然后我不想显示任何事件的日期已经过去。下面的代码完美地实现了这一点。 我现在的问题是我的分页都搞砸了。我的默认阅读设置为每页2篇文章,我有一个“加载更多”按钮来显

  • 我创建了一个自定义超文本标记语言归档博客页面,我想在其中放置WordPress帖子。自定义页面有几个不同的布局,因此在帖子中循环只是重新生成我试图避免的整个页面。我想做的是循环通过每个帖子,然后将该帖子放在相关的档案中。 因此,第1列进入第1列第1列,第2列进入第2列第1列,第3列进入第2列第2列,第4列进入第2列第3列,以此类推。。。 如果这是不可能的,有没有一种方法,我可以有一个代码块,例如,

  • 我有一个自定义的帖子类型设置,有许多类别和子类别。 我试图做的是创建一个页面,显示特定类别中的所有帖子,并使用一个菜单列出所有类别子类别,以便可以过滤帖子。 我已尝试复制存档模板并将其重命名为分类法-(我的自定义分类法)。php,如果我转到slug,它会显示某些帖子,并使用

  • 我的股票与分页我尝试了很多解决方案,但没有工作!我使用WordPress5.4。我的网站是一个单页网站,当然我想在索引中使用它。php 这是我的密码: 很抱歉,如果这是一个高度重复的问题——我发现的其他修复方法都不适用于我。请有人告诉我问题在哪里? 谢谢! 编辑1我尝试了这个,它也不工作!

  • 我有一个与自定义分类法(支持)相关的自定义帖子类型(问题) 在我的主题/分类中。php我有以下代码: 这意味着我针对“支持”分类法的特定模板文件。 在分类法模板文件中,我进行了自定义查询: 我有大约11篇文章,第1页显示了前5篇文章,但问题是没有显示分页。 有什么想法吗?非常感谢。

  • 我已经创建了一个包含多个分类法的自定义查询,在此基础上,我使用进行分页。分类法是通过下拉选择表单来选择的,使用可以过滤帖子,这只是一个临时解决方案,但目前效果良好,值得注意。 分页工作正常,当选择分类术语时,帖子可以正确显示,但是当我离开第一页,比如说我进入第2页,在下拉选择器中选择不同的分类术语时,URL会保留当前页码,并向其中添加选择器ID。我想实现的是,当我在选择器中单击不同的分类法时,新选