如何为 CPT 创建档案

问题描述 投票:0回答:2

我在 WordPress 中创建了一个名为“分钟”的 CPT(自定义帖子类型),并使用以下 php 为帖子提供存档

add_filter( 'getarchives_where', 'getarchives_where_filter', 10, 2 );
add_filter( 'generate_rewrite_rules', 'generate_events_rewrite_rules' );

function getarchives_where_filter( $where, $args ) {

    if ( isset($args['post_type']) ) {      
        $where = "WHERE post_type = '$args[post_type]' AND post_status = 'publish'";
    }

    return $where;
}

function generate_minutes_rewrite_rules( $wp_rewrite ) {

    $event_rules = array(
        'minutes/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => 'index.php?post_type=minutes&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
        'minutes/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?post_type=minutes&year=$matches[1]&monthnum=$matches[2]',
        'minutes/([0-9]{4})/?$' => 'index.php?post_type=minutes&year=$matches[1]' 
    );

    $wp_rewrite->rules = $minutes_rules + $wp_rewrite->rules;
}

function get_archives_minutes_link( $link ) {

    return str_replace( get_site_url(), get_site_url() . '/minutes', $link );

};

并在我的单个帖子模板上创建每月侧栏。我正在用这个:

add_filter( 'get_archives_link', 'get_archives_minutes_link', 10, 2 );

wp_get_archives( array( 'post_type' => 'minutes' ) );            
wp_get_archives( array( 'post_type' => 'minutes', 'type' => 'yearly' ) );
wp_get_archives( array( 'post_type' => 'minutes', 'type' => 'monthly' ) );
wp_get_archives( array( 'post_type' => 'minutes', 'type' => 'daily' ) );

remove_filter( 'get_archives_link', 'get_archives_minutes_link', 10, 2 );

我已重新保存永久链接,但每月链接未链接到存档。

有什么想法吗?

php wordpress templates post archive
2个回答
0
投票

https://developer.wordpress.org/themes/basics/template-hierarchy/#custom-post-types

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'minutes',
        array(
            'labels' => array(
                'name' => __( 'Minutes' ),
                'singular_name' => __( 'Minute' )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );
}

确保 'has_archive' => true

然后,创建一个 archive-months.php 文件。这将是会议记录页面的存档。

在该页面中,使用默认的 WordPress 循环,或任何您想要获取内容的内容

if ( have_posts() ) :

    /* Start the 'WORDPRESS LOOP' */
    while ( have_posts() ) :
        
        /* Might be one or the other vased on your setup. */
        the_post();
        get_post_type();

    endwhile;

else :

    echo 'No Minutes CPT posts created yet.';

endif;

0
投票

看来这个问题与您所需要的有些相似。 请检查下面的答案并更新“分钟”帖子类型的代码。

自定义帖子类型每年/每月存档

希望您的问题能够得到解决。我不会在这里再次添加相同的代码。

© www.soinside.com 2019 - 2024. All rights reserved.