Wordpress search page loading on homepage - php

I'm pretty new to Wordpress and I've been building a theme with reverie-master but the only problem is the search page. All the other pages, category, archive, etc are working.
I use this for searching in my header.php :
<form method="post" action="<?php echo home_url('/'); ?>" class="search-form" role="search">
<div class="small-9 columns no-pad">
<input type="text" class="search-input" name="s" placeholder="Search" value="" />
</div>
<div class="small-3 columns no-pad">
<input class="btn-submit postfix search-input" type="submit" value=""/>
</div>
This shows the search results on the homepage. If I type in "ok" in the search bar it takes me to example : www.homepage.com when it should be on www.homepage.com/?s=ok. The problem with it being on the home page, the paginations will take you to page 2 of the home blog feed instead on www.homepage.com/page/2/ when it should be something ilke this www.homepage.com/page/2/?s=test
When I manualy type in this url in the browser : www.homepage.com/page/2/?s=test the pagination is working. My problem is the pagination and I am wondering why the posts displays on the homepage(which I assume is the problem)
Here is my search.php code :
Starts with <?php get_header(); ?> and ends with <?php get_footer(); ?>
This is the loop in between with pagination :
<h2><?php _e('Search Results for', 'reverie'); ?> "<?php echo get_search_query(); ?>"</h2>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="row pad-row">
<div class="large-11 columns bor-bottom">
<a class="fade" href="<?php the_permalink(); ?>"><p class="feed-title"><?php the_title(); ?></p></a>
<p class="feed-info">By <span class="feed-author"><?php the_author(); ?> </span> • <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?> •
<?php
foreach((g_the_category()) as $category) {
if ($category->cat_name != 'featured') {
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
}
}
?>
• <span class="feed-cmt"><?php comments_number( '0 Comments', '1 Comments', '% Comments' ); ?>. </span></p>
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; // end have_posts() check ?>
<!-- Start Pagination -->
<div class="row pad-row">
<div class="large-11 columns pagination-centered large-centered panel-page">
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( function_exists('reverie_pagination') ) { reverie_pagination(); } else if ( is_paged() ) { ?>
<nav id="post-nav">
<div class="post-previous"><?php next_posts_link( __( '← Older posts', 'reverie' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( 'Newer posts →', 'reverie' ) ); ?></div>
</nav>
<?php } ?>
</div>
</div>

Do not put search form directly. WP has a functionality to get search form called get_search_form(). This function prints wp basic search form, but still you can customize this. In order to customize, you can use a filter called get_search_form.
function custom_search_form( $form ) {
$form = '<form method="get" action="' . home_url('/') . '" class="search-form" role="search">
<div class="small-9 columns no-pad">
<input type="text" class="search-input" name="s" id="s" placeholder="Search" value="' . get_search_query() . '" />
</div>
<div class="small-3 columns no-pad">
<input class="btn-submit postfix search-input" id="searchsubmit" type="submit" value="'. esc_attr__( 'Search' ) .'"/>
</div>
</form>';
return $form;
}
add_filter( 'get_search_form', 'custom_search_form' );
And put this to functions.php

Related

Make background image clickable with other links and elements inside the same div

Right now the code below generates the content seen here:
Currently the title has to be clicked for the post page to be loaded, but I want to allow the entire background image to be clickable. Is this possible? I've tried surrounding the card div with the tag from the title code, but that still did not let me click the background image like a link.
<article id="post-<?php the_ID(); ?>" <?php post_class('card-box col-lg-4 col-md-6 col-sm-12 col-xs-12'); ?>>
<div class="card" data-background="image" data-src="<?php esc_url( the_post_thumbnail_url( 'large' ) ); ?>">
<div class="header">
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
?>
<div class="category">
<h6>
<span class="category">
<?php echo '<a class="category" href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>'; ?>
</span>
</h6>
</div>
<?php } ?>
</div>
<div class="content">
<?php the_title( '<h4 class="entry-title">', '</h4>' ); ?>
<span class="date"><?php echo esc_html( get_the_date() ); ?></span>
</div>
<div class="filter"></div>
</div> <!-- end card -->
</article>
Try jQuery approach like below
$('.card').on('click', function(event){
var elementTag = event.target.tagName.toLowerCase();
if(elementTag === 'div') {
var pageurl = $(this).find('.entry-title a').attr('href');
window.location.href = pageurl;
}
});`

Blank page after replying to a comment in Wordpress

I'm building a Wordpress theme of my own, and having a problem with my comment-page. I can post comments, but when I press the reply-button to reply to my own comment, I get a blank wp-comments-post.php - page when posting my reply. I've tried some solutions, but it's still not working. Any ideas?
This is my comments.php file :
<?php
/**
* The template for displaying Comments.
*
* The area of the page that contains comments and the comment form.
*
*/
/*
* If the current post is protected by a password and the visitor has not yet
* entered the password we will return early without loading the comments.
*/
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die('Please do not load this page directly. Thanks!');
if (post_password_required()) {
?>
<p class="nocomments">Det här inlägget är lösenordsskyddat. Knappa in lösenordet för att se inlägget.</p>
<?php
return;
}
?>
<div id="commentsbox" class="post">
<?php if (have_comments()) : ?>
<h3 id="comments">
<?php comments_number('Inga kommentarer',
'En kommentar',
'% kommentarer'); ?>
än så länge.</h3>
<ol class="commentlist">
<?php wp_list_comments(array(
'avatar_size' => 70)); ?>
</ol>
<div class="comment-nav">
<div class="alignleft">
<?php previous_comments_link() ?>
</div>
<div class="alignright">
<?php next_comments_link() ?>
</div>
</div>
<?php else : ?>
<?php if (comments_open()) : ?>
<?php else : ?>
<p class="nocomments">Kommentarer är avstängda</p>
<?php endif; ?>
<?php endif; ?>
<?php if (comments_open()) : ?>
<div class="commentform_wrapper">
<div class="post-info">
</div>
<div id="comment-form">
<?php $comment_args = array( 'title_reply'=>'Vänligen lämna dina synpunkter och kommentarer:',
'title_reply_to' => __( 'Svara %s' ),
'cancel_reply_link' => __( 'Stäng svar' ),
'label_submit'=>'Skicka',
'reply_text' => 'Svara',
'fields' => apply_filters( 'comment_form_default_fields', array(
'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Namn' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) .
'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
'email' => '<p class="comment-form-email">' .
'<label for="email">' . __( 'Mejl' ) . '</label> ' .
( $req ? '<span>*</span>' : '' ) .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' />'.'</p>',
'url' => '' ) ),
'comment_field' => '<p>' .
'<label for="comment">' . __( 'Kommentarer' ) . '</label>' .
'<textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>' .
'</p>',
'comment_notes_after' => '',
);
comment_form($comment_args); ?>
</div>
</div>
<?php endif; ?>
</div>
And this is my single.php file, in case you need it:
<?php
/**
* The Template for displaying all single posts.
*/
get_header('header3'); ?>
<section id="headerbox">
<header>
<h2 class="referensrubrik">Rubrik</h2>
</header>
<p class="referenstext">Text</p>
</section>
<?php
$post = $wp_query->post;
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article id="blogpost2" id="post-<?php get_the_ID(); ?>" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<h5><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></h5>
<?php the_content(); ?>
<hr>
</article>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Äldre inlägg'); ?></div>
<div class="next"><?php previous_posts_link('Nyare inlägg »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Äldre inlägg'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
<!--Start Comment box-->
<?php comments_template(); ?>
<!--End Comment box-->
<?php get_footer(); ?>
Most of the time this is an (500) error:
Enable debugging (see https://codex.wordpress.org/Debugging_in_WordPress)
Open your develop bar in your browser
Record the requests
Check the http status code and error info
This typically only happens to me when I make a mistake with either leaving a line break or space in front of the pages opening PHP tag. While it does not looks not be the case based on the example code - but I would triple check that you have no carriage returns or spaces before the opening
Problem solved! Because I could comment and reply comments when logged in as admin, but not as an anonymous visitor, I cheched my discussion settings. They were set to send me an email every time someone posted a comment. Somehow this is failing and when I uncheched the box the problem was gone. I need to look up why the email is failing, but for now I'm just happy that everything is working as it should.
Some of this problem are from the server where you are hosted, you may me able to change it on the php settings,
post_max_size
, and the
upload_max_filesize
this will the solution of many problem uploading files on wordpress

Wordpress returns white page when searching

Whenever I search on my site I get an blank error page. I have tried the following:
Disable plugin.
Turn on debug.
Scanned my code with theme checker for deprecated tags.
Search.php
get_header(); ?>
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header><!-- .page-header -->
<?php shape_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php shape_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
</div><!-- #content .site-content -->
</section><!-- #primary .content-area -->
Searchform.php
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search">
<label for="s" class="assistive-text"><?php _e( 'Search', 'shape' ); ?></label>
<input type="text" class="field" name="s" value="<?php echo esc_attr( get_search_query() ); ?>" id="s" placeholder="<?php esc_attr_e( 'Search …', 'shape' ); ?>" />
<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'shape' ); ?>" />
</form>
The weird thing is that all my html show up when searching, but I am still getting a white page of death.
What am I doing wrong?
All code is readable with CTRL+U. So your WordPress works. One of the problems is the display:none value in the CSS under .search class. So delete the .search { display:none; } line from CSS.

My search form is redirecting to the homepage. How to solve?

Everytime when I try to search on my website, it keeps returning to the homepage. I have a search form though, built-in with my theme. Can't find the problem. Can you help me?
<?php get_header(); ?>
<div class="content">
<?php tie_breadcrumbs(); ?>
<div class="page-head">
<h2 class="page-title">
<?php if ( have_posts() ) : ?>
<?php printf( __( 'Search Results for: %s', 'tie' ), '<span>' . get_search_query() . '</span>' ); ?>
<?php else : ?>
<?php _e( 'Nothing Found', 'tie' ); ?>
<?php endif; ?>
</h2>
<div class="stripe-line"></div>
</div>
<?php if ( have_posts() ) : ?>
<?php get_template_part( 'loop', 'search' ); ?>
<?php if ($wp_query->max_num_pages > 1) tie_pagenavi(); ?>
<?php else : ?>
<div id="post-0" class="post not-found post-listing">
<div class="entry">
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'tie' ); ?></p>
</div>
</div>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
This is the website: http://plugr.nl
Addition: The HTML
<?php if(tie_get_option( 'top_right' ) == 'search'): ?>
<div class="search-block">
<form method="get" id="searchform" action="<?php echo home_url(); ?>/">
<button class="search-button" type="submit" value="<?php if( !$is_IE ) _e( 'Search' , 'tie' ) ?>"></button>
<input type="text" id="s" name="s" value="<?php _e( 'Search...' , 'tie' ) ?>" onfocus="if (this.value == '<?php _e( 'Search...' , 'tie' ) ?>') {this.value = '';}" onblur="if (this.value == '') {this.value = '<?php _e( 'Search...' , 'tie' ) ?>';}" />
</form>
</div><!-- .search-block /-->
Looks like this search URL is what your template is expecting:
http://plugr.nl/?s=waar
Your form has code like:
<form method="get" id="searchform" action="http://plugr.nl/">
<input type="text" name="inputBox" placeholder="Typ wat je zoekt en druk op enter">
</form>
Change the input name to "s" and see what that does for you.
You might need to grep through the files in your template to find where this form is actually generated. Nested template files will not show up in the wordpress GUI editor. It's also possible your search form is provided through a widget which the template depends on, so you might need to dig a little further.
You need to place "s" for the name value...name="s". Its a default setting for wordpress which will detect this variable for what kind of search you did.. /?s="whatever you searched for"

Wordpress exclude search page from empty search

I've created a search page and using the loop return results based on users input. Problem is when I go to the search page it displays the search page title when the user hasn't entered anything. Is there a way I can remove the search page from the results? I've tried plugins but they don't work - am convinced there is something wrong with my loop but I don't see it.
My code is:
<?php
/*
Template Name: Search Page
?>
<?php get_header(); ?>
<section id="post-<?php the_title(); ?>" class="search-section left <?php post_class(); ?>" role="main">
<div class="search-input">
<form action="<?php echo home_url(); ?>" method="get" role="search" class="search-big">
<input type="text" name="s" id="s" class="search-big" placeholder="Type in your search and press enter..." />
</form>
</div>
<div class="search-result">
<?php if ( have_posts() ) : ?>
<h2><?php if( !empty( $_GET) ) { printf( __( 'Search results for: %s', 'xma' ), '<span>' . get_search_query() . '</span>' ); } ?></h2>
<?php while ( have_posts() ) : the_post(); ?>
<div class="search-single-result">
<a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p></a>
</div>
<?php endwhile; ?>
<?php else : ?>
<h3><?php _e('Sorry, we couldn\'t find anything that matched your search.', 'xma' ); ?></h3>
<?php endif; ?>
</div>
</section>
<aside class="search-sidebar right">
<?php if ( is_active_sidebar( 'sidebar-4' ) ) : ?>
<?php dynamic_sidebar( 'sidebar-4' ); ?>
<?php endif; ?>
</aside>
screenshot of bug thats happening:
Basically from my understand, you want it so that if the user goes to the search page, they don't see a title on the page that says 'Search Results for: ' with nothing after the colon. I have a proposed code change that will eliminate the title, and allow you to put a message for the user to search for something, to display results.
<?php
/*
Template Name: Search Page
?>
<?php get_header(); ?>
<section id="post-<?php the_title(); ?>" class="search-section left <?php post_class(); ?>" role="main">
<div class="search-input">
<form action="<?php echo home_url(); ?>" method="get" role="search" class="search-big">
<input type="text" name="s" id="s" class="search-big" placeholder="Type in your search and press enter..." />
</form>
</div>
<div class="search-result">
<?php /* ADD THIS CODE */>
<?php $search_term = get_search_query(); ?>
<?php if (!empty($search_term)): /* if search term is not empty */ ?>
<?php /* END ADD THIS CODE */>
<?php if ( have_posts() ) : ?>
<h2><?php if( !empty( $_GET) ) { printf( __( 'Search results for: %s', 'xma' ), '<span>' . get_search_query() . '</span>' ); } ?></h2>
<?php while ( have_posts() ) : the_post(); ?>
<div class="search-single-result">
<a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p></a>
</div>
<?php endwhile; ?>
<?php else : ?>
<h3><?php _e('Sorry, we couldn\'t find anything that matched your search.', 'xma' ); ?></h3>
<?php endif; ?>
<?php /* ADD THIS CODE */>
<?php else: /* if search term is empty */ ?>
<p>Please enter some search text to display search results.</p>
<?php endif; ?>
<?php /* END ADD THIS CODE */>
</div>
</section>
<aside class="search-sidebar right">
<?php if ( is_active_sidebar( 'sidebar-4' ) ) : ?>
<?php dynamic_sidebar( 'sidebar-4' ); ?>
<?php endif; ?>
</aside>
There are two code blocks to add, one up top, and one down low. Hope this helps!
EDIT (after clarification)
After we talked, it looks like the problem is related to the fact that you have a custom URL for the search page, something like '/search', which is actually a WordPress page labeled 'Search'. The problem is that when you load this search page, your loop already has one result in it, the current page. What you need to do is first check if your current loop is for the search, or if it is for the display of the search page. Do this like so:
<?php
/*
Template Name: Search Page
?>
<?php get_header(); ?>
<section id="post-<?php the_title(); ?>" class="search-section left <?php post_class(); ?>" role="main">
<div class="search-input">
<form action="<?php echo home_url(); ?>" method="get" role="search" class="search-big">
<input type="text" name="s" id="s" class="search-big" placeholder="Type in your search and press enter..." />
</form>
</div>
<div class="search-result">
<?php /* CHANGE THESE LINES FROM ABOVE, KEEP LOWER LINES */ ?>
<?php global $wp_query; ?>
<?php if (!empty($wp_query->query_vars['s'])): ?>
<?php /* END CHANGE THESE LINES FROM ABOVE, KEEP LOWER LINES */ ?>
<?php if ( have_posts() ) : ?>
<h2><?php if( !empty( $_GET) ) { printf( __( 'Search results for: %s', 'xma' ), '<span>' . get_search_query() . '</span>' ); } ?></h2>
<?php while ( have_posts() ) : the_post(); ?>
<div class="search-single-result">
<a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p></a>
</div>
<?php endwhile; ?>
<?php else : ?>
<h3><?php _e('Sorry, we couldn\'t find anything that matched your search.', 'xma' ); ?></h3>
<?php endif; ?>
<?php /* ADD THIS CODE */>
<?php else: /* if search term is empty */ ?>
<p>Please enter some search text to display search results.</p>
<?php endif; ?>
<?php /* END ADD THIS CODE */>
</div>
</section>
<aside class="search-sidebar right">
<?php if ( is_active_sidebar( 'sidebar-4' ) ) : ?>
<?php dynamic_sidebar( 'sidebar-4' ); ?>
<?php endif; ?>
</aside>
Keep the lower lines, and change those top lines to what I have. This is going to check that your loop is actually the loop that has the results of the search in it. If it is, then it draws the results, if not, then it displays the message below.
Try this:
<div class="search-input">
<form action="<?php echo home_url(); ?>" method="get" role="search" class="search-big">
<input type="text" name="s" id="s" class="search-big" placeholder="Type in your search and press enter..." />
</form>
</div>
<div class="search-result">
<h2><?php if( !empty( $_GET) ) { printf( __( 'Search results for: %s', 'xma' ), '<span>' . get_search_query() . '</span>' ); } ?></h2>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="search-single-result">
<a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p></a>
</div>
<?php endwhile; ?>
<?php else : ?>
<h3><?php _e('Sorry, we couldn\'t find anything that matched your search.', 'xma' ); ?></h3>
Try this method which I found WordPress StackExchange
$exclude_pages = array('music', 'contact');
$exclude_post_types = array('music', 'any_here');
if ( have_posts() ) : while ( have_posts() ) : the_post();
if ( is_page() && ! empty($exclude_pages) && (
in_array($post->post_name, (array)$exclude_pages) ||
in_array($post->post_title, (array)$exclude_pages)
) ||
( is_single() && in_array(get_post_type(), (array)$exclude_post_types) ) ) continue;
Let me know if it works, or need improvement

Categories