admin管理员组文章数量:1024639
I'm trying to display only posts in my search result page.
so I just added the code below in functions.php
:
function is_type_page() {
global $post;
// Check if the current post is a page.
if ($post->post_type == 'page') {
return true;
} else {
return false;
}
}
and this condition below in search.php
:
<?php while ( have_posts() ) : the_post(); ?>
<?php if (is_type_page()) continue; ?>
I can get successfully the posts but the problem is that pagination is not displayed correctly , it shows me many blank pages, it seems like it still quering both posts and pages ( just hiding pages content)
How can I fix that?
This is my code:
<?php
while (have_posts()):
the_post();
?>
<?php
if (is_type_page())
continue;
?>
<div class="search-result">
<div class="image-content">
<?php
if (has_post_thumbnail()) {
the_post_thumbnail('large');
} else {
?>
<img src="<?php bloginfo('template_directory'); ?>/assets/images/image-not-found.png" />
<?php
}
?>
</div>
<div class="text-content">
<h4 style="">
<?php the_title(); ?>
</h4>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
<?php endwhile; ?>
<!-- PAGINATION here below -->
<div class="nav-previous alignleft">
<?php previous_posts_link('Older posts'); ?>
</div>
<div class="nav-next alignright">
<?php next_posts_link('Newer posts'); ?>
</div>
I'm trying to display only posts in my search result page.
so I just added the code below in functions.php
:
function is_type_page() {
global $post;
// Check if the current post is a page.
if ($post->post_type == 'page') {
return true;
} else {
return false;
}
}
and this condition below in search.php
:
<?php while ( have_posts() ) : the_post(); ?>
<?php if (is_type_page()) continue; ?>
I can get successfully the posts but the problem is that pagination is not displayed correctly , it shows me many blank pages, it seems like it still quering both posts and pages ( just hiding pages content)
How can I fix that?
This is my code:
<?php
while (have_posts()):
the_post();
?>
<?php
if (is_type_page())
continue;
?>
<div class="search-result">
<div class="image-content">
<?php
if (has_post_thumbnail()) {
the_post_thumbnail('large');
} else {
?>
<img src="<?php bloginfo('template_directory'); ?>/assets/images/image-not-found.png" />
<?php
}
?>
</div>
<div class="text-content">
<h4 style="">
<?php the_title(); ?>
</h4>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
<?php endwhile; ?>
<!-- PAGINATION here below -->
<div class="nav-previous alignleft">
<?php previous_posts_link('Older posts'); ?>
</div>
<div class="nav-next alignright">
<?php next_posts_link('Newer posts'); ?>
</div>
Share
Improve this question
edited Apr 16, 2019 at 15:57
norman.lol
3,2413 gold badges30 silver badges35 bronze badges
asked Apr 16, 2019 at 11:40
AtefAtef
1232 bronze badges
1 Answer
Reset to default 2The pagination isn't working correctly because it assumes you want to paginate all results. You are simply hiding pages, by which time it's too late to adjust the pagination.
To fix this, you need to alter the query using the pre_get_posts filter. For example:
function search_only_posts($query) {
if($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','search_only_posts');
You may need to add various checks like is_admin() or is_main_query() so that you only change the behaviour on the front-end of the site, and not the admin area.
I'm trying to display only posts in my search result page.
so I just added the code below in functions.php
:
function is_type_page() {
global $post;
// Check if the current post is a page.
if ($post->post_type == 'page') {
return true;
} else {
return false;
}
}
and this condition below in search.php
:
<?php while ( have_posts() ) : the_post(); ?>
<?php if (is_type_page()) continue; ?>
I can get successfully the posts but the problem is that pagination is not displayed correctly , it shows me many blank pages, it seems like it still quering both posts and pages ( just hiding pages content)
How can I fix that?
This is my code:
<?php
while (have_posts()):
the_post();
?>
<?php
if (is_type_page())
continue;
?>
<div class="search-result">
<div class="image-content">
<?php
if (has_post_thumbnail()) {
the_post_thumbnail('large');
} else {
?>
<img src="<?php bloginfo('template_directory'); ?>/assets/images/image-not-found.png" />
<?php
}
?>
</div>
<div class="text-content">
<h4 style="">
<?php the_title(); ?>
</h4>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
<?php endwhile; ?>
<!-- PAGINATION here below -->
<div class="nav-previous alignleft">
<?php previous_posts_link('Older posts'); ?>
</div>
<div class="nav-next alignright">
<?php next_posts_link('Newer posts'); ?>
</div>
I'm trying to display only posts in my search result page.
so I just added the code below in functions.php
:
function is_type_page() {
global $post;
// Check if the current post is a page.
if ($post->post_type == 'page') {
return true;
} else {
return false;
}
}
and this condition below in search.php
:
<?php while ( have_posts() ) : the_post(); ?>
<?php if (is_type_page()) continue; ?>
I can get successfully the posts but the problem is that pagination is not displayed correctly , it shows me many blank pages, it seems like it still quering both posts and pages ( just hiding pages content)
How can I fix that?
This is my code:
<?php
while (have_posts()):
the_post();
?>
<?php
if (is_type_page())
continue;
?>
<div class="search-result">
<div class="image-content">
<?php
if (has_post_thumbnail()) {
the_post_thumbnail('large');
} else {
?>
<img src="<?php bloginfo('template_directory'); ?>/assets/images/image-not-found.png" />
<?php
}
?>
</div>
<div class="text-content">
<h4 style="">
<?php the_title(); ?>
</h4>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
<?php endwhile; ?>
<!-- PAGINATION here below -->
<div class="nav-previous alignleft">
<?php previous_posts_link('Older posts'); ?>
</div>
<div class="nav-next alignright">
<?php next_posts_link('Newer posts'); ?>
</div>
Share
Improve this question
edited Apr 16, 2019 at 15:57
norman.lol
3,2413 gold badges30 silver badges35 bronze badges
asked Apr 16, 2019 at 11:40
AtefAtef
1232 bronze badges
1 Answer
Reset to default 2The pagination isn't working correctly because it assumes you want to paginate all results. You are simply hiding pages, by which time it's too late to adjust the pagination.
To fix this, you need to alter the query using the pre_get_posts filter. For example:
function search_only_posts($query) {
if($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','search_only_posts');
You may need to add various checks like is_admin() or is_main_query() so that you only change the behaviour on the front-end of the site, and not the admin area.
本文标签: Pagination not applied on posts
版权声明:本文标题:Pagination not applied on posts 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745583284a2157435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论