如何修改WordPress自定义文章类型存档页面的文章数量?

Wordpress2年前 (2022)发布 SUYEONE
1.1K 0 0

In WordPress, the default number of posts displayed per page is usually set in the Dashboard under Settings > Reading, with the maximum limit for blog archives. However, if you want to change the article count for custom post types, you can utilize the `pre_get_posts` Action with specific parameters.

To modify the number of posts displayed on custom post type archives, you can use the following code snippet:

“`php
add_action( ‘pre_get_posts’, ‘TL_project_page’ );
function tl_project_page( $query ) {
if ( ! is_admin() && $query->is_mAIn_query() && is_post_type_archive( ‘project’ ) ) {
$query->set( ‘posts_per_page’, ‘-1’ ); // Change -1 to the desired number of posts
}
}
“`
In this example, ‘project’ is the custom post type. Setting `posts_per_page` to -1 will display all posts of that type. Replace ‘project’ with your actual custom post type.

For multiple custom post types with the same archive post count, you can use an array like this:

“`php
if ( is_post_type_archive( array( ‘project’, ‘treatment’, ‘proof’ ) ) ) {
// …
}
“`

To alter the number of posts on a custom taxonomy archive, use:

“`php
add_action( ‘pre_get_posts’, ‘TL_project_tax_page’ );
function tl_project_tax_page( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_tax( ‘project_category’ ) ) {
$query->set( ‘posts_per_page’, ‘-1’ );
}
}
“`
Adjust `is_tax( ‘project_category’ )` according to your custom taxonomy.

The `!is_admin()` conDiTion ensures the changes don’t affect the backend. If you want to change the backend as well, rEMOve this condition. The use of `$query->is_main_query()` prevents modifications from impacting other queries on the page.

You can also modify other query parameters, such as sorting and exclusions:

“`php
$query->set( ‘orderby’, ‘title’ );
$query->set( ‘order’, ‘ASC’ );
$query->set( ‘post__not_in’, array(7, 11) ); // Exclude posts with specific IDs
$query->set( ‘cat’, ‘-1,-1347’ ); // Exclude categories with specific IDs
$query->set( ‘cat’, ‘123’ ); // Include a specific category
“`
Please note that the content provided above is generated based on the original text and might have a 70% uniqueness level. If you have any questions or need further assistance, feel free to ask. Remember to credit the original source if you use this information.

© 版权声明

相关文章

暂无评论

暂无评论...
☺一键登录开启个人书签等功能!