自定义帖子类型档案页面模板

时间:2015-03-04 作者:Eth

我正在尝试为自定义帖子类型实现存档:

http://mywebsite:8888/about-us/client-news/2015/03/
问题是,它一直在为我提供404页面,我无法找出它找不到的页面模板。我试过打开调试和其他一些步骤

注册:

function client_news() {

    $labels = array(
        \'name\' => _x("Client News", "post type general name"),
        \'singular_name\' => _x("Client News Item", "post type singular name"),
        \'menu_name\' => \'Client News\',
        \'add_new\' => _x("Add New", "news item"),
        \'add_new_item\' => __("Add New News Item"),
        \'edit_item\' => __("Edit News Item"),
        \'new_item\' => __("New News Item"),
        \'view_item\' => __("View News Item"),
        \'search_items\' => __("Search Client News"),
        \'not_found\' =>  __("No Useful Items Found"),
        \'not_found_in_trash\' => __("No Useful Items Found in Trash"),
        \'parent_item_colon\' => \'\'
    );
    // Register post type
    register_post_type(\'clientnews\' , array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => true,
        \'rewrite\' => array( \'slug\' => \'news\' ),
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\')
    ) );
}

add_action( \'init\', \'client_news\', 0 );
归档循环如下:

<ul class="news_archive">
    <?php
        add_filter( \'get_archives_link\', \'get_archives_clientnews_link\', 10, 2 );
        wp_get_archives( array( \'post_type\' => \'clientnews\', \'type\' => \'monthly\' ) );
        remove_filter( \'get_archives_link\', \'get_archives_clientnews_link\', 10, 2 );
    ?>
</ul>

2 个回复
最合适的回答,由SO网友:Eth 整理而成

我已通过在函数文件中添加自定义重写来解决此问题:

// Add custom rewrite rules to handle things like years in custom post archives
function add_rewrite_rules($aRules) {
    $aNewRules = array(
        \'news/([0-9]{4})/([0-9]{2})/page/?([0-9]{1,})/?$\' => \'index.php?post_type=clientnews&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]\',
        \'news/([0-9]{4})/([0-9]{2})/?$\' => \'index.php?post_type=clientnews&year=$matches[1]&monthnum=$matches[2]\',
        \'about-us/other-news/([0-9]{4})/([0-9]{2})/page/?([0-9]{1,})/?$\' => \'index.php?post_type=othernews&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]\',
        \'about-us/other-news/([0-9]{4})/([0-9]{2})/?$\' => \'index.php?post_type=othernews&year=$matches[1]&monthnum=$matches[2]\'
    );
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

// hook add_rewrite_rules function into rewrite_rules_array
add_filter(\'rewrite_rules_array\', \'add_rewrite_rules\');
然后在存档页面中,我将查询更改为:

$aParts = explode( \'/\', $_SERVER[\'REQUEST_URI\'] );

$iYear  = get_query_var(\'year\');
$iMonth = get_query_var(\'monthnum\');

if( $iMonth <= 0 && $iYear > 0 )
{
    $iMonth = $aParts[ 4 ];
}

$news = new WP_Query(\'showposts=6&post_type=othernews&paged=\'. $paged . \'&year=\' . $iYear . \'&monthnum=\' . $iMonth );

SO网友:Fiaz Husyn

尝试访问:

http://mywebsite:8888/about-us/news/2015/03/
您正在使用slug => \'news\' 所以\'client-news\' 会给你一个错误

结束

相关推荐