I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.
Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.
Any idea how to get the theme to search both posts and pages?
Here is the majority of search.php:
<?php if (have_posts()) : ?>
<h3>Search Results</h3>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h4><?php the_title(); ?></h4>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h3>No posts found.</h3>
<?php endif; ?>
Add this code to your functions.php file.
function wpshock_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post','page') );
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
http://wpth.net/limit-wordpress-search-results-to-specific-post-types
WP Search http://wpsear.ch/ has that ability.You can adjust what post types you want to show in results page.
In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='page')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
Let me know if it worked.
Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/
Related
I need to style the first post in the loop differently from the rest in a wordpress theme template. Is there a specific wordpress function to check for this or do I just need to set a "first" flag like the below?
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $first = !isset( $first ) ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php endwhile; ?>
The function I'm looking for would replace the $first = ! isset( $first) check. Does this function exist in WordPress?
Try this
<?php $Inc=0; ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $Inc==0 ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php $Inc++; ?>
<?php endwhile; ?>
I am learning WordPress theme development and I have developed a theme. All work has been done, but the author name not displaying in a blog page. I used the following:
<?php the_author(); ?>
but it is not working. Is it necessary to do any function or code in functions.php?
Basically, this is the loop of a page in wordpress:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
some wordrpess code
<?php endif; ?>
You have to enter the get request INTO the loop, something like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $author = get_the_author(); ?>
or
<?php the_author(); ?>
<?php endif; ?>
If not within the loop you can try this
<?php the_author_meta( 'display_name', $userID ); ?>
From Codex
i use that code to post an ad in my homepage every 3 posts but instead of that the ad appears behind the 1st post and it seems like it doesnt recognise the loop at all
<div id="mason-layout" class="transitions-enabled fluid">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'blog' ); ?>
<?php if ($i == 3) { ?> ADHERE <?php } ?>
<?php $i++; ?>
<?php endwhile; ?>
any ideas?
thanks
You're missing part of your loop.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
Edit: If you want to break the if statement onto multiple lines you'll probably want to use a different syntax. Eg:
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
Recommended reading: http://codex.wordpress.org/The_Loop
I'm currently working on integrating wordpress with my website, but I'm hitting a wall here.
I need the initial blog page(basically just the index.php of WP) to pick up the <!--more--> quicktags, but I can't seem to get it to work.
I've followed the WP Codex, but that didn't help me at all.
So here's part of the template file: content.php
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
the_excerpt();
?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<div class="bthumb2">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(220, 130));
}
?>
</div>
<?php
the_content("READ MORE");
?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'tinfoilhats' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php endif; ?>
But it just keeps displaying the <!--more--> tag in the posts once I load the page.
I've tried it with the global $more thing, but that didn't work for me.
<?php while ( have_posts() ) : the_post(); ?>
<?php
global $more;
$more = 0;
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
So what am I doing wrong?
Your global $more example looks good, and almost follows the recommendation on the documentation page applicable (the global $more line itself is supposed to be outside "the loop" - before while ( have_posts() ) - leaving the next line where it is.)
However, I couldn't get this to work on my own blog (using a a child theme of Coraline) until I moved both lines to before "the loop". Starting with your code, I'd suggest something like this:
<?php
global $more;
$more = 0;
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
Ok so I am having issues with linking to next and previous posts...
Here is my code:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<div id="project-prev"> <?php previous_post_link('Prev'); ?> </div>
<div id="project-next"> <?php next_post_link('Next'); ?> </div>]
...
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php get_footer(); ?>
I have read places that next/prev posts requires a 'new WP_Query' query, but have had no such luck. There is no next/prev link rendered out on my site, using the above.
As always appreciate solutions and pointers.
Many thanks
Have you tried following (according to Wordpress codex)
<?php next_post_link('<strong>%link</strong>'); ?>
<?php previous_post_link('<strong>%link</strong>'); ?>
In your divs ... :) If you still encouter problems, then just try something like:
<?php echo get_previous_posts_link('Prev'); ?>
<?php echo get_next_posts_link('Next'); ?>
Should work.
EDIT:
<div id="project-prev"><?php previous_post_link('%link', 'PREV'); ?></div>
<div id="project-next"><?php next_post_link('%link', 'NEXT'); ?></div>
First try and get the next and previous posts.
<?php
$previous_post_url = get_permalink(get_adjacent_post(false, '', true));
$next_post_url = get_permalink(get_adjacent_post(false, '', false));
?>
And then create to <a> tags and echo out the URLs we set above.
<?php if ( $previous_post_url != get_the_permalink() ) : ?>
Previous Project
<?php endif; ?>
<?php if ( $next_post_url != get_the_permalink() ) : ?>
Next Project
<?php endif; ?>
One reason that could make these links not to show is having the posts set as draft. Only published posts will make the previous and next post links render.