好吧,我正在尝试将默认分类法“类别”注册到wordpress中的一些自定义帖子...

字段“类别”正在工作,但当我转到“/ category / cat_name”时,我得到一个空数组用于该类别的帖子 . 但如果我在常规帖子上使用相同的类别,它的效果非常好 .

我在每个网站上阅读的解决方案是: register_taxonomy_for_object_type('category', 'custom-type'); ,当然我试了一下......没有任何反应 .

看看我的 funcions.php

这是我的 register_post_types() 以获取所有已注册的帖子类型:

function register_post_types() {
    register_post_type( 'articulos',
            array(
                'labels' => array(
                    'name' => __( 'Tiendas' ),
                    'singular_name' => __( 'Tienda' )
                    ),
                'public' => true,
                'has_archive' => true,
                'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
                'taxonomies' => array('category')
                )
            );

        register_post_type( 'locacion',
            array(
                'labels' => array(
                    'name' => __( 'Arriendos' ),
                    'singular_name' => __( 'Arriendo' )
                    ),
                'public' => true,
                'has_archive' =>  true,
                'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
                'taxonomies' => array('category')
                )
            );
}

如您所见,分类法已启用,然后,我注册它们:

function register_taxonomies() {
    register_taxonomy_for_object_type('category', 'articulos');
    register_taxonomy_for_object_type('category', 'locacion');
}

after 我迷上了他们的 init

function __construct() {
    add_theme_support( 'post-formats' );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'menus' );
    add_filter( 'timber_context', array( $this, 'add_to_context' ) );
    add_filter( 'get_twig', array( $this, 'add_to_twig' ) );
    add_action( 'init', array( $this, 'register_post_types' ) );
    add_action( 'init', array( $this, 'register_taxonomies' ) );
    parent::__construct();
}

忽略 add_action 上面的所有内容...在所有情况下,问题都是相同的,标有任何类别的自定义帖子未列在相应的链接中 .

任何的想法?

提前致谢!!

Edit

我的 archive.php 文件看起来像这样:

$templates = array( 'archive.twig', 'index.twig' );

$data = Timber::get_context();

$data['title'] = 'Archive';
if ( is_day() ) {
    $data['title'] = 'Archive: '.get_the_date( 'D M Y' );
} else if ( is_month() ) {
    $data['title'] = 'Archive: '.get_the_date( 'M Y' );
} else if ( is_year() ) {
    $data['title'] = 'Archive: '.get_the_date( 'Y' );
} else if ( is_tag() ) {
    $data['title'] = single_tag_title( '', false );
} else if ( is_category() ) {
    $data['title'] = single_cat_title( '', false );
    array_unshift( $templates, 'archive-' . get_query_var( 'cat' ) . '.twig' );
} else if ( is_post_type_archive() ) {
    $data['title'] = post_type_archive_title( '', false );
    array_unshift( $templates, 'archive-' . get_post_type() . '.twig' );
}

$data['posts'] = Timber::get_posts();

Timber::render( $templates, $data );

现在,我发布这个回答一个问题:

您是否在查询中设置了post_type?默认查询仅显示标准帖子

但据我所知,这不是必要的......

PD:我正在使用timber插件进行wordpress,这是用于渲染twig文件