php 如何使用自定义帖子类型存档作为首页

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 如何使用自定义帖子类型存档作为首页相关的知识,希望对你有一定的参考价值。

<?php
/*
 * 1. Go to Settings > Permalinks and select any page as home page
 * Then use the following code
 *
 * You can add those codes in your functions.php in the theme, if you think your theme won’t be changed.
 * Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'.
 * If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, 
 * give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, 
 * so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.
 *
 * Just change the correct CPT name
 */

add_action( "pre_get_posts", "archive_page_as_front_page" );
function archive_page_as_front_page( $query ){
    if( is_admin() ) return;

    if( $query->get( 'page_id' ) == get_option( 'page_on_front' ) ){
        $query->set( 'post_type', 'SET YOUR CPT NAME' );
        $query->set( 'page_id', '' );
        $query->is_page = $query->is_singular = 0;
        $query->is_archive = $query->is_post_type_archive = 1;
    }
}

以上是关于php 如何使用自定义帖子类型存档作为首页的主要内容,如果未能解决你的问题,请参考以下文章

WordPress 自定义帖子类型存档-<帖子类型>.php 不起作用

如何:在wordpress中自定义帖子类型的自定义类别存档页面

如何为自定义帖子类型创建档案

php 如果是自定义帖子类型类别 - 存档

php 在自定义帖子类型存档和一些自定义分类存档上强制命令到`menu_order`

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