Wordpress search results - php

I have created a searchfrom.php for wordpress but then it's giving me a false returns you can try the search yourself here
Here is the code for my search form
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<input type="text" class="form-text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" class="goField" />
</form>
Here is the code of my search.php page
get_header(); ?>
<div id="BodyWrap">
<div id="mainCont">
<?php get_sidebar(); ?>
<div id="mainCopy">
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Search Results</h2>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?>>
<h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>
<small><?php the_time('l, F jS, Y') ?></small>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php else : ?>
<h2 class="center">No posts found. Try a different search?</h2>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
It does do the search but there are this "at | Uncategorized | No comment" which is not even part of the search term.

Oh, I see. You want to know why it lists categories even if they don't match the search term?
Well, that's the default WordPress behavior. I guess you could change it, but I don't see why, actually.
It is the
Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
part in your search.php that displays the categories and comments count. However, if you remove the code above, the categories and comments count will not be shown (even if the categories matches your search term).
You may replace the code above with this code, if you only want to display the categories if they are the same as the search term:
<?php
foreach(get_the_category() as $cat){
if(strtolower($cat->cat_name)==strtolower($_GET['s'])){
$match = true;
}
}
if($match){ echo 'Posted in '; }
foreach(get_the_category() as $cat){
if(strtolower($cat->cat_name)==strtolower($_GET['s'])){
echo $cat->cat_name;
echo ', ';
}
}
edit_post_link('Edit', '', ' | ');
comments_popup_link('No Comments »', '1 Comment »', '% Comments »');
?>
It should produce something like this, if the categories match the search term:
Posted in Category That Matches, Edit | 1 Comment
If the categories don't match the term:
Edit | 1 Comment
If you don't want the comments count (or Comments Off) to display, remove the comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); line from my code.

It does do the search but there are
this "at | Uncategorized | No comment"
which is not even part of the search
term.
You'll have to edit your page.php or single.php to exclude the search page from showing the Comments/Categories.

Maybe change searchform.php to this?

Remove the line that's wrapped in . That will get rid of the "Posted in Uncategorized | Comments Off".
The reason it show's is because it is the meta information for your search result. It will be displayed in your results even if it wasn't the search term because it is associated with it.
All in all, your search.php page should look like this:
get_header(); ?>
<div id="BodyWrap">
<div id="mainCont">
<?php get_sidebar(); ?>
<div id="mainCopy">
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Search Results</h2>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?>>
<h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>
<small><?php the_time('l, F jS, Y') ?></small>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php else : ?>
<h2 class="center">No posts found. Try a different search?</h2>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>

Related

Wordpress - When clicking on posted in link, categories aren't filtering

I have a new blog I am building here https://www.storageworks.biz/blog
When on a single post page such as https://www.storageworks.biz/blog/6-must-have-tips-for-moving-into-your-first-apartment/ when I go to the bottom and click on the posted in link to view other posts in the same category, it shows me all posts in place of only the one category type.
How do I set it up to filter out the results?
My code is as follows,
<?php get_header(); ?>
<div id='page_wrapper'>
<div id='page'>
<div id='slider_wrapper' >
<?php get_sidebar(); ?>
<div id='wrapper_right'>
<div id="content">
<div id="nav-above" class="navigation">
<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">«</span> %title' ) ?></div>
<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">»</span>' ) ?></div>
</div><!-- #nav-above -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content(); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php //comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php //comments_template(); ?>
<?php endwhile; endif; ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">«</span> %title' ) ?></div>
<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">»</span>' ) ?></div>
</div><!-- #nav-below -->
</div>
<div style="clear:both;"></div>
</div>
<div class="clear"></div>
</div>
</div>
<div class="clear"></div>
<?php get_footer(); ?>
I'm running worpress version 4.5.3.
My template parts are made up of:
Header
index
single
sidebar
footer
functions
Thanks!

im trying to remove the double post image

when you go to:
http://dageniusmarketer.com/
you will see the posts have 2 images. I'm trying to remove the one on the bottom.
I initially added
<?php the_post_thumbnail( $size, $attr ); ?>
for the main post image, as I wasn't getting the placement I wanted, which was ABOVE the post statistics. Now that I have it above the statistics, I still have the original image, and I want to remove it.
I'm trying to play with the class .wp-image,
but that isn't giving me any sort of luck. I just want to remove the 2nd duplicate image underneath the statistics and keep whatever other images I may wish to write into my post body in the future. How do I do this?
edit: here is my php code:
<?php get_header(); ?>
<div id="main">
<div id="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- item -->
<div class="item entry" id="post-<?php the_ID(); ?>">
<div class="itemhead">
<h1>
<?php the_title(); ?>
</h1>
<?php the_post_thumbnail( $size, $attr ); ?>
<div class="postStatsContainer">
<div class="postViews">100 Views</div>
<div class="slash"></div>
<div class="postShares">100 Shares</div>
<div class="slash"></div>
<div class="postComments">100 Comments</div>
<div class="slash"></div>
<div class="date"><?php the_time('M jS, Y') ?></div>
</div>
<div style="clear: both"></div>
<!--<div class="readMore_Container">-->
<?php the_content('Read More »'); ?>
<div class="postCounter">
<i class="fa fa-pencil-square-o fa-2x"></i>#200
</div>
<div class="metadata" style="clear:both;">
<div class="TagIcon"></div>
Filed under
<span class="category"><?php the_category(', ') ?></span> |
<?php edit_post_link('Edit', '', ' | '); ?>
<?php comments_popup_link('Comment (0)', ' Comment (1)', 'Comments (%)'); ?>
</div>
<div style="clear:both;"></div>
<div style="clear:both;"></div>
</div>
</div>
<!-- end item -->
<?php comments_template(); // Get wp-comments.php template ?>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
<p> </p>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
<!-- end content -->
</div>
<div id="primary">
<?php include(TEMPLATEPATH."/l_sidebar.php");?>
</div>
<div id="secondary">
<?php include(TEMPLATEPATH."/r_sidebar.php");?>
</div>
<?php get_footer(); ?>

WordPress - pagination on template page?

I'm building my site with WordPress templates for granular control over how each of them looks.
I have my loop on my news page to pull in posts from, well, posts.
Working fine, or so I thought.
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<div id="main-content">
<?php get_sidebar(); ?>
<?php query_posts ("posts_per_page=4"); ?>
<?php if (have_posts()) : while ( have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content(); ?>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
Posted in <?php the_category(', ') ?> |
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
</div> <!-- end div main-content -->
<?php get_footer(); ?>
The older/newer entries show up, but after clicking on them it just takes me to the same page/newest 4 posts.
How do I get the pagination working with template pages like this?
You need to add the paged parameter to query_posts() to paginate the results:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=3&paged=' . $paged);
?>
http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
Just put this line in your page.

PHP Wordpress loop, how to remove loop and show single iteration instead?

This page shows many recent posts, instead I want it to show only the most recent. How would I remove this loop and replace it with a single iteration of the most recent?
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<span class="postmetadata"><?php the_category(' / ') ?> — <?php edit_post_link('Edit', '', ' — '); ?> <?php comments_popup_link('No comments', '1 comment', '% comments'); ?></span><br/>
<small><span class="date"><?php the_time('d') ?></span><br /><?php the_time('M y') ?> <!-- by <?php the_author() ?> --></small>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content('<em>Continue reading →</em>'); ?>
</div>
<div class="clearfix"></div>
</div>
<?php endwhile; ?>
Well if the posts are already sorted in the correct order and you just want the first one you could change it to:
<?php if (have_posts()): the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<span class="postmetadata"><?php the_category(' / ') ?> — <?php edit_post_link('Edit', '', ' — '); ?> <?php comments_popup_link('No comments', '1 comment', '% comments'); ?></span><br/>
<small><span class="date"><?php the_time('d') ?></span><br /><?php the_time('M y') ?> <!-- by <?php the_author() ?> --></small>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content('<em>Continue reading →</em>'); ?>
</div>
<div class="clearfix"></div>
</div>
<?php endif; ?>
Modify the global $query_string variable:
<?php
global $query_string;
$query_string .= "&posts_per_page=1"; // append the post count limit
query_posts($query_string); // perform the query
?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<span class="postmetadata"><?php the_category(' / ') ?> — <?php edit_post_link('Edit', '', ' — '); ?> <?php comments_popup_link('No comments', '1 comment', '% comments'); ?></span><br/>
<small><span class="date"><?php the_time('d') ?></span><br /><?php the_time('M y') ?> <!-- by <?php the_author() ?> --></small>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content('<em>Continue reading →</em>'); ?>
</div>
<div class="clearfix"></div>
</div>
<?php endwhile; ?>

Wordpress theme problems

I've been making a wordpress theme recently and just now some of the code just stopped working. The post formatting disappeared and now I just have a paragraph with text inside it for each post! Here's the php code for the post:
<?php get_header(); ?>
<div id="content">
<div class="wrap">
<div id="leftcol">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="meta alignleft">
<span class="date"><?php the_time('F j, y') ?></span>
<span class="comments"><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></span>
<span class="tags"><?php the_tags('', ', ', ''); ?></span>
</div>
<div class="body alignright">
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</div>
<div class="clear"></div>
</div>
<?php endwhile; ?>
<ul>
<li><?php next_posts_link('« Older Entries') ?></li>
<li><?php previous_posts_link('Newer Entries »') ?></li>
</ul>
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
</div>
</div>
This is all the code in the index.php file in the theme folder. I have some metadata of the post on the left side of the post and the actual post on the right side.
This is what is outputs to the page. (This is only what is inside the #content div. There was too much for me to post everything.)
<div id="content"><div class="wrap">
<div id="leftcol">
<p>This is some random text that’s added just for effect. Thanks for the consideration. This is some random text that’s added just for effect. Thanks for the consideration. This is some random text that’s added just for effect. Thanks for the consideration. This is some random text that’s added just for effect. Thanks for the consideration. </p>
<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! Welcome to WordPress</p>
</div></div></div>
What happened!?
Do you mean that the only thing that gets processed is <?php the_excerpt(); ?>. Or is everything getting parsed, but you haven't posted the stuff generated by <?php the_time('F j, y') ?> between the other spans?
If the letter is the case, than it's simply a matter of using another template tag. the_excerpt only posts a small portion of a post, limited to a certain amount of characters. If you want to display the whole post, you'll need to use the_content.

Categories