I have 20 posts and i need to display text after every 5 posts. So i have asked the query in stackoverflow and i have get the solution. I have tried the query in my site. My code is,
<?php
query_posts( array(orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<div class="post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
</div>
<?php ($p%5 == 0) ? ($p/5): ""; ?>
<?php if($p==5):
echo hai;
elseif($p==10):
echo fine;
endif;
?>
<?php
$p++;
?>
<?php
endwhile;
?>
It works fine when echoeing "Hai" and "Fine" after 5 posts.
BUt while replacing the code in the below format. The posts are not fetching correctly. Please anyone help me. I need to add my own category id 3 after 5th post and category id 4 after 10th post
<?php
query_posts( array(orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<div class="post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
</div>
<?php ($p%5 == 0) ? ($p/5): ""; ?>
<?php if($p==5):
if (have_posts()) :
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
elseif($p==10):
echo fine;
endif;
?>
<?php
$p++;
?>
<?php
endwhile;
?>
inplace of echo fine; i need to add the same code used above in place of echo hai;
Use wp_reset_query().
wp_reset_query() restores the $wp_query.
<?php
query_posts( array(orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<div class="post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
</div>
<?php ($p%5 == 0) ? ($p/5): ""; ?>
<?php if($p==5):
if (have_posts()) :
wp_reset_query();
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while (have_posts()) : the_post(); ?>
<?php the_title();
?>
<?php endwhile; ?>
<?php endif;
elseif($p==10):
wp_reset_query();
if (have_posts()) :
wp_reset_query();
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
endif;
$p++
?>
<?php
endwhile;
?>
Related
I would like to display post from only one category. How should I change this function?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post','paged' => $paged);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'post-format/content', get_post_format() );
endwhile;
else:
get_template_part( 'post-format/content', 'none' );
endif;
?>
<?php
/*
Template Name: Boats'n'Hoes
*/
?>
<?php get_header(); ?>
<div id="main">
<div id="content" class="narrowcolumn">
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
reference :- https://wordpress.org/support/topic/display-the-posts-of-one-category-in-a-page-solved
Something I created for a client was a template that lists posts from a category that has the same name as the page.
So, create a Page called "funnies" if you want to list all posts in the "technology" category.
Oh, and the original content of the Page is displayed too, if you need an introductory text.
<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>
<?php get_header(); ?>
<div id="content">
<div id="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.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(); ?>
Pass this with $args
'category_name' => 'cat-name',
How should look my code in order to display a preview of a categories with main main page?
Now its is
<?php $posts = get_posts ("category=2&orderby=date&numberposts=3"); ?>
<?php if ($posts) : ?>
<?php foreach ($posts as $post) : setup_postdata ($post); ?>
<div>
<?php the_title(); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
and below this
<?php if(have_posts()): ?>
<?php while(have_posts()): the_post(); ?>
<div class="pageContainer"><?php the_content(); ?></div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?><div>Sorry, posts are not found.</div>
<?php endif; ?>
This leads to ignore second part...
Sorry for my english.
<?php
// Category posts
$posts = get_posts("category=2&orderby=date&numberposts=3");
if ($posts){
foreach ($posts as $article){
echo '<div><a href="'.get_permalink($article->ID) ?>" rel="bookmark">'.
$article->post_title.'</a></div>';
}
}
// Current page content
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="pageContainer">
<?php the_content(); ?>
</div>
<?php endwhile;
else:
echo '<div>Sorry, posts are not found.</div>';
endif;
?>
<?php $counter = 3; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('3') ): ?>
<?php else: ?>
<?php endif; ?>
<?php if($counter%2 == 0){echo 'floatRight';} else { echo 'floatLeft'; } ?>
<?php the_ID(); ?>
<h1 > <?php the_title(); ?></h1>
<?php the_post_thumbnail('full'); ?>
<?php the_content(__('(more...)')); ?>
<?php comments_template(); // Get wp-comments.php template ?>
<?php if($counter%2 == 0){ echo "<div class='clear'></div>";} ?>
<?php $counter++; ?>
<?php endwhile; else: ?>
<?php endif; ?>
i am trying to display in this way and i want to display particular category of post
post1
post2
post3
post4
please give me the solution ...
use the below code.
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
<?php $i = 0; rewind_posts(); ?>
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
You can use this css3 property to do it (This would take care odd and even elements, you don't have to write down the loop explicitly):
p:nth-child(odd) //you can do the same for div
{
float:left;
}
p:nth-child(even)
{
float:right;
}
You can use query_post() or Wp_query() in wordpress .
Use this code. to get the particular categories post .
<?php
// The Query
query_posts( 'cat=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
I'm trying to setup a way to display related posts by category and exclude current one, but when a category has only one post, my current code to display anything else, is not working.
This is the code that I have so far. Any ideas why?
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content();?>
<?php $current_post_id = get_the_ID(); $post_cats = wp_get_post_categories( get_the_ID(), NULL ); ?>
<?php query_posts('showposts=8&orderby=desc&cat='.implode(',',$post_cats)); if (have_posts()) : ?>
<?php $i=0; while (have_posts()) : the_post(); if($current_post_id==get_the_ID()) continue; $i++; ?>
<span class="related-title"><?php short_title('...', 43); ?></span>
<?php endwhile;?>
<?php else : ?>
<p>Sorry! There is no more related posts in this category.</p>
<?php endif; wp_reset_query(); ?>
<?php endwhile;endif ?>
<div style="clear:both;"></div>
<?php get_template_part('includes/loop-tips');?>
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>