I'll try to word this the best I can:
My blog homepage has 4 feeds of "Latest from: (a category)" fed by 4 different categories, each showing 2 posts. I discovered the do_not_duplicate method and used that to prevent any of them from showing up twice (since the authors use multiple categories on each post to populate our blog). This works great, but here is my next issue:
If a post has multiple categories that populate the home page, it will post in just one category and not duplicate (as wanted), but the other category it is in now only shows just 1 post, where I'd like it to show 2. Since the 2nd 'missing' post is the duplicate, not be shown, I'm wondering how I can show the next (3rd) post in that category, if the duplicate is being hidden.
Here is my current code:
<!-- BEGIN WP PHP BLOG INSERT-->
<?php query_posts('category_name=campuses&showposts=2'); //Get 2 most recent posts from category with slug campuses ?>
<h2 class="cat"><?php if (have_posts()) single_cat_title("Latest from: ", true) //if there are posts in the category, display the category name in an H2 ?></h2>
<?php if (have_posts()) while (have_posts()) : the_post(); $do_not_duplicate[] = $post->ID; // prevents the post from showing up twice on home page?>
<?php if ( has_post_thumbnail()) : //check to see if the post has a featured image ?>
<a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(category-thumb); ?>
</a>
<?php elseif( catch_that_image() ) : ?>
<a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><img height="150px" src="<?php echo catch_that_image() ?>" /></a>
<?php endif; ?>
<h3 class="recent"><?php the_title(); ?></h3>
<?php the_excerpt ()?><br class="clear" />
<?php endwhile;?>
<?php query_posts('category_name=programs&showposts=2'); //Get 2 most recent posts from category with slug programs?>
<?php if (have_posts()) single_cat_title('<h2 class="cat">Latest from: ', true) //if there are posts in the category, display the category name in an H2 ?></h2>
<?php if (have_posts()) while (have_posts()) : the_post(); if ( in_array( $post->ID, $do_not_duplicate ) ) continue; // prevents the post from showing up twice on home page?>
<?php if ( has_post_thumbnail()) : //check to see if the post has a featured image ?>
<a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(category-thumb); ?>
</a>
<?php elseif( catch_that_image() ) : ?>
<a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><img height="150px" src="<?php echo catch_that_image() ?>" /></a>
<?php endif; ?>
<h3 class="recent"><?php the_title(); ?></h3>
<?php the_excerpt ()?><br class="clear" />
<?php endwhile;?>
<?php query_posts('category_name=online&showposts=2'); //Get 2 most recent posts from category with slug online?>
<?php if (have_posts()) single_cat_title('<h2 class="cat">Latest from: ', true) //if there are posts in the category, display the category name in an H2 ?></h2>
<?php if (have_posts()) while (have_posts()) : the_post(); if ( in_array( $post->ID, $do_not_duplicate ) ) continue; // prevents the post from showing up twice on home page?>
<?php if ( has_post_thumbnail()) : //check to see if the post has a featured image ?>
<a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(category-thumb); ?>
</a>
<?php elseif( catch_that_image() ) : ?>
<a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><img height="150px" src="<?php echo catch_that_image() ?>" /></a>
<?php endif; ?>
<h3 class="recent"><?php the_title(); ?></h3>
<?php the_excerpt ()?><br class="clear" />
<?php endwhile;?>
<?php query_posts('category_name=service-applied-learning&showposts=2'); //Get 2 most recent posts from category with slug service-applied-learning ?>
<h2 class="cat"><?php if (have_posts()) single_cat_title("Latest from: ", true) //if there are posts in the category, display the category name in an H2 ?></h2>
<?php if (have_posts()) while (have_posts()) : the_post(); if ( in_array( $post->ID, $do_not_duplicate ) ) continue; // prevents the post from showing up twice on home page?>
<?php if ( has_post_thumbnail()) : //check to see if the post has a featured image ?>
<a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(category-thumb); ?>
</a>
<?php elseif( catch_that_image() ) : ?>
<a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><img height="150px" src="<?php echo catch_that_image() ?>" /></a>
<?php endif; ?>
<h3 class="recent"><?php the_title(); ?></h3>
<?php the_excerpt ()?><br class="clear" />
<?php endwhile;?>
<?php if( function_exists( 'wp_pagenavi ' ) ) {
wp_pagenavi();
} else {
next_posts_link('Older Posts');
previous_posts_link(' | Newer Posts');
} ?>
</div>
</div>
<div class="sidebar-wrapper">
<?php get_sidebar(); ?>
</div>
<!--END WP PHP BLOG INSERT-->
Not sure if this will help but it looks as though you need to reset your query_posts(); to give it another clean run
After each <?php endwhile;?> you would add
<?php wp_reset_query(); ?>
https://codex.wordpress.org/Function_Reference/wp_reset_query
Quote:
wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts(), if you must use that function. As noted in the examples below, it's heavily encouraged to use the pre_get_posts filter to alter query parameters before the query is made.
Calling wp_reset_query is not necessary after using WP_Query or get_posts as these don't modify the main query object. Instead use wp_reset_postdata.
Related
I have a custom post type archive page that lists the categories associated with that post type. I would like to include the thumbnail of the latest post in each category as the thumbnail for the said category.
So far I have managed to create the list of the categories, but my attempt at including the latest post thumbnail has resulted in the latest post for the post type as a whole being returned for all categories.
This is how I currently have it below:
<?php if ( $terms && !is_wp_error( $terms ) ) :
?>
<ol class="lister__list">
<?php foreach ( $terms as $term ) { ?>
<li class="lister__item item">
<article class="lister__article">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>" class="item__link" title="Permalink to <?php the_title(); ?>" rel="bookmark">
<!--image-->
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumb' );?>
<?php if ( has_post_thumbnail() ) { ?>
<img src="<?php echo $thumb[0]; ?>" width="<?php echo $thumb[1];?>" height="<?php echo $thumb[2];?>" />
<?php } else { ?>
nope
<?php }?>
<h2 class="item__title"><?php echo $term->name; ?></h2>
</a>
</li>
<?php } ?>
</ol>
<?php endif;?>
any help much appreciated.
I try to insert the value of a custom field in a background-image property ( therefore not in img src="..."').
In my category.php; I can display the custom field linked to each post; but when I put the variable in my style ( inline css ), wordpress always displays the same image.
The code :
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<style type="text/css">
.photo_interview {
background-image: url(<?php echo $photo_interview; ?>);
}
</style>
<div class="photo_interview"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
Any idea ? My page here : http://www.overso.me/category/interview/
Your code should be this:
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<!-- You don't need style tags if you only want to set the background image -->
<div style="background-image: url(<?php echo $photo_interview; ?>)"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
For what I can see you haven't set the global $post, so get_field do not know which is needed to display. in this case it will be better to use the_ID() to get the current post ID inside the while loop.
Cheers
this is my mp_productlist.php
<?php get_header();?>
<div class="content_mid">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="post thin">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>
<h3><?php mp_product_price(true) ?></h3><?php mp_buy_button(true, 'single')?>
</div>
<?php endwhile;?>
<?php else : ?>
<?php endif; ?>
<!--end post-->
</div>
<?php get_footer();?>
and this is what I'm getting http://angelov.ga/old/store/products/
it's only fetching one products instead of the two test products I have
thanks in advance
Nevermind, I just did a simple workaround and categorized everything in the category ALL and I have subcategories for each supplement thing and changed the BROWSE PRODUCTS thing to /category/all
got another question to ask:
I would like to display post thumbnails with the post title underneath. I have managed to work that out through asking on here, however now I would like to add a function that checks for a thumbnail and if none is available displays a dummy image.
Here is my try, which does display the thumbnail (if there) but not the dummy (if no thumbnail is attached):
<div class="gallery_container_wrapper">
<?php query_posts( $args ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="gallery_image_container">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<div class="gallery_image_thumb">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>
</div>
<div class="gallery_title">
<h2>
<?php the_title(); ?>
</h2>
</div>
</a>
</div>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
What am I doing wrong here?
This code works for me. Yours is virtually identical.
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/sunlit_path_banner.jpg" alt="Sunlit Path" />
<?php endif; ?>
Note: I also cut and pasted your code and tested on my WP install, and it worked fine.
Or... try this alternative to the if condition:
<?php if ( get_the_post_thumbnail( get_the_ID() ) ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>
I am using query_post to show a list of recent post.
And I want to give special style and html markup to the first post.
This is my current code:
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => -1,
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=query_posts($args);
if ($posts) {
echo '<h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
while ( have_posts() ) : the_post();
if( $wp_query->current_post == 0 ) :?>
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('post-thumb'); ?></a>
<?php endif; ?>
<h5> <?php the_title(); ?></h5>
<?php else : ?>
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('post-thumb'); ?></a>
<?php endif; ?>
<h5> <?php the_title(); ?></h5>
<?php endif;
endwhile;
} // if ($posts
} // foreach($categories
All you need to do is just wrap your snippet into if statement and call the_post function before while loop. By calling the_post function you will fetch first record from your queue. It should be like this:
if ( have_posts() ) :
the_post();
if ( has_post_thumbnail()) :
?><a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail('post-thumb'); ?>
</a><?php
endif;
?><h5>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h5><?php
while ( have_posts() ) :
the_post();
if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('post-thumb'); ?></a>
<?php endif; ?>
<h5> <?php the_title(); ?></h5>
endwhile;
endif;
Depending on what you want to style [the whole post, the title, whatever], you're going to want to identify the first post, and then conditionally output a class or whatever HTML you want for that post. The "loop" is the part of your code between the while and the endwhile. So right before the loop, put:
$is_first_post = TRUE;
then inside the while loop, before whatever it is you're trying to add styles to, put:
if ($is_first_post == TRUE){
echo (" .... THIS WOULD BE ADDED TO THE FIRST POST ONLY ....");
$is_first_post = FALSE; //this flags the next post as not being first
}