我已经创建了一个自定义的帖子类型,并创建了一个归档页面来在此页面上显示它们。
默认情况下显示的帖子数是10,但我想将帖子数减少到6。
我使用此功能减少帖子数量:
function num_posts_archive_project_ie($query){
if ($query->is_archive(\'projects-ie\')) {
$query->set(\'posts_per_page\', 6);
}
return $query;
}
add_filter(\'pre_get_posts\', \'num_posts_archive_project_ie\');
这个功能似乎很有效,但当我去后台添加新项目(自定义帖子类型)时,我注意到即使在后台,显示的帖子最多也有6个。
在归档页面上,我还添加了以下代码,允许我在归档中创建更多页面,但每页仅显示6篇文章:
<div class="container projects-wrap-projects-page">
<div class="row">
<?php
if(have_posts()) : while(have_posts()) : the_post();
$categorythumb = get_field( "category_hover_thumb_project");
?>
<div class="col-sm-12 col-md-4 col-lg-4 col-xl-4 projects-container-1">
<div class="projects-container-projects-page">
<?php
if ( has_post_thumbnail() ) {
?>
<img class="projects-thumb-projects-page" src="<?php the_post_thumbnail_url(); ?>" alt="">
<?php
}
?>
<div class="overlay-white-projects">
<div class="text-thumb-projects-wrap">
<span><?php echo $categorythumb; ?></span>
</div>
</div>
</div>
<div class="name-of-the-project">
<span class="header-2"><?php the_title(); ?></span>
</div>
<div class="read-more-project">
<a class="read-more-project-btn" href="<?php the_permalink(); ?>">Read more</a>
</div>
</div>
<?php
endwhile; endif;
?>
</div>
<div class="row">
<div class="col-12 text-center">
<?php
the_posts_pagination( array(
\'mid_size\' => 2,
\'prev_text\' => \'Previous\',
\'next_text\' => \'Next\',
) );
?>
</div>
</div>
</div>
如何减少前端(归档页面)上的帖子数量,而不减少可以在后台添加的帖子数量?
我的意思是,在后台发布的帖子数量不应该受到限制,因为在归档页面上,显示了6个以上的产品,它们只是分在不同的页面上。
标题中的代码。php
<div id="mega-menu-projects-ie" class="mega-menu-projects-wrap" style="display: none;">
<div class="mega-menu-prjects-row-1">
<?php
$args = array( \'post_type\' => \'projects-ie\', \'posts_per_page\' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$mega_menu = get_field( "mega_menu");
if (!empty($mega_menu)) {
$projectlocationmenu = get_field( "project-location");
?>
<div class="mega-menu-projects-container">
<div>
<div class="mega-menu-proj-title-container"><span>
<?php
$terms = get_the_terms( $post->ID , \'projects_ie_categories_register\' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
</span></div>
<div class="mega-menu-projects-img-container">
<a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
?>
<img class="mega-menu-projects-img" src="<?php the_post_thumbnail_url(); ?>" alt="...">
<?php
}
?>
</a>
</div>
<div class="mega-menu-proj-name-container"><span class="small-text"><?php the_title(); ?></span></div>
<div class="mega-menu-proj-country-container"><span class="small-text"><?php echo $projectlocationmenu; ?></span></div>
</div>
</div>
<?
}
endwhile;
wp_reset_query();
?>
</div>