I have a slider script for my homepage (wp nivo slider), but only want to show slides (posts) if todays date is before post_end_date (custom field). This is so that I do not have to manually remove posts that are no longer relevant.
This is the code that loads the posts. Any help would be greatly appreciated.
Custom post field: post_end_date
<?php
$category = get_option('wpns_category');
$n_slices = get_option('wpns_slices');
?>
<?php query_posts( 'cat='.$category.'&posts_per_page=$n_slices' ); if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php if ( '' != get_the_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif ?>
<?php endwhile; endif;?>
<?php wp_reset_query();?>
<?php $meta_values = get_post_meta($post_id, $key, $single); ?>
in this function $postid is that id of wich is coming in loop and key is that name of custom field last in $single you can write true of false
get the custom fild by this function after that try the if condition for you custom postfeild values
hope this will help you
Related
In sigle.php page I need to display a list of post that belong to the category of the post and the I have to display the post content.
So I did a query to get the posts tat belong to the post category:
<?php
$cat = get_the_category();
query_posts('cat='.$cat[0]->cat_ID);
while (have_posts()) : the_post();?>
<li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><?php echo get_the_title();?></li>
<?php endwhile;?>
Now how can I retrive the current post data?
If I do like this
<?php while ( have_posts() ) : the_post(); ?>
<?php echo the_post_thumbnail(get_the_ID());?>
<?php endwhile;?>
I still display query_posts.
I solved using WP_Query instead of query_posts();
<?php
$query = new WP_Query('cat='.$cat[0]->cat_ID);
while ($query->have_posts()) : $query->the_post();?>
<li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><?php echo get_the_title();?></li>
<?php endwhile;?>
I have a Custom Post Type set up called Venues. I'm also using a plugin called Event Organiser, and I want to display the title and link to the venue within one of the Event Organiser templates.
The code is:
<?php if( $eo_event_loop->have_posts() ): ?>
<ul <?php echo $id; ?> class="<?php echo esc_attr($classes);?>" >
<?php while( $eo_event_loop->have_posts() ): $eo_event_loop->the_post(); ?>
<?php
//Generate HTML classes for this event
$eo_event_classes = eo_get_event_classes();
//For non-all-day events, include time format
$format = ( eo_is_all_day() ? $date_format : $date_format.' '.$time_format );
?>
<li class="<?php echo esc_attr(implode(' ',$eo_event_classes)); ?>" >
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php the_title(); ?></a> at VENUE NAME HERE <?php echo __('on','eventorganiser') . ' '.eo_get_the_start($format); ?>
</li>
<?php endwhile; ?>
</ul>
Where VENUE NAME HERE is, I want the title and link to my custom post type. Might be just something really simple I'm missing, but any help much appreciated.
I used Advanced Custom Fields plugin and used 'Relationship' field so that I could choose a Venue for each event. Then used this code to bring through the Venue link:
<?php $venue = get_field('location_venue'); ?>
<?php foreach( $venue as $venue ): ?>
<?php echo get_the_title( $venue->ID ); ?>
<?php endforeach; ?>
More info available here:
http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/
I have a custom post type called 'grhdogs'. The problem is that the loop inside the WordPress search.php is styling the custom post type like a normal hit on a post or page. I want to style the custom post type search results with a different template part. How can I do this? It speaks for it self that normal posts and pages should get the default template part (content,search).
This is the loop...
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
<?php wplook_content_navigation('postnav' ) ?>
The template part I want to use for the custom post type 'grhdogs' is:
<?php get_template_part( 'search', 'grhdogs' ); ?>
You should include the template part by comparing the post type using get_post_type() function -
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php // so if the current post type is your custom post type ?>
<?php if( 'grhdogs' == get_post_type() ): ?>
<?php get_template_part( 'search', 'grhdogs' ); ?>
<?php // for any other post type ?>
<?php else : ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
<?php wplook_content_navigation('postnav' ) ?>
Additionally, For including separate template file based on post type and current object page, a dynamic method be -
<?php $query_type = 'search'; // pv: archive, author, category ?>
<?php $post_type = get_post_type(); // pv: post, custom post type ?>
<?php get_template_part( $query_type, $post_type ); ?>
And example template filename would be then - 'search-post.php'
** pv = possible value
I am using an if elseif statement to check what page it is, and it requires that there are posts on the page so when a search is returned with a result of 0, my code stops working. Any ideas how to code this better?
this code is in my sidebar and is showing the recent articles, you can see an example of a search working here(the sidebar is setup as a sub-footer):
http://ivry.sweetyams.ca/?s=new
and a search not working here:
http://ivry.sweetyams.ca/?s=asjdfkl%3B
Code I am using:
(I have tried putting stuff into the else{ code and it doesn't work either because there are no posts on the 'nothing found' search page
<?php if (have_posts()) : ?>
<?php /* IF SEARCH PAGE */ if (is_search() ) { ?>
<?php query_posts('category_name=0&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF ESCALADE PAGE */ }elseif (is_category_or_sub(6)) { ?>
<?php query_posts('category_name=escalade&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php query_posts('category_name=montagne&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php } else { ?>
DO SOMETHING ELSE
<?php }?>
<?php endif; ?>`
I am trying to get all recent articles for my sidebar and when there is no result in the search, it also stops my sidebar from working.
The code I added is not in my search page but in my SIDEBAR, it separates the comments into 2 categories, (6 and 14) I am essentially splitting my site using categories for navigation, anything as a child below 6 will be styled one way and 14 the other, you can see the difference on the escalade and montagne links:
This is getting every post below escalade, OR every post below the montagne category
My search pulls results from ALL categories, but if there is no search result, for some reason my sidebar code, the code I included, doesn't work.
first, showposts is deprecited since v2.1, used posts_per_page instead.
second, after each query_post, you need to reset the query like this:
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
this is how I understand it. see more detail here:query_posts(), hope this helps.
I found a way to get it to work,
I found that it is encouraged to use WP_Query for secondary posts within a page, http://new2wp.com/noob/query_posts-wp_query-differences/
also, for anyone that is wondering the is_category_or_sub function is from here: http://valendesigns.com/wordpress/is-category-or-subcategory-wp-function/
This code checks whether you are on a category or subcategory,
<?php
$my_posts = new WP_Query(); ?>
<?php /* IF ESCALADE PAGE */ if (is_category_or_sub(6)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=6');?>
<?php /* ELSEIF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=14'); ?>
<?php } /* AND OTHER PAGE */ else { ?>
<?php $my_posts->query('posts_per_page=5'); ?>
<?php }?>
<?php while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts ?>
<a href="<?php echo esc_url( get_permalink( $post->the_permalink ) ) ?>" title="Permanent Link to <?php echo substr($post->post_title,0,200);?>">
<p><?php echo substr($post->post_title,0,200); ?></p>
</a>
<?php endwhile; ?>
<?php wp_reset_query() // RESET QUERY ?>
I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.
Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.
Any idea how to get the theme to search both posts and pages?
Here is the majority of search.php:
<?php if (have_posts()) : ?>
<h3>Search Results</h3>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h4><?php the_title(); ?></h4>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h3>No posts found.</h3>
<?php endif; ?>
Add this code to your functions.php file.
function wpshock_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post','page') );
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
http://wpth.net/limit-wordpress-search-results-to-specific-post-types
WP Search http://wpsear.ch/ has that ability.You can adjust what post types you want to show in results page.
In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='page')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
Let me know if it worked.
Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/