Below is the code for the Search Results. The search separates page results from post results.
<?php
foreach (array('post','page') as $pt) :
$search_query = new WP_Query(array(
'post_type' => $pt,
's' => $s,
'posts_per_page' => 10,
'paged' => $paged
)
);
?>
<?php if ($pt == 'post') : ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php else : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php endif; ?>
<?php
if ($search_query->have_posts()) : while ($search_query->have_posts()) : $search_query->the_post();
if ($pt == 'post') :
?>
Post Results Code
<?php else : ?>
Page Results Code
<?php endif; ?>
<?php endwhile; else : ?>
No Results
<?php endif; ?>
<?php endforeach; ?>
Using the current code, if no search results are found for either Post/Page Search Results, the phrase "No Results" is displayed below the Post/Page Results header(s). This needs to be preserved.
I want to position the header "Post Search Results" and the "Post Results Code" in one div and the header "Page Search Results" and the "Page Results Code" in a separate div that is set inside another div. Like this:
<div id="A">
<div id="B">Post Results header & Post Results Code</div>
<div id="C">Page Results header & Page Results Code</div>
</div>
Where do I insert the div tags in the code to accomplish this? Thank you!
This should do what you want:
<div id="A">
<?php
foreach (array('post','page') as $pt) :
$search_query = new WP_Query(array(
'post_type' => $pt,
's' => $s,
'posts_per_page' => 10,
'paged' => $paged
)
);
?>
<?php if ($pt == 'post') : ?>
<div id="B">
<h2 class="pagetitle">Post Search Results</h2>
<?php else : ?>
<div id="C">
<h2 class="pagetitle">Page Search Results</h2>
<?php endif; ?>
<?php
if ($search_query->have_posts()) : while ($search_query->have_posts()) : $search_query->the_post();
if ($pt == 'post') :
?>
Post Results Code
<?php else : ?>
Page Results Code
<?php endif; ?>
<?php endwhile; else : ?>
No Results
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
Related
I want to get post into my wordpress homepage, I tried to search lot from google but I am not geting any proper solutions, So if any one have idea please help me.
Thanks in advance.
Here is my tried code so far :
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 8,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content,0,300)." . . . ";
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php
endwhile;
?>
</div>
<div id="page-gina">
<?php
//wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
</div>
Two Ways is possible,
i)Create a custom template.
ii)In your page.php check condition.
Ex:
<?php if(is_front_page()){
echo "This Home";//Here Your code
}
?>
I have this code which works okay for what I wanted but I will probably need to put a condition if there's only 2 posts then wrap it in a <div class="large-6"> then if there's 3 posts then wrap it in large-4.
Just a little confused how to add a condition statement.
<?php
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'showposts' => '3', 'offset' => '1' ) );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="large-4 columns">
<h5><?php the_title(); ?></h5>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail(); ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
To Pieter:
something like this? if so, that didn't work though unless I'm doing it totally wrong and noobishly.
<?php
$query = new WP_Query(array(
'posts_per_page' => '3',
'post_type' => 'portfolio',
'offset' => '1'
));
while ($query->have_posts()): $query->the_post(); ?>
<?php if(!isset ($query->posts[2])){ ?>
<div class="large-6 columns">
<h5><?php the_title(); ?></h5>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail(); ?>
<?php endif; ?>
</div>
<?php } else {?>
<div class="large-4 columns"> </div>
<?php }?>
<?php endwhile; ?>
This is just a theory but should work. Here is the idea:
$loop->posts holds an array with all the posts with all their resepective postdata. So, with that in mind, it should be easy to determine whether you have one, two or three posts in that array by simply checking if a certain array key exists
So you basically needs to check if you have a third post, so you can try something like this
if( !isset( $loop->posts[2] ) ) {
//add div if less than 3 posts
}else{
//add div if you have 3 posts
}
EDIT
Just a note. The OP has changed the query variable from $loop to $query in his original code. $loop should be changed accordingly
You can put conditionals like
<?php if(condition) : ?>
code to execute if condition == true
<?php elseif(condition2) :?> // repeat as many times as necessary
...
<?php else :?>
final else if needed, you can just end the if statement if it's only one condition, if you have else if else this is needed
<?php endif; ?>
That's basically it...
I have to add some simple HTML to the homepage of a client's WordPress website. At first, I added the code to the header.php and everything looked and worked how it should; but, then I realised that it was showing on every page when I just need it on the home page.
So, I tried adding the same HTML after the get_header in the homepage.php and then in the index.php but nothing shows at all when I add the code to these pages.
No matter where I put the HTML in homepage.php or index.php it will not show. I'm new to WordPress as you have no doubt gathered.
Do I need to make a template or use some code to introduce the HTML in either of these two pages? Why does it show in the header.php?
I tried using with no joy:
if ( is_home() ) {
The home page code is here if it helps:
<?php
/**
Template Name: Homepage
*
*/
get_header(); ?>
<?php $general_options = get_option( 'meanthemes_theme_general_options' ); ?>
<?php $content_options = get_option ( 'meanthemes_theme_content_options' ); ?>
<?php if( isset( $general_options[ 'hide_slider' ] ) ) {
// do nothing
} else {
?>
<section class="hero flexslider">
<article class="wrapper">
<div class="flex-container"></div>
<ul class="slides">
<?php
$bannerPortfolio = $general_options[ 'swap_slider' ];
if(!$bannerPortfolio) {
query_posts( array ( 'posts_per_page' => 3 ) );
while ( have_posts() ) : the_post();
?>
<li>
<h2>
<?php the_title(); ?>
</h2>
<div>
<?php echo excerpt_clip(get_the_excerpt(), '28'); ?> <?php _e('[...]','meanthemes'); ?>
</div>
<time class="time" datetime="<?php the_time('Y-m-d', '', ''); ?>"><?php the_time('jS M Y') ?></time>
</li>
<?php endwhile; wp_reset_query(); ?>
<?php
} else {
?>
<?php
query_posts( array( 'post_type' => 'portfolio', 'order' => 'desc', 'posts_per_page' => '3' ) );
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<li>
<hgroup>
<h2>
<?php the_title(); ?>
</h2>
<div>
<?php echo excerpt_clip(get_the_excerpt(), '48'); ?> <?php _e('[...]','meanthemes'); ?></div>
</hgroup>
</li>
<?php endwhile; ?>
<?php } ?>
</ul>
</article><!-- / article.wrapper -->
</section>
<script>
jQuery(window).load(function() {
jQuery('.flexslider').flexslider({
animation: "fade", // animation type fade or slide
slideshowSpeed: <?php echo sanitize_text_field( $general_options ['slider_timer'] ); ?>, // sections between transitions
directionNav: true, // show previous and back
controlNav: false,
controlsContainer: '.flex-container'
});
});
</script>
<?php } ?>
<?php if( $general_options[ 'show_portfolio' ] ) { ?>
<section class="wrapper portfolio">
<article>
<hgroup>
<h2><?php echo balanceTags( $content_options['we_do'] ); ?></h2>
</hgroup>
<div><?php echo balanceTags( $content_options['we_do_summary'] ); ?></div>
</article>
<?php
query_posts( array( 'post_type' => 'portfolio', 'posts_per_page' => '4', 'orderby' => 'date', 'order' => 'desc' ) );
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<aside>
<?php the_post_thumbnail('portfolio-thumb'); ?>
<hgroup>
<h3><?php the_title(); ?></h3>
</hgroup>
<div><?php echo excerpt_clip(get_the_excerpt(), '14'); ?><?php _e('[...]','meanthemes'); ?></div>
</aside>
<?php endwhile; wp_reset_query(); ?>
</section>
<?php } ?>
<script>
jQuery(document).ready(function() {
jQuery('body').addClass("home");
});
</script>
<?php get_footer(); ?>
Howlin's example should work. Another solution is to create a front-page.php in the theme-directory, copy the content from the page.php to it, and add your specific HTML-Code for the homepage (frontpage).
Visit http://codex.wordpress.org/Template_Hierarchy to understand which template is invoked in which case.
Visit http://codex.wordpress.org/Conditional_Tags for Conditional Tags, which you can use e.g. in theheader.php
You might need to try is_front_page instead.
if(is_front_page()){
is_home works if you have blog posts displaying on the homepage and is_front_page if the homepage is set as a static page.
First time creating/implementing custom post types in Thesis, not first time using custom post types.
I used the Reed Write plugin to create the custom post types. The site is using Thesis 1.8.5.
On the following page (http://www.snyderleadership.com/press-releases/) I have the main content getting dropped in with the contents of the custom post type after it.
I used the custom_functions.php file to create a custom page template and call the db for the contents of the custom post type. Here is my code:
/* CUSTOM PRESS RELEASE TEMPLATE - ADDED by BRETT ATKIN */
function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>
<div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post_box">
<div class="headline_area"><h1><?php the_title(); ?></h1></div>
<div class="format_text">
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="press-wrapper">
<div id="press-image">
<?php echo wp_get_attachment_image(get_post_meta($post->ID, 'release_image', true)); ?>
</div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo get_post_meta($post->ID, 'release_date', true); ?></p>
<p class="press-link"><?php echo get_post_meta($post->ID, 'release_title', true); ?></p>
<p class="press-author"><?php echo get_post_meta($post->ID, 'release_author', true); ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_postdata(); ?>
</div><!-- end press-releases div -->
</div>
</div>
</div><!-- end content -->
<?php echo thesis_sidebars(); ?>
<?php } }
add_action('thesis_hook_custom_template', 'press_releases_page');
It seems like everything is working correctly, just not pulling in the data for the custom post type.
Having done this on other sites (using custom themes), I'm not sure if I did something wrong here or if it is a Thesis issue.
Any help would be great.
Thanks
Brett
Here is the final working code. Thanks to the help from of a local WP Guru friend and maiorano84. We didn't figure out the cause, but we did fine a solution.
function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>
<div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post_box">
<div class="headline_area"><h1><?php the_title(); ?></h1></div>
<div class="format_text">
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php $results = get_post_custom(); ?>
<div id="press-wrapper">
<div id="press-image"><?php echo wp_get_attachment_image($results['release_image'][0] ); ?></div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo $results['release_date'][0] ?></p>
<p class="press-link"><?php echo $results['release_title'][0] ?></p>
<p class="press-author"><?php echo $results['release_author'][0] ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_query(); wp_reset_postdata(); ?>
</div><!-- end press-releases div -->
</div>
</div>
</div><!-- end content -->
<?php echo thesis_sidebars(); ?>
<?php } }
remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
add_action('thesis_hook_custom_template', 'press_releases_page');
Try changing this:
<div id="press-releases">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="press-wrapper">
To this:
<div id="press-releases">
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="press-wrapper">
UPDATE:
The only other thing I can see is that you're not resetting your post data, and that the $wp_query variable is actually a Wordpress Global. Try using the reset functions and changing your WP_Query instance name like this:
<?php
wp_reset_query();
wp_reset_postdata();
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
UPDATE 2:
Now that you have your wrappers being written to your document, you need to normalize your information. Try to avoid using Metadata instead of normal post attributes. I imagine your metadata 'release_image' is a link to an image somewhere on your server:
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div id="press-wrapper">
<div id="press-image">
<img src="<?php echo get_post_meta(get_the_ID(), 'release_image', true); ?>" />
</div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo the_date(); ?></p>
<p class="press-link"><?php the_title(); ?></p>
<p class="press-author"><?php the_author(); ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_postdata(); ?>
The following is my search.php:
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type == 'page') : ?>
Show Page results
<?php endif; ?>
<?php endwhile; ?>
<?php rewind_posts(); ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type != 'page') : ?>
Show non-page results
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
No Results
<?php endif; ?>
The code allows search results under posts to be separated from those found in pages. If no search results are found for either posts or pages, the text "No Results" is displayed.
However, when results are found for pages but NOT posts (and vice versa), under the 'Post Search Results" there is nothing displayed. I would like the code tweaked so that if there are no results found under "Post Search Results" but there are results found under "Page Search Results," the text "No Results" is displayed underneath the "Post Search Results" header.
Thanks a lot everyone.
Give this a try, you can expand the array to include any post_type your site is using:
<?php
foreach (array('post','page') as $pt) :
$search_query = new WP_Query(array(
'post_type' => $pt,
's' => $s,
'posts_per_page' => 10,
'paged' => $paged
)
);
?>
<?php if ($pt == 'post') : ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php else : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php endif; ?>
<?php
if ($search_query->have_posts()) : while ($search_query->have_posts()) : $search_query->the_post();
if ($pt == 'post') :
?>
Post Results Code
<?php else : ?>
Page Results Code
<?php endif; ?>
<?php endwhile; else : ?>
No Results
<?php endif; ?>
<?php endforeach; ?>