我无法在自定义分类中显示自定义帖子

时间:2018-10-10 作者:Adrian

我对自定义分类法中的显示帖子有问题。我创建了自定义帖子类型m_product 和此帖子类型的自定义分类法m_product_category. 我在主主题文件夹中创建了名为taxonomy-m_product_category.php 并为其生成html:

protectFile();

get_header();
    $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
    print_r($wp_query);
    ?>
    <div class="top-bg" style="background-image: url(<?php echo THEME_IMAGES_URI; ?>/ronney-produkty-akcesoria-salon-fryzjerski-str-kategorie-produktow-tlo-01.jpg)"></div>
    <?php
    echo do_shortcode(\'[meetProducts]\');
        if (have_posts()) {
            ?>
            <div class="container py-5">
                <div class="row" id="products-list">
                    <?php
                        while (have_posts()) {
                            the_post();
                            $subtitle = get_field(\'m_product_subtitle\');
                            ?>
                            <div class="col-12 col-md-3 mb-3">
                                <a href="<?php echo the_permalink(); ?>" class="product-box d-block" rel="noopener">
                                    <?php
                                        if (has_post_thumbnail()) {
                                            the_post_thumbnail(\'thumbnail\', [\'class\' => \'d-block mx-auto img-fluid\']);
                                        }
                                    ?>

                                    <div class="text-center mt-3 product-box-title">
                                        <?php
                                            the_title();

                                            if ($subtitle) {
                                                ?>
                                                <div class="product-box-subtitle">
                                                    <?php echo sanitize_text_field($subtitle); ?>
                                                </div>
                                                <?php
                                            }
                                        ?>

                                    </div>
                                </a>
                            </div>
                            <?php
                        }
                    ?>
                </div>

                <?php
                    if ($paged < $wp_query->max_num_pages) {
                        ?>
                        <div class="text-center mt-4">
                            <button type="button" class="load-more-products btn btn-secondary btn-sm" data-next-page="<?php echo esc_attr($paged + 1); ?>" data-max-page="<?php echo esc_attr($wp_query->max_num_pages); ?>">Wczytaj więcej</button>
                        </div>
                        <?php
                    }
                ?>
            </div>
            <?php
            wp_reset_postdata();
        } else {
            echo \'test\';
        }
    echo do_shortcode(\'[meetProducts]\');
get_footer();
但当我转到自定义分类页面时,无法在while循环中显示帖子,我的意思是什么都看不到。我没有创建任何函数来更改分类法的某些查询。存档工作正常,我没有将public设置为false或注册分类法的其他设置,我将向您显示注册分类法代码:

// Protect file
protectFile();

function custom_taxonomy_m_product_category() {
    register_taxonomy(\'m_product_category\', [\'m_product\'], [
        \'hierarchical\' => true,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'show_admin_column\' => true,
        \'exclude_from_search\' => false,
        \'query_var\' => true,
        \'public\' => true,
        \'rewrite\' => [
            \'slug\' => \'cat-slug\'
        ],
    ]);
}

add_action(\'init\', \'custom_taxonomy_m_product_category\', 0);
我在设置中重写了链接,但这没有帮助。我不知道为什么这样不行,有人能给我一些建议吗?谢谢

//编辑:以下是register custom\\u post\\u类型的代码:

function custom_post_type_m_product() {    
    $args = [
        \'label\'               => __(\'Produkty\'),
        \'description\'         => __(\'Produkty\'),
        \'labels\'              => $labels,
        \'supports\'            => [
            \'title\',
            \'revisions\',
            \'thumbnail\',
            \'excerpt\'
        ],
        \'taxonomies\' => [
            \'m_product_category\',
            \'m_product_color\'
        ],
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 6,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => true,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
        \'rewrite\'             => [
            \'slug\' => \'produkty\'
        ],
        \'menu_icon\'           => \'dashicons-cart\'
    ];

    register_post_type(\'m_product\', $args);

}

add_action(\'init\', \'custom_post_type_m_product\', 0);
//编辑2:

我在社区帮助下找到了解决方案exclude_from_search 自定义帖子类型为true. 谢谢你的耐心,干杯!

1 个回复
SO网友:Rachid Chihabi

try this code :

function custom_taxonomy_m_product_category() {
        register_taxonomy(\'m_product_category\', [\'m_product\'], [
            \'hierarchical\' => true,
            \'labels\' => $labels,
            \'show_ui\' => true,
            \'show_admin_column\' => true,
            \'exclude_from_search\' => **false**,
            \'query_var\' => true,
            \'public\' => true,
            \'hierarchical\' => true,
            \'rewrite\' => [\'slug\' => \'cat-slug\', \'with_front\' => true ],
        ]);
    }
结束