Call top posts in different divs - php

I want to have my latest post show up with all info(title, author, thumbnail, and content) but only the title of the second most recent post in another div. Here is my code. It renders the divs correctly but the 'title' in the second div is the 'title' of the latest post still.
<div id="blog-pane">
<div id="blog-post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-title">
<?php the_title(); ?>
</div>
<div id="post-author">
<?php the_author(); ?>
<?php the_date(); ?>
<?php the_time(); ?>
</div>
<div id="post-image">
<?php the_post_thumbnail(); ?>
</div>
<div id="post-text">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php rewind_posts(); ?>
</div>
<div id="post-link-1">
<?php
query_posts( 'p' );
while ( have_posts() ) : the_post();
the_title();
endwhile;
wp_reset_query();
?>
</div>
</div>
</div>
<?php get_footer(); ?>

you can try to introduce some skip logic to have it skip the first post,
<div id="post-link-1">
<?php
query_posts( 'p' );
$count = 0;
while ( have_posts() ) {
if ($count++ == 0) {
the_post();
continue;
}
the_post();
the_title();
}
wp_reset_query();
?>
</div>

Related

PHP code for running "being watched" videos from selected category

In this code videos are shown at random. I wanted to show them as selected category (i-e= 'links' category). How can I write this code to work for the links category?
<?php if( !defined('ABSPATH') ) exit; global $awpt; ?>
<div class="being_watched">
<div class="heading">
<?php $h_tag = $awpt['live_videos_heading']; $title = $awpt['beingwatched_title'];
echo '<'.$h_tag.'>'.$title.'</'.$h_tag.'>' ?>
</div>
<div class="row">
<div class="col-md-12">
<ul class="Thumbnail_List">
<?php
$per_page = $awpt['watched_now_videos'];
query_posts('showposts='.$per_page.'&orderby=rand');
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php do_action( 'bestia_thumbnail_compatibility' ); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php wp_reset_query(); ?>
</ul>
</div>

How to make a second loop in single.php work?

-simple blog
-twenty twelve child theme
I need: a second loop in single.php that shows the selected post and all the other posts below.
What I have so far in single.php (results in a blank page) :
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php get_sidebar(); ?>
<?php
// The Second Query
$the_query = new WP_Query();
// The Loop
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ):
$the_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
This should do the trick:
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php
$args_second = array(
'posts_per_page' => -1,
);
// The Second Query
$second_query = new WP_Query( $args_second );
// The Loop
if ( $second_query->have_posts() ):
while ( $second_query->have_posts() ):
$second_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
Notes:
You need to properly show the title and content using the_title() and the_content() inside the single loop.
To show other posts, you need to query them, you'll quickly understand by looking at the code above.
I'll leave the styling for you.
It is tested and working.

if have posts display div else display no posts statment

I have no idea why this code is not working. I am trying to create a conditional statement where if this custom post type has posts then display customer-section div. if post does not exist then print no post statement. I did everything I thought I am suppose to but I must be doing something silly wrong because I can still see the customer-section div even though there are no posts.
here is what I have:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summery">
<?php the_excerpt(); ?>
</div>
<footer>
<?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</div>
<hr />
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
What you are currently doing is checking if your initial have_posts() (the default query) condition is true and if this is a page template it is always true. Actually (for page templates) you don't need that check at all, since WordPress will return 404 if the page is not found.
You need a check for posts for your custom query:
<?php the_post(); ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) );
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summery">
<?php the_excerpt(); ?>
</div>
<footer>
<?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</div>
<hr />
</div>
<?php endwhile;
else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
wp_reset_query();
?>
</div>
</div>
I believe the issue is your syntax with the two loops.
You're missing the if statement before the while condition
You're trying to load your $loop loop inside the standard page or post loop.
Try this instead:
<?php
$case_studies = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) );
if ( $case_studies->have_posts() ) : ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php while ( $case_studies->have_posts() ) : $case_studies->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<footer>
<?php if (get_the_tags()) { ?>
<p><?php the_tags(); ?></p>
<?php } ?>
</footer>
</div>
<hr />
</div>
<?php endwhile; ?>
</div>
</div>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; wp_reset_query(); ?>
this is what worked for me. You don't need to end the while loop if it's inside the WP_Query. All you have to do is close the if statement for the main div you want to show, add the while for the repeating section and if it doesn't work, I'd try deleting the conditions you have set on your footer.
<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) ); if($loop->have_posts()): ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summery">
<?php the_excerpt(); ?>
</div>
<footer>
<?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</div>
<hr />
</div>
<?php } wp_reset_postdata(); ?>
</div>
</div>
<?php endif; ?>

pagination custom post type with multiple WP_Query loop

after adding the function of pagination to Functions.php and recall it in template-product-listing.php
there is nothing shown in result.
I have a big problom with this...
could you find and resolve the problem?
thnx
<article class="row">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="single-head" class="sixteen columns">
<h1><?php the_title(); ?></h1>
<?php if (has_excerpt()) { the_excerpt(); } ?>
</div>
<div class="row">
<nav id="portfolio-filters" class="sixteen columns">
<?php _e('Show All', 'ci_theme'); ?>
<?php
$args = array(
'hide_empty' => 0
);
$skills = get_terms('product-category', $args);
?>
<?php foreach ( $skills as $skill ) : ?>
<?php echo $skill->name; ?>
<?php endforeach; ?>
</nav><!-- /portfolio-filters -->
</div>
<div id="portfolio-items" class="row">
<?php $ci_product_query = new WP_Query('post_type=product&posts_per_page=4'); ?>
<?php if ( $ci_product_query-> have_posts() ) : while ( $ci_product_query->have_posts() ) : $ci_product_query->the_post(); ?>
<?php $item_skills = wp_get_object_terms($post->ID, 'product-category'); ?>
<article class="<?php ci_e_setting('product_columns'); ?> columns <?php foreach ( $item_skills as $item_skill ) : echo $item_skill->slug.' '; endforeach; ?> columns portfolio-item">
<a href="<?php echo get_permalink(); ?>" title="<?php echo esc_attr(get_the_title()); ?>" class="fb">
<?php the_post_thumbnail('ci_portfolio_slider', array('class'=>'scale-with-grid')); ?>
</a>
<div class="portfolio-desc">
<h3><?php the_title(); ?></h3>
<p class="desc"><?php echo mb_substr(get_the_excerpt(), 0, 70); ?>...</p>
</div>
</article><!-- /portfolio-item -->
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
</div><!-- /portfolio-items -->
<?php get_template_part('part', 'call_to_action'); ?>
<?php endwhile; endif; ?>
<div class="pagination">
<?php wp_pagination(); ?>
</div>
</article>
if your sure there is a post_type called products and there is posts in it...
try:
<div id="portfolio-items" class="row">
<?php $ci_product_query = new WP_Query(array('post_type'=>'product', 'posts_per_page'=> 4); ?>
<?php if ( $ci_product_query-> have_posts() ) : while ( $ci_product_query->have_posts() ) : $ci_product_query->the_post(); ?>
You know you are starting a new wp_query for every post in have_posts() ? you might want to rethink what you are trying to achieve!

Display Post image on the Custom Page Template with Pagination

I have this code for my page template.
This is what i have right now: http://www.ohright.com/emoticons/
How do I change it so that it can display all my post images at 20 images per-page and with a pagination below?
<?php
/*
Template Name: emoticons-page
*/
get_header(); ?>
<div id="content">
<div id="main">
<?php query_posts('cat=44'.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
<?php
/*
Template Name: emoticons-page
*/
get_header(); ?>
<div id="content">
<div id="main">
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$posts_per_page = 20;
query_posts('cat=44&post_status=publish&paged=' . $paged . '&posts_per_page=' . $posts_per_page);?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile;
posts_nav_link();
wp_reset_query();
endif; ?>
</div>

Categories