WooCommerce面包屑显示自定义帖子而不是产品数据

时间:2017-07-05 作者:Maciej Rogosz

我正在为我的商店开发一个带有一些附加功能的自定义主题。为了在我的店铺后端和前端的一个地方收集所有促销活动,我为其创建了自定义帖子类型和自定义分类法。其代码为:

/**
*
* Registering custom post type \'promocja\'
*
**/
function missplanner_promotion_post_type() {

    /**
    * UI Labels
    **/
    $custom_post_labels = array(
        \'name\'               => _x( \'Promocje\', \'post type general name\', \'missplanner-official\' ),
        \'singular_name\'      => _x( \'Promocje\', \'post type singular name\', \'missplanner-official\' ),
        \'menu_name\'          => _x( \'Promocje\', \'admin menu\', \'missplanner-official\' ),
        \'name_admin_bar\'     => _x( \'Dodaj promocję\', \'add new on admin bar\', \'missplanner-official\' ),
        \'add_new\'            => _x( \'Dodaj promocję\', \'promocja\', \'missplanner-official\' ),
        \'add_new_item\'       => __( \'Dodaj promocję\', \'missplanner-official\' ),
        \'new_item\'           => __( \'Nowa promocja\', \'missplanner-official\' ),
        \'edit_item\'          => __( \'Edytuj promocję\', \'missplanner-official\' ),
        \'view_item\'          => __( \'Zobacz promocję\', \'missplanner-official\' ),
        \'all_items\'          => __( \'Zobacz wszystkie promocje\', \'missplanner-official\' ),
        \'search_items\'       => __( \'Szukaj...\', \'missplanner-official\' ),
        \'parent_item_colon\'  => __( \'Elementy rodzice:\', \'missplanner-official\' ),
        \'not_found\'          => __( \'Nie znaleziono.\', \'missplanner-official\' ),
        \'not_found_in_trash\' => __( \'Nie znaleziono w koszu.\', \'missplanner-official\' )
    );

    /**
    * Other options
    **/
    $custom_post_args = array(
        \'labels\' => $custom_post_labels,
        \'description\'        => __( \'Wszystkie promocje Miss Planner.\', \'missplanner-official\' ),
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'promocja\' ),
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\' )
    );
    register_post_type( \'promocja\', $custom_post_args);

}

add_action( \'init\', \'missplanner_promotion_post_type\' );

/**
*
* Custom Taxonomy for post type "Promocja"
*
**/
add_action( \'init\', \'missplanner_promotion_taxonomy\', 0);

function missplanner_promotion_taxonomy() {

    /**
    * custom taxonomy labels
    **/
    $kategorie_promocje_labels = array(
        \'name\'              => _x( \'Kategorie Promocji\', \'taxonomy general name\', \'missplanner-official\' ),
        \'singular_name\'     => _x( \'Kategoria Promocji\', \'taxonomy singular name\', \'missplanner-official\' ),
        \'search_items\'      => __( \'Szukaj kategorii promocji\', \'missplanner-official\' ),
        \'all_items\'         => __( \'Wszystkie kategorie promocji\', \'missplanner-official\' ),
        \'parent_item\'       => __( \'Kategorie Promocji rodzic\', \'missplanner-official\' ),
        \'parent_item_colon\' => __( \'Kategorie Promocji rodzic:\', \'missplanner-official\' ),
        \'edit_item\'         => __( \'Edytuj kategorię promocji\', \'missplanner-official\' ),
        \'update_item\'       => __( \'Aktualizuj kategorię promocji\', \'missplanner-official\' ),
        \'add_new_item\'      => __( \'Dodaj nową kategorię promocji\', \'missplanner-official\' ),
        \'new_item_name\'     => __( \'Nazwa nowej kategorii promocji\', \'missplanner-official\' ),
        \'menu_name\'         => __( \'Kategorie Promocji\', \'missplanner-official\' ),
    );

    /**
    * custom taxonomy args
    **/
    $kategorie_promocje_args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $kategorie_promocje_labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'promocje\' ),
    );

    /**
    * Add taxonomy "Kategorie Promocji"
    **/
    register_taxonomy( \'kategorie-promocji\', array( \'promocja\' ), $kategorie_promocje_args );

}
这里是单一产品页面上的面包屑(在其他任何地方都很好):wrong breadcrumb single product page woocommerce

将所有自定义帖子设置为草稿后,它会恢复正常。enter image description here我应该在哪里更改代码?

1 个回复
最合适的回答,由SO网友:Maciej Rogosz 整理而成

解决方案非常简单。我复制了single-product.php 从…起woocommerce/templates 并对其进行了修改,使循环在\\u main\\u content挂钩之前开始:

get_header( \'shop\' ); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php   
        /**
        * woocommerce_before_main_content hook.
        *
         * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
         * @hooked woocommerce_breadcrumb - 20
         */
        do_action( \'woocommerce_before_main_content\' );
    ?>
这样工作正常,所有功能都得到了维护。

结束

相关推荐

Breadcrumbs - get the author?

我有自己的函数breadcrumbs()。在其中,我调用is\\u author()来确定我是否在作者页面上。如果是真的,我想知道我在哪个作者的页面上。我尝试了\\u author(),但没有结果。我还查阅了WP codex。有人能帮忙吗?