I'm trying to remove the date from pages only in search results.
I found this: https://wordpress.org/support/topic/search-results-hide-date-for-pages-not-posts - however, I can't seem to figure out where to add it without causing errors.
I also found an answer suggesting just removing the date code from page.php but I don't have that in there anyway.
My search.php:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<div id="content">
<div class="article">
<?php
$wp_query->query_vars["posts_per_page"] = 16;
$wp_query->get_posts();
?>
<?php if ( have_posts() ) : ?>
<?php
global $wp_query;
$total_results = $wp_query->found_posts;
?>
<?php printf( __( 'Search results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?>
<br/><br/>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="searchresultsdate">
<?php the_time('M j, Y') ?> </div><div class="searchresults"><?php the_title(); ?></div>
<?php endwhile; ?>
</div>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { ?>
<div class="right"><?php next_posts_link('Next Page »') ?></div>
<div class="left"><?php previous_posts_link('« Previous Page') ?></div>
<?php } ?>
<br><br><br>
<?php else : ?>
<div class="posttitle">Nothing found. Try something else?</div>
This page doesn't exist
<?php endif; ?>
<?php get_footer(); ?>
Change
<div class="searchresultsdate">
<?php the_time('M j, Y') ?>
</div>
to
<?php if ("page" != get_post_type()){ ?>
<div class="searchresultsdate">
<?php the_time('M j, Y'); ?>
</div>
<?php } ?>
Also you're missing ';' in many places.
Related
I've been cobbling together bits and pieces of various examples together but can't seem to wrap my head around this.
I have regular blog posts (News) that I would like displayed with the most recent on top followed by a custom post type (Businesses) that I would like grouped below the News. I am using Sage and this is my first theme.
Here is my beginner code so far:
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert alert-warning">
<?php _e('Sorry, no results were found.', 'sage'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<?php $type = get_post_type(); ?>
<?php if ($type == 'post') { ?>
<h2>News Results</h2>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?>
</a>
</div>
<div>
<h3><?php the_title(); ?></h3>
<div class="result-excerpt">
<?php if ( has_excerpt( $post->ID ) ) {
echo the_excerpt();
} else {
echo get_excerpt();
} ?>
</div>
</div>
</article>
<?php } elseif ($type == 'business') { ?>
<h2>Business Results</h2>
<article <?php post_class(); ?>>
<header>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?> -
<?php the_title(); ?>
</a>
<h3><?php echo the_sub_field('title'); ?></h3>
<?php if( get_sub_field('content') ): ?>
<div class="result-excerpt">
<?php echo custom_field_excerpt(); ?>
</div>
<?php endif; ?>
</article>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
I think the problem is that you're looping over every post, in order, and deciding what to do with it. This will result in the output, whatever it is, in the same order as the input.
I don't speak very fluent PHP, but I think what's happening is that the algorithm reads like this:
-for each post:
- if it's a 'post', display it like «this»
- otherwise, if it's a 'business', display it instead like «this»
I think you want to have two loops, like this:
-for each post:
- if it's a 'post', display it like «this»
-for each post:
- if it's a 'business', display it like «this»
Unfortunately, I don't see an obvious reference to store in an array and loop over. I really don't know how sage works, so I'll have to leave the implementation to you. My best guess - and I sincerely doubt this will work - is as follows:
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert alert-warning">
<?php _e('Sorry, no results were found.', 'sage'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<?php $type = get_post_type(); ?>
<?php if ($type == 'post') { ?>
<h2>News Results</h2>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?>
</a>
</div>
<div>
<h3><?php the_title(); ?></h3>
<div class="result-excerpt">
<?php if ( has_excerpt( $post->ID ) ) {
echo the_excerpt();
} else {
echo get_excerpt();
} ?>
</div>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php if (!have_posts()) : ?>
<div class="alert alert-warning">
<?php _e('Sorry, no results were found.', 'sage'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<?php $type = get_post_type(); ?>
<?php if ($type == 'business') { ?>
<h2>Business Results</h2>
<article <?php post_class(); ?>>
<header>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?> -
<?php the_title(); ?>
</a>
<h3><?php echo the_sub_field('title'); ?></h3>
<?php if( get_sub_field('content') ): ?>
<div class="result-excerpt">
<?php echo custom_field_excerpt(); ?>
</div>
<?php endif; ?>
</article>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
Good luck!
I made my own search form page and search page, But know when I search for something the search result gives back nothing I searched alot on the web and following some tutorials to find the issue but I can't. Her's the code:
searchform.php:
<form role="search" method="get" class="visible-lg search-form arrow_box search-back navbar-form navbar-right" action="<?php echo home_url( '/' ); ?>">
<div class="form-group">
<input type="search" class="search-field form-control" size="49" placeholder="بگەڕێ" value="" name="s" title="بگەڕێ" />
</div>
<input type="submit" class="search-submit search-button btn btn-default" value="بگەڕێ" />
</form>
search.php:
<?php
/*
Template Name: Search Page
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-md-5 news">
<?php if ( have_posts() ) : ?>
<?php
global $wp_query;
$total_results = $wp_query->found_posts;
?>
<?php printf( __( 'ئەنجامەکانی گەڕان بۆ: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
After I tried many codes I finally found that I have to add the title or the content after the while loop, I don't know why but this is the solution I just changed the search.php:
<?php
/*
Template Name: Search Page
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-md-5 news">
<?php if ( have_posts() ) : ?>
<?php
global $wp_query;
$total_results = $wp_query->found_posts;
?>
<?php printf( __( 'serch results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?>
<br/><br/>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h4 class=""><?php the_title(); ?></h4>
<div class="naskh"><?php the_excerpt(); ?></div>
<?php endwhile; ?>
<?php else : ?>
<h4>Nothing found try something else.</h4>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
I'm having some trouble witin one of my page templates for my Wordpress theme. I wish to list all post but exclude those from one category (id: 4).
Below I have used query_posts() as per the Codex documentation:
<?php get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<header
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
echo 'style="background-image:url(' . $large_image_url[0] . ')"';
}
?>
>
<div class="row">
<div class="page-title small-12 large-8 columns">
<h1>
<?php the_title(); ?>
</h1>
</div>
</div>
</header>
<?php get_template_part( 'content', 'headerbanner' ); ?>
<?php endwhile;?>
<section class="container main-content">
<div class="row">
<div id="content" class="small-12 columns">
<?php query_posts($query_string . '&cat=-4'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
</div>
</div>
</section>
<?php get_footer(); ?>
The problem is that this does not return a list of the posts. Instead it only returns the details for the page itself (in this case called "blog").
Amending the code from inside #content to the following solved this problem:
<?php query_posts('cat=-4'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
here you need to pass post_type
query_posts($query_string . '&cat=-4&post_type=post');
I have a wordpress site : There is a problem with the pagination.. When I click on 2'nd page it shows up the first one(index page). The other pages are working. Can you help me?
I looked up for errors in the loop but i didn't found anything!
this my index.php code
<?php get_header(); ?>
<div id="content">
<?php if(get_option('freshlife_featured_content_enable') == 'on') { ?>
<div id="featured-content">
<div class="heading">
<span class="heading-text"><?php _e('Featured Articles', 'themejunkie'); ?></span>
</div> <!-- end .heading -->
<ul>
<?php
$counter = 1;
query_posts( array(
'showposts' => get_option('freshlife_featured_post_num'),
'tag' => get_option('freshlife_featured_post_tags')
) );
if( have_posts() ) : while( have_posts() ) : the_post();
?>
<li class="featured-<?php echo $counter; ?>"><?php the_post_thumbnail('featured-thumb', array('class' => 'entry-thumb')); ?><span class="entry-date"><abbr title="<?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?>"><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . __(' ago', 'themejunkie'); ?></abbr></span><h2 class="entry-title"><?php the_title(); ?></h2></li>
<?php $counter++; endwhile; endif; wp_reset_query(); ?>
</ul>
</div> <!-- end #featured-content -->
<?php } ?>
<div class="heading">
<span class="heading-text"><?php _e('All Stories', 'themejunkie'); ?></span>
</div> <!-- end .heading -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php include(TEMPLATEPATH. '/includes/templates/loop.php'); ?>
<?php endwhile; ?>
<div class="clear"></div>
<?php if (function_exists('wp_pagenavi')) wp_pagenavi(); else { ?>
<div class="pagination">
<div class="left"><?php previous_posts_link(__('Newer Entries', 'themejunkie')) ?></div>
<div class="right"><?php next_posts_link(__('Older Entries', 'themejunkie')) ?></div>
<div class="clear"></div>
</div> <!-- end .pagination -->
<?php } ?>
<?php else : ?>
<?php endif; ?>
</div> <!-- end #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
It's hard to say without looking at the WP_Query generated by the requests.
But I do see something that might be causing problems, query_posts alters the main query, that could be messing with the pagination.
Try to use get_posts instead query_posts, so the main loop is not affected.
A more detailed explanation can be found here
I have to add a few links to where the taxonomy terms are displaying, I am using a custom module. I tried hook_link but it add links at the end of the node, How can I add links to the right side of the node title
Thank you very much
To extend Scott's answer:
you can still use your custom module with hook_link(), but you need to edit node.tpl.php or node-type.tpl.php.
i.e. the Garland node.tpl.php looks like:
<?php
// $Id: node.tpl.php,v 1.5 2007/10/11 09:51:29 goba Exp $
?>
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
<?php print $picture ?>
<?php if ($page == 0): ?>
<h2><?php print $title ?></h2>
<?php endif; ?>
<?php if ($submitted): ?>
<span class="submitted"><?php print $submitted; ?></span>
<?php endif; ?>
<div class="content clear-block">
<?php print $content ?>
</div>
<div class="clear-block">
<div class="meta">
<?php if ($taxonomy): ?>
<div class="terms"><?php print $terms ?></div>
<?php endif;?>
</div>
<?php if ($links): ?>
<div class="links"><?php print $links; ?></div>
<?php endif; ?>
</div>
</div>
what you need to do is move the <?php if ($links): ... block somewhere before <?php if ($submitted): ...
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
<?php print $picture ?>
<?php if ($page == 0): ?>
<h2><?php print $title ?></h2>
<?php endif; ?>
<?php if ($links): ?>
<div class="links"><?php print $links; ?></div>
<?php endif; ?>
<?php if ($submitted): ?>
<span class="submitted"><?php print $submitted; ?></span>
<?php endif; ?>
...
then float both the title and the links block, for example.
you could theme nodes yourself (i.e., create your own node.tpl.php or 'node-type.tpl.php') and add whatever you want after the $terms variable (or anywhere).