The following is my search.php:
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type == 'page') : ?>
Show Page results
<?php endif; ?>
<?php endwhile; ?>
<?php rewind_posts(); ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type != 'page') : ?>
Show non-page results
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
No Results
<?php endif; ?>
The code allows search results under posts to be separated from those found in pages. If no search results are found for either posts or pages, the text "No Results" is displayed.
However, when results are found for pages but NOT posts (and vice versa), under the 'Post Search Results" there is nothing displayed. I would like the code tweaked so that if there are no results found under "Post Search Results" but there are results found under "Page Search Results," the text "No Results" is displayed underneath the "Post Search Results" header.
Thanks a lot everyone.
Give this a try, you can expand the array to include any post_type your site is using:
<?php
foreach (array('post','page') as $pt) :
$search_query = new WP_Query(array(
'post_type' => $pt,
's' => $s,
'posts_per_page' => 10,
'paged' => $paged
)
);
?>
<?php if ($pt == 'post') : ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php else : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php endif; ?>
<?php
if ($search_query->have_posts()) : while ($search_query->have_posts()) : $search_query->the_post();
if ($pt == 'post') :
?>
Post Results Code
<?php else : ?>
Page Results Code
<?php endif; ?>
<?php endwhile; else : ?>
No Results
<?php endif; ?>
<?php endforeach; ?>
Related
I have one custom loop in a custom homepage in WordPress that is pulling 4 posts with some banners in middle. All is working fine, however pagination always shows same posts. Is there a way for me to add pagination to this custom loop?
My index.php code:
<?php
query_posts(array(
'post_type' => 'post',
'showposts' => 4,
) );
?>
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 3) : ?>
<!-- banners -->
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<?php else : ?>
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<!-- banners -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<p align="center"><?php posts_nav_link(); ?></p>
Inside your query_post array add this line:
'paged' => ( get_query_var('paged') ) ? get_query_var('paged') : 1,
and in settings -> reading set 4 posts per blog page.
I am doing theme integration in wordpress.I created home.php (template page) and select from backend(for front end page).
Now i want to show posts on front page.I put following code for display post in front page.But code is not working.Where i am wrong ?
Here is my code
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile;
endif;
?>
You can use following code -
<?php
$args = array('posts_per_page' => 3, 'post__not_in' => get_option('sticky_posts'));
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php esc_html(the_title()); ?>
<?php the_excerpt(); ?>
<?php
endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I am using a wordpress query to display 3 posts at random from a custom post type. I am using the following code which is working fine:
<?php $my_query = new WP_Query('post_type=my_post_type&orderby=rand&showposts=3'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php endwhile; ?>
<?wp_reset_query(); ?>
However, I want to mirror the same query below to show the same items once again. So two wordpress queries on one page, the first query picking 3 random posts and the second query showing the exact same results of the first query. Any help would be appreciated. Thanks
try this out :)
<?php $my_query = new WP_Query('post_type=post&orderby=rand&showposts=3'); ?>
<?php $i=0; ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php
$myPostVar[$i] = array (
'title' => get_the_title(),
'content' => get_the_content()
);
$i++;
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php foreach ($myPostVar as $Postvar) : ?>
<h2><?php echo $Postvar['title']; ?></h2>
<p><?php echo $Postvar['content']; ?></p>
<?php endforeach; ?>
Below is the code for the Search Results. The search separates page results from post results.
<?php
foreach (array('post','page') as $pt) :
$search_query = new WP_Query(array(
'post_type' => $pt,
's' => $s,
'posts_per_page' => 10,
'paged' => $paged
)
);
?>
<?php if ($pt == 'post') : ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php else : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php endif; ?>
<?php
if ($search_query->have_posts()) : while ($search_query->have_posts()) : $search_query->the_post();
if ($pt == 'post') :
?>
Post Results Code
<?php else : ?>
Page Results Code
<?php endif; ?>
<?php endwhile; else : ?>
No Results
<?php endif; ?>
<?php endforeach; ?>
Using the current code, if no search results are found for either Post/Page Search Results, the phrase "No Results" is displayed below the Post/Page Results header(s). This needs to be preserved.
I want to position the header "Post Search Results" and the "Post Results Code" in one div and the header "Page Search Results" and the "Page Results Code" in a separate div that is set inside another div. Like this:
<div id="A">
<div id="B">Post Results header & Post Results Code</div>
<div id="C">Page Results header & Page Results Code</div>
</div>
Where do I insert the div tags in the code to accomplish this? Thank you!
This should do what you want:
<div id="A">
<?php
foreach (array('post','page') as $pt) :
$search_query = new WP_Query(array(
'post_type' => $pt,
's' => $s,
'posts_per_page' => 10,
'paged' => $paged
)
);
?>
<?php if ($pt == 'post') : ?>
<div id="B">
<h2 class="pagetitle">Post Search Results</h2>
<?php else : ?>
<div id="C">
<h2 class="pagetitle">Page Search Results</h2>
<?php endif; ?>
<?php
if ($search_query->have_posts()) : while ($search_query->have_posts()) : $search_query->the_post();
if ($pt == 'post') :
?>
Post Results Code
<?php else : ?>
Page Results Code
<?php endif; ?>
<?php endwhile; else : ?>
No Results
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
Can anybody look at the following codes? After I excluded the category 15, the next_posts_link does not work, nor does the page navigation plugin. When I click the page 2, 3,4, it only display the posts in page 1.
<?php query_posts('cat=-15'); ?>
<?php while (have_posts()) : the_post(); ?>
... ....
<?php endwhile; else: ?>
<?php next_posts_link() ?>
<?php endif; ?>
How about...
<?php query_posts('cat=-15'); ?>
<?php while (have_posts()) : the_post(); ?>
... ....
<?php endwhile; ?>
<?php next_posts_link() ?>
<?php endif; ?>
??
Try it like this:
<?php query_posts(array( 'cat' => -15, 'paged' => get_query_var('paged') ) ); ?>
This should work.