I am new to wordpress. Inside the loop I want to check if the post is the first result from the query.
This is my code.
<?php
$args = array('post_type' => 'home-slider', 'posts_per_page' => 6, 'order' => 'ASC');
$query = new WP_Query( $args );
while ($query->have_posts()) : $query->the_post();
?>
<?php if ( ) { ?> // check if first post
<p>TEST</p>
<?php } else { ?>
<p>TEST123</p>
<?php } ?>
<?php
endwhile;
?>
Thanks in advance.
Try This code
First define $inc=1; out side the while loop
Then Check in while loop if($inc==1) then print what you want and increment the $inc value using $inc++;
<?php
$args = array('post_type' => 'home-slider', 'posts_per_page' => 6, 'order' => 'ASC');
$query = new WP_Query( $args );$inc=1;
while ($query->have_posts()) : $query->the_post();
?>
<?php if ($inc==1 ) {$inc++; ?> // check if first post
<p>TEST</p>
<?php } else { ?>
<p>TEST123</p>
<?php } ?>
<?php
endwhile;
?>
The WP_Query object has a current_post field, which is actually a counter, counting from 0.
<?php
$args = array('post_type' => 'home-slider', 'posts_per_page' => 6, 'order' => 'ASC');
$query = new WP_Query( $args );$inc=1;
while ($query->have_posts()) : $query->the_post();
?>
<?php if ($query->current_post==0) { ?> // check if first post
<p>TEST</p>
<?php } else { ?>
<p>TEST123</p>
<?php } ?>
<?php
endwhile;
?>
Instead of doing the check inside the loop, you could move it to the outside and query the first post after an if() statement:
<?php
$args = array('post_type' => 'home-slider', 'posts_per_page' => 6, 'order' => 'ASC');
$query = new WP_Query( $args );
?>
<?php if ($query->have_posts()) : $query->the_post(); ?>
<p>TEST</p>
<?php
endif;
while ($query->have_posts()) : $query->the_post();
?>
<p>TEST123</p>
<?php endwhile; ?>
Related
I'm trying to modify a projects.php file which uses some loops and if statements to fetch projects based on there tags. I need to modify thee below code to only fetch projects with the tag new. Currently all the projects are being fetched and displayed.
Any help would be great. See code attached.
Thanks
<div id="projects" class="clearfix">
<?php $page_skills = get_post_meta($post->ID, "_ttrust_page_skills", true); ?>
<?php $skill_slugs = ""; $skills = explode(",", $page_skills); ?>
<?php if (sizeof($skills) >= 1) : // if there is more than one skill, show the filter nav?>
<?php $skill = $skills[0]; ?>
<?php $s = get_term_by( 'name', trim(htmlentities($skill)), 'skill'); ?>
<?php if($s) { $skill_slugs = $s->slug; } ?><?php endif;
$temp_post = $post;
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 200,
'post_type' => 'project',
'skill' => $skill_slugs
);
$projects = new WP_Query( $args );
endif; ?>
<div class="wrap">
<div class="thumbs masonry">
<?php while ($projects->have_posts()) : $projects->the_post(); ?>
<?php
global $p;
$p = "";
$skills = get_the_terms( $post->ID, 'skill');
if ($skills) {
foreach ($skills as $skill) {
$p .= $skill->slug . " ";
}
}
?>
<?php get_template_part( 'part-project-thumb'); ?>
<?php endwhile; ?>
<?php $post = $temp_post; ?>
</div>
</div>
you should just need to add a field in your $args array :
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 200,
'post_type' => 'project',
'skill' => $skill_slugs,
'tag' => 'new' // add this for the tag
);
$projects = new WP_Query( $args );
Let me know if it works :D
Only two categories need to be showed in the homepage. Can anyone help.
You can use WP_Query to get your posts list, and display it with the loop
Example :
$the_query = new WP_Query( array( 'category_name' => 'staff,news' ) );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
In your functions.php file paste the below code:
I am assuming that you want to show categories from two categories which are having ids 5 and 9.
function kiran_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '5,9');
}
}
add_action( 'pre_get_posts', 'kiran_home_category' );
Explanation:
kiran_home_category is just a custom name for the function. That can be any name. The way it works is you attach a function to the action hook pre_get_posts. So before getting the posts the function kiran_home_category will be called. And then inside the function I am changing the query here to only load categories with ID 5 and 9
In wordpress WP_query, category__in parameter used to select category with posts.
<?php
$query = new WP_Query( array( 'category__in' => array( 2, 6 ),'post_status'=>'publish','orderby'=>'menu_order','order'=>'Asc' ) );
if($query->have_posts()):
echo '<ul>';
while ( $query->have_posts() ) : the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
echo '</ul>';
endif;
?>
For more information about wordpress query click here , you can read more information.
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => -1,'category_name' => array('Latest News','News') );
$loop = new WP_Query( $args );
if($loop->have_posts()):
?><ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li> <span class="date"><?php echo get_the_date( 'd F Y');?></span>
<h3><?php echo get_the_title();?></h3>
<?php echo $description = get_the_content(); ?>
</li>
<?php endwhile;?>
</ul>
<?php endif;?>
<?php wp_reset_postdata(); ?>
Do the following, usually in page.php or single.php or if you want a custom page for a category, you can do, category-samplecat.php..
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => array('samplecat', 'anothercat'),
'paged' => $paged
);
$arr_posts = new WP_Query($args);
Then do the usual if, while statement..
if($arr_posts->have_posts() ) :
// Start the loop.
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();?>
<?php endwhile;
endif;
I hace 2 sections on my site: first is for popular posts (based on views) and the second section is for the recent posts.
If the post is already in the popular posts section, I don't want it to be displayed in the "Recent posts" section. Below is my code. In the first loop I've created an array to store all post ID's which are in that section. In the second loop I check if the id is in that array (may be not the best solution).
For some reason it only works with the first duplicate, even though $cont becomes true required amount of times(I checked with echo). So what gives?
<?php
$popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );
$counter=0;
$post_ids = array();
while ( $popularpost->have_posts() ) : $popularpost->the_post();
$postID = get_the_ID();
$post_ids[$counter] = $postID;
?>
<?php the_title(); ?>
<?php $counter++; ?>
<?php endwhile; ?>
<?php $myquery = new WP_Query('posts_per_page=6');
while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<?php $post_id = get_the_ID(); ?>
<?php $post_ids_length = count($post_ids); ?>
<?php for ($i=0; $i < $post_ids_length; $i++) {
if ($post_id == $post_ids[$i]) {
$cont = "true";
} else {
$cont = "false";
}
} ?>
<?php if ($cont == "true") {
continue;
} ?>
<?php the_title(); ?>
<?php endwhile; ?>
Update your Second Query as below:
$args2 = array('post__not_in' => $post_ids,'posts_per_page' => 6 );
$myquery = new WP_query($args2);
And then just iterate over the result using the While loop.
Hi i need to display list of post by post type and category, i have code like this, but it isint working properly:
<?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename&cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?>
This code displaying post from "posttypename", but it displays all post from that custom post type, but i need to displaying post from only "categoryname"
The whole code looks like this:
<?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename%cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?> <?php if($i == 1) : ?> <div class="">content of the post</div> <?php endif; ?> <?php $i++; endwhile; ?>
Use WP_Query like this :
$catquery = new WP_Query(array(
'post_type' => 'posttypename',
'posts_per_page' => -1,
'category_name' => 'categoryname',
// 'cat' => cat ID here
));
if( $catquery->have_posts() ){
while($catquery->have_posts()){
$catquery->the_post();
// your stuff
}
wp_reset_postdata();
}
Try below code
<?php
$catquery = new WP_Query(array(
'post_type' => 'posttypename',
'posts_per_page' => -1,
'category_name' => 'categoryname'
));
if( $catquery->have_posts() ){
$i = 1;
while($catquery->have_posts()){
if($i == 1){ ?>
<div class="">content of the post</div>
<?php } ?>
$i++;
}
wp_reset_postdata();
}
?>
I'm using the following PHP script to find the most recent post on the Team area of my site.
I also use a very similar one to find the most recent news entry on my home page.
To reduce the amount of repeated code (DRY), is there a way I can use a function and just pull in a specific custom post type e.g. most_recent('team'); would show the most recent post from my Team CPT.
Here's my existing code:
<?php
// find most recent post
$new_loop = new WP_Query( array(
'post_type' => 'team',
'posts_per_page' => 1,
"post_status"=>"publish"
));
?>
<?php if ( $new_loop->have_posts() ) : ?>
<?php while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile;?>
<?php else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php
function most_recent($type) {
$new_loop = new WP_Query( array(
'post_type' => $type,
'posts_per_page' => 1,
"post_status"=>"publish"
));
if ( $new_loop->have_posts() ) {
while ( $new_loop->have_posts() ) : $new_loop->the_post();
echo '<h2>'.the_title().'</h2>';
the_content();
endwhile;
}
wp_reset_query();
}
?>
Yes, It is indeed possible.
First what you need to do is ,
Add below code to your theme's functions.php file:
function most_recent($name){
// find most recent post
$new_loop = new WP_Query( array(
'post_type' => $name,
'posts_per_page' => 1,
"post_status"=>"publish"
));
if ( $new_loop->have_posts() ) :
while ( $new_loop->have_posts() ) : $new_loop->the_post();
echo "<h2>".the_title()."</h2>";
the_content();
endwhile;
else:
endif;
wp_reset_query();
}
Now you can use it any where in your theme folder template like below:
$most_recent = most_recent('product');
echo $most_recent;
So in your case, it would be most_recent('team') or even you can use for other as well just like I did for product.
Tell me if you have any doubt.