【问题标题】:Correct Approach WP Custom Taxonomy and Categories正确的方法 WP 自定义分类和类别
【发布时间】:2020-05-29 04:19:48
【问题描述】:

我有许多基于不同篮球联赛的类别League A, League B, League C 等等。我想做的是让每个联赛为每个赛季都有一个自定义分类2006-2007,2007-2008,2008-2009,这样我就可以只显示那个赛季的帖子Category: League A -> Season: 2006-2007 -> Posts

到目前为止,我已经为 post 帖子类型创建了以下分类:

add_action( 'init', 'create_seasons' );

function create_seasons() {
    $labels = array(
        'name'                           => 'Seasons',
        'singular_name'                  => 'Season',
        'search_items'                   => 'Search Seasons',
        'all_items'                      => 'All Seasons',
        'edit_item'                      => 'Edit Season',
        'update_item'                    => 'Update Season',
        'add_new_item'                   => 'Add New Season',
        'new_item_name'                  => 'New Season Name',
        'menu_name'                      => 'Seasons',
        'view_item'                      => 'View Season',
        'popular_items'                  => 'Popular Season',
        'separate_items_with_commas'     => 'Separate Season with commas',
        'add_or_remove_items'            => 'Add or remove Season',
        'choose_from_most_used'          => 'Choose from the most used seasons',
        'not_found'                      => 'No seasons found'
    );

    register_taxonomy(
        'seasons',
        'post',
        array(
            'label' => __( 'Season' ),
            'hierarchical' => false,
            'labels' => $labels,
            'public' => true,
            'show_in_nav_menus' => false,
            'show_tagcloud' => false,
            'show_admin_column' => true,
            'rewrite' => array(
                'slug' => 'seasons'
            )
        )
    );
}

现在,如果我想在季节分类 2008-2009 期间显示类别 League A 的所有帖子,我可以使用 URL 参数 ?seasons=2008-2009。完整网址http://localhost/site/category/league-a/?seasons=2008-2009

困扰我的是,我想默认显示当前季节的帖子2019-2020,而不使用每个类别的 URL 参数?seasons=2019-2020

示例: http://localhost/site/category/league-a/ -> Show posts from seasons taxonomy 2019-2020 only

有没有办法让它工作?重写规则?

您还认为我的方法对我正在尝试做的事情是正确的吗?

谢谢

【问题讨论】:

    标签: php wordpress url-rewriting


    【解决方案1】:

    我认为有多种方法可以实现这一目标。我的建议是在您的术语存档页面上创建一个自定义查询。

    请先看这里:https://developer.wordpress.org/themes/basics/template-hierarchy/

    在 Wordpress 中,您可以创建您的自定义 category-{term}.php 页面,在您的情况下称为 category-league-a.php。在您的主题文件夹中复制您的 category.php,或使用 wordpress 主题中的示例(即二十九),然后复制代码。

    在您的新存档页面(WP 会自动识别它)中删除帖子循环。您现在可以使用您的分类术语创建自己的帖子查询。

    看看:https://developer.wordpress.org/reference/classes/wp_query/

    您会获得所有具有特定类别术语和季节术语的帖子。

    $args = array(
        'post_type' => 'post',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => 'league-a',
            ),
            array(
                'taxonomy' => 'seasons',
                'field'    => 'slug',
                'terms'    => '2019-2020',
            ),
        ),
    );
    $current_season_query = new WP_Query( $args );
    

    您现在可以循环浏览您的帖子:

    <?php
    // The Loop
    if ( $current_season_query ->have_posts() ) {
        while ( $current_season_query ->have_posts() ) {
            $current_season_query ->the_post();
                // echo get_the_title().'<br>'; or whatever
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        // no posts found
    }
    ?>
    

    将发布数据重置为原始查询很重要,正如上面代码中所做的那样。否则,您可能会与页面上的其他查询发生冲突。

    所以您现在已经为您的类别术语定义了页面模板。您创建了一个自定义查询,以仅显示另外具有当前季节分类术语的帖子。

    希望这会有所帮助!


    编辑:对所有术语仅使用一个类别页面模板

    您还可以在 tax_query 'category''terms' 中使用变量。

            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => $current_category,
    

            'taxonomy' => 'seasons',
            'field'    => 'slug',
            'terms'    => $current_season,
    

    $current_category 中保存 slug 或当前查看类别术语,在 $current_season 中保存分类术语的 slug。


    其他解决方法:检查url字符串中是否有参数

    但现在我想到的是,在 url 中查找参数并仅在没有设置参数时才更改查询将是另一种解决方案。这样你只需要一个category.php,你必须稍微调整一下:

    1。从url获取参数

    global $wp;
    // Get current page url
    $current_url = home_url( add_query_arg( array(), $wp->request ) );
    // Parse the URL to get array which contains its components
    $url_components = parse_url($current_url);
    // parse_str() function to parse the string passed via URL
    parse_str($url_components['query'], $params); 
    

    2。检查是否未设置参数并在这种情况下使用另一个 WP_Query

    ​​>
    if(!isset($params['seasons'])) {
        $current_season_query = new WP_Query( $args );
        // your code
    }
    else {
        // unchanged WP_Query, standard post loop in archive file
    }
    

    if 之前,您定义了新的 WP_query,如本答案的第一个代码块中所述。在else 中,您放置了您的标准帖子循环。

    这样您只需更改您的category.php 而不必为所有类别术语制作文件。

    【讨论】:

    • 感谢您的回答,这种方法的问题是我有 45 个联赛,所以我认为创建 45 个不同的category 模板效率不高。另一个问题是,如果我想导航到前几季 http://localhost/site/category/league-a/?seasons=2018-2019,它仍然会显示来自 ?seasons=2019-2020 的帖子。
    • 刚刚有了另一个想法:检查 url 的参数!也许它可以帮助你。请看看我编辑的答案。
    猜你喜欢
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 2011-05-24
    • 2019-03-12
    • 1970-01-01
    • 2014-07-12
    • 2022-12-28
    相关资源
    最近更新 更多