Wordpress exclude search page from empty search - php

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

Related

How do i only display posts with a certain category?

So I'm building a website for my new business.
An I want to only run the following code if the post have a category of 'certain-category'.
How do I do this? I tried a number of things. but they do not work..``
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb">
<a href="<?php the_permalink(); ?>" class="H1-posts" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('moesia-thumb'); ?>
</a>
</div>
<?php endif; ?>
<?php if (has_post_thumbnail()) : ?>
<?php $has_thumb = ""; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="post-content <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
</div><!-- .entry-content -->
Use has_category()
if(has_category('certain-category', get_the_ID())):
// Do something
endif;
WordPress now has a native block which does this for you if you want the easy route the block is called Post and Page Grid it allows you to select what category of posts or pages it will show and you can select what information is shown e.g. Title, thumbnail, excerpt etc

Ajax Load More and conditional loop

I am trying to set up a wordpress. For my blog page, I displayed items differently depending on the position of the article (first line) and content (quote or article with image). It does the job.
<?php $post_counter = 0; // START COUNTDOWN ?>
<?php if ( have_posts() ) : ?>
<?php query_posts('category_name=blog'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_counter++; // COUNTDOWN FOR EACH POST ?>
<!-- IF FIRST POST -->
<?php if ( $post_counter == 1 ) : ?>
<div class="first">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div style="background-image:url('<?php echo $url; ?>')" class="fullimg">
</div>
<a href="<?php the_permalink() ?>">
<?php the_title() ?>
<?php echo get_the_date( 'd/m/Y' ); ?>
</a>
</div>
<?php endif; ?>
<!-- IF NOT FIRST POST -->
<?php if ( $post_counter != 1 ) : ?>
<!-- IF NOT FIRST POST AND IS A QUOTE -->
<?php if ( has_post_format( 'quote' )) : ?>
<div class="quote">
<?php the_content() ?>
<?php echo get_the_date( 'd/m/Y' ); ?>
</div>
<?php endif; ?>
<!-- IF NOT FIRST POST AND IS NOT A QUOTE -->
<?php if ( !has_post_format( 'quote' )) : ?>
<div class="noquote">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div style="background-image:url('<?php echo $url; ?>')" class="fullimg">
</div>
<a href="<?php the_permalink() ?>">
<?php the_title() ?>
<?php echo get_the_date( 'd/m/Y' ); ?>
</a>
</div>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
But now I want to integrate a load more, why I use the plugin "ajax load more". When I insert my hook into the template and the plugin integrates the shortcode, the button appears but nothing happens. Ideas ?

Infinite scrolling of WP categories doesn't load all elements

I set up a site for a photographer & she wanted to use her blog categories as her portfolio, which works fine, except for the fact that the infinite scrolling feature I'm using won't load all of the content for the excerpts.
The two items that it omits are the post thumbnail (image) and the horizontal row serving as a separator between the posts.
Any insight on this is appreciated - I'm using Paul Irish's infinite scroll plugin & the category excerpts are being called like so:
<?php get_header(); ?>
<section id="content" role="main">
<header class="header">
<h1 class="entry-title"><!-- <?php _e( 'Category Archives: ', 'themename' ); ?> --> <?php single_cat_title(); ?></h1>
<?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
</header>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><div class="cat-thumbs align-right"><?php the_post_thumbnail(); ?></div>
<?php get_template_part( 'entry' ); ?>
<hr />
<?php endwhile; endif; ?>
<?php get_template_part( 'nav', 'below' ); ?>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is the entry template:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header>
<?php if ( is_singular() ) { echo '<h1 class="entry-title">'; } else { echo '<h2 class="entry-title">'; } ?><?php the_title(); ?><?php if ( is_singular() ) { echo '</h1>'; } else { echo '</h2>'; } ?><?php edit_post_link(); ?>
<?php if ( !is_search() ) get_template_part( 'entry', 'meta' ); ?>
</header>
<?php get_template_part( 'entry', ( is_archive() || is_search() ? 'summary' : 'content' ) ); ?>
<?php if ( !is_search() ) get_template_part( 'entry-footer' ); ?>
</article>
For reference, this is how I'm calling the excerpts for the blog, which have no issue:
<section class="entry-content">
<div class="cat-thumbs align-right"><?php the_post_thumbnail(); ?></div>
<?php the_excerpt(); ?>
<em><p>
<?php comments_number( '0 comments', '1 comment', '% comments' ); ?>.
</p></em>
<div class="entry-links"><?php wp_link_pages(); ?></div>
</section>
<hr />
Link to example
Move <div class="cat-thumbs align-right"><?php the_post_thumbnail(); ?></div> and <hr> into the entry template.

Wordpress Blog page only showing one post

Im having an issue with a theme i've been customizing. My blog post only shows ( 1 ) post and i cant seem to fix the issue in Admin > Reading > Blog pages show at most. The Value only stays at "1" even after save. The code i have here is in the Loop.php
<?php ?>
<article class="primary-content">
<?php $firstClass='first-post' ; ?>
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article role="main" class="the-content">
<h1><?php _e( '404 - I'm sorry but the page can't be found' ); ?></h1>
<p>Please try searching again or head back to the homepage.</p>
</article>
<?php endif; ?>
<?php ?>
<?php if (is_home()): ?>
<h1>
<?php if ( is_day() ) : ?><?php printf( __( '<span>Daily Archive</span> %s' ), get_the_date() ); ?>
<?php elseif ( is_month() ) : ?><?php printf( __( '<span>Monthly Archive</span> %s' ), get_the_date('F Y') ); ?>
<?php elseif ( is_year() ) : ?><?php printf( __( '<span>Yearly Archive</span> %s' ), get_the_date('Y') ); ?>
<?php elseif ( is_category() ) : ?><?php echo single_cat_title(); ?>
<?php elseif ( is_search() ) : ?><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?>
<?php elseif ( is_home() ) : ?>Blog<?php else : ?>
<?php endif; ?>
</h1>
<?php endif; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php /* How to display standard posts and search results */ ?>
<article class="article-archive <?php echo $firstClass; ?>" id="post-<?php the_ID(); ?>">
<?php $firstClass="" ; ?>
<?php ?>
<?php if (is_front_page()) { ?>
<div class="home-summary">
<?php } else { ?>
<div class="entry-summary">
<?php } ?>
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php the_post_thumbnail( 'flozo-thumb');?>
</a>
<?php if (is_front_page()) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h2><?php the_title(); ?></h2>
<?php } ?>
<?php the_excerpt(); ?>
<?php if ( is_home() ) : ?>
<p class="entry-meta">
<time datetime="<?php the_time('l, F jS, Y') ?>" pubdate>
<?php the_time( 'l jS F Y') ?>
</time>
</p>
<?php endif; ?>
</div>
</article>
<?php /*?>
<?php comments_template( '', true ); ?>
<?php */?>
<?php endwhile; // End the loop. Whew. ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="navigation">
<div class="nav-previous">
<?php next_posts_link( __( 'Older posts' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts' ) ); ?>
</div>
</div>
<!-- #nav-below -->
<?php endif; ?>
</article>
Any and all help with be greatly appreciated! Sorry if this was posted already.
L

Having trouble displaying custom post meta via a short code in a WordPress plugin

I've created a plugin that sets up a custom post type ('Programmes'), taxonomy and a short code to display the custom posts. I'm trying to display a custom post meta field (Broadcast Date) via the shortcode. These are the methods I've tried so far:
This is the method I'd have preferred as it allows me to use it for multiple meta values:
if ( $query->have_posts() ) { ?>
<div class="schedule-container">
<?php while ( $query->have_posts() ) : $query->the_post();
$programmeImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') );
$programmeMeta = get_post_meta($post->ID,'_meta_content',TRUE);
?>
<div id="programme-<?php the_ID(); ?>" <?php post_class(); ?> style="background-image: url( <?php echo $programmeImage ?> )">
<div class="schedule-titlebar">
<h3><?php the_title(); ?></h3>
<p><?php echo $programmeMeta['broadcast-date'] ?></p>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
}
But that didn't work so I tried this instead:
if ( $query->have_posts() ) { ?>
<div class="schedule-container">
<?php while ( $query->have_posts() ) : $query->the_post();
$programmeImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') );
$programmeBroadcastDate = get_post_meta($post->ID,'broadcast-date',TRUE);
?>
<div id="programme-<?php the_ID(); ?>" <?php post_class(); ?> style="background-image: url( <?php echo $programmeImage ?> )">
<div class="schedule-titlebar">
<h3><?php the_title(); ?></h3>
<p><?php echo $programmeBroadcastDate ?></p>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
}
and that doesn't work either!
Any ideas? Thanks

Categories