I'm using this code to show my 10 homepage posts:
<?php query_posts('category_name=homepage&showposts=10&order=DESC');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
I would like somehow to identify the first post and change the code only for the first post,
For example first post should show me the_excerpt instead of the_title like this:
<?php query_posts('category_name=homepage&showposts=10&order=DESC');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_excerpt(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
Use a variable to count the number of posts you're looping through. If the counter is 0, you're on the first post:
<?
query_posts('category_name=homepage&showposts=10&order=DESC');
$i = 0;
while ( have_posts() ) : the_post();
$i == 0 ? the_excerpt() : the_title();
the_content();
$i++;
endwhile;
?>
You can set a counter that increments each loop.
Start it by 1
$count = 1;
each loop do $count++;
if ($count ==1){the_excerpt();} else{the_content();}
Related
To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});
I want display wordpress loop but I want only show count of items in the loop filtered by a custom field value. I don't want to show the title or the content, only the count. This is what I have:
<?php query_posts('meta_key=city&meta_value=Seelbach'); ?>
<?php $count = 0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; //increment the variable by 1 each time the loop executes ?>
<div>
<?php if ($count==1) {
echo "1";
}
elseif ($count==2) {
echo "2";
}
elseif ($count==3) {
echo "3";
}
elseif ($count==4) {
echo "4";
} ?>
</div>
<?php endwhile; endif; ?>
but the output is "1 2" - he shows each item and gives it a number but i only want to show the count of ALL (in this case 2 ... ) items that fit to my meta_value. that means i only want to show the "2".
if you want the total count you can get it like this
<?php query_posts('meta_key=city&meta_value=Seelbach'); ?>
<?php $count = 0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; //increment the variable by 1 each time the loop executes ?>
<?php endwhile; endif; ?>
<?php echo "Count".$count; ?>
You dont need a loop to get the total number, simply use count on the posts array returned by get_posts:
echo count(get_posts(array('meta_key' => 'city', 'meta_value' => 'Seelbach')));
Edit as per comment:
$count = count(get_posts(array('meta_key' => 'city', 'meta_value' => 'Seelbach')));
if($count){
//your link code here
}
If you want to get the total number of posts inside the loop, do something like this (result will always be 2):
<?php $posts = query_posts('meta_key=city&meta_value=Seelbach'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
<div>
<?php echo count($posts); ?>
</div>
<?php endwhile; endif; ?>
If you need the posts count only once, place the div outside the loop
<?php $posts = query_posts('meta_key=city&meta_value=Seelbach'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
// do something with post data
?>
<?php endwhile; ?>
<div>
<?php echo count($posts); ?>
</div>
<?php endif; ?>
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;
?>
<?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();
?>
This is what I have so far. Unfortunately this grabs ALL my pages' titles and content including sub-pages. I only want to display top-level main pages. How can I do this?
<?php query_posts('post_type=page'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
*Note: I want to display both title and content for the pages. Not just title. Otherwise I would use:
<?php wp_list_pages('depth=1'); ?>
UPDATE: Following konsolenfreddy's advice I was able to loop through my pages properly. However a new problem arose. The content is being striped of its tags. Anyway I can keep them? Here's my new code:
<?php
$pages = get_pages('parent=0');
foreach ($pages as $pagg) {
$option .= $pagg->post_title;
$option .= $pagg->post_content;
echo $option;
}
?>
You can use get_pages() (see http://codex.wordpress.org/Function_Reference/get_pages ), it takes the same arguments as wp_list_pages()
Does this work?
<?php query_posts('post_type=page&post_parent='.$parent);?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>