I need to get second newest post from wordpress, and I cant do this. I found I lot of tutorials how to get several newest posts, but no how to get only one post that is second in the order (or fifth, or tenth).
I have something like this:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if(get_posts()[1] == true) : ?>
<?php get_template_part( 'content-second-row', ( post_type_supports( get_post_type(), 'post-formats' ) ? get_post_format() : get_post_type() ) ); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
But, of course it is not working. I tried to get get_post().length. I know, unfortunately I can't use posts id, because there is no one by one.
Please, help me.
Of course, I try to finde something in wordpress codex
try this
get_posts( array('posts_per_page' => 5,'offset' => 1,))
You can change offset to any number you want.
Related
I'm using a theme for some Wordpress-integrated software which means that I am basically stuck with the template hierarchy, otherwise I'd just set up a custom template to get around this.
Anyway, I have my page.php, which looks a little like the following
<?php //start the main loop
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php
if ( is_page('contact-us') ) {
get_template_part( 'content', 'contact' );
}else{
get_template_part( 'content', 'page' );
}
?>
<?php //end the main loop
endwhile;
else:
?>
Something is missing
<?php
endif;
?>
This works fine, and as expected, I am able to add html within content-page.php
However, I would like to add a custom loop within content-page.php to display customer testimonials. I've attempted this with the code below inside content-page.php:
<?php //close the main loop
endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php
//close the main loop and open a custom loop
wp_reset_postdata();
$args = array (
'post_type' => 'testimonial',
'posts_per_page' => 5,
'orderby' => 'rand'
);
$the_query = new WP_Query ( $args );
if ( have_posts() ) : while ( $the_query ->have_posts() ) : $the_query ->the_post(); ?>
Do HTML stuff here
<?php //close the custom loop
endwhile; else: ?>
Uh oh, there is meant to be a testimonial here. Please create some.
<?php endif; ?>
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
This code creates a PHP error (unexpected endwhile where I attempt to close the main loop). However, I put this exact same code straight inside page.php, it works. It only errors when inside content-page.php. Am I not able to include custom loops with get_template_part?
the last endwhile and endif are not closed
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
endwhile;
endif;
?>
How to get all posts from one category. i tried this code , it's not showing any output.Is it correct or any correction is here? Thanks.
include('wp-config.php');
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result = array(
"id"=>$args['ID'],
"type"=>$args['post_type'],
"url"=>$args['guid']);
endforeach;
wp_reset_postdata();
print($result);
Try below :-
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result[] = array(
"id"=>$post->ID, // changed $args to $post
"type"=>$post->post_type,
"url"=>$post->guid);
endforeach;
wp_reset_postdata();
print_r($result);
If you want to display posts from a specific category in the category page, you can use the below given code in your theme's category.php file.
<?php
if(have_posts()) :
while (have_posts()) : the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
?>
If you want to display the same in pages other than category page, just add the following code to the corresponding files.
<?php
$posts = new WP_Query();
$posts->query( "category_name='{enter your category slug here}'&posts_per_page=-1" );
if($posts->have_posts()) :
while ($posts->have_posts()) : $posts->the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
are you just trying to get posts from a category?
This is a handy code from the Codex that I keep around. Use this on any custom category page or anywhere on any page, for that matter, to start the loop. Make sure you put your category slug into the right spot in the code.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
// YOUR STUFF LIKE the_title(); or the_content();
endwhile; endif;
This is NOT a fix to your code, but it answers the question you asked. I think your problem may be in the use of $args inside the loop (seems odd), but if you want me to make sure I might need more of the code or a working example I can see.
http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category
AHEM...yeah... I'm an idiot... don't go pasting this around. Use WP_Query!! thanks Pieter.
On your theme directory, search category.php. If it doesn't contain, create a new file category.php and paste this code:
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_excerpt();?>
<?php endwhile; else :?>
<?php endif;?>
In the Twenty Fourteen theme and many others authored by experts, single.php contains the code:
while ( have_posts() ) : the_post();
get_template_part( 'content', 'page' );
endwhile;
Is this any different or better than
the_post();
get_template_part( 'content', 'page' );
Is there a scenario where WordPress may expect single.php to be able to display more or less than exactly one post or is there another reason authors choose to use a while loop?
the while ( have_posts() ) : the_post(); is there by default so you can put conditional logic like in the example code below and won't get a 404 just incase there's no post or the permalink changes.
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Hope it helps.
I hope someone can help me with this. I have been reading other posts, the Codex, and trying others code, but can't fix my query.
I am creating a Page that contains a list of authors. The authors are in two categories, and I need to sort by last name, first name. I want to sort them by the custom field wpcf-sortname (from the Types plugin).
I get the correct results from the query, but the results are sorted by ID.
Note: I'm not very good with queries, but any help will be appreciated.
I've tried:
<?php query_posts(array('category__and'=>array(48,49),'meta_key'=>wpcf-sortname,'orderby'=>meta_value,'order'=>ASC,));
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?><br />
<?php endwhile; // end of the loop. ?>
And:
<?php
$args = array( 'category__and'=>array(48,49),'meta_key'=>wpcf-sortname,'orderby'=>wpcf-sortname,'order'=>ASC,'posts_per_page'=>-1);
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div>
<?php the_title(); ?><br />
</div>
Try with this
<?php query_posts(array('category__and'=>array(48,49),'meta_key'=>'wpcf-sortname','orderby'=>'meta_value','order'=>ASC,));if ( have_posts() ) while ( have_posts() ) : the_post(); ?><?php the_title(); ?><br /><?php endwhile; // end of the loop. ?>
I have just put the single quote in metakey and orderby field. I think this will work for you.
I displayed all the post from all the post type in the page. Now i want to display all the latest post from all the post type. I want only one post from all the post type.
To display all the post from all post type i used the following code.
<?php query_posts(array('post_type'=>array('a','b','c'), 'posts_per_page' => 2)); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="issue-content">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();} ?>
<?php get_template_part( 'content-issues', 'page' ); ?>
<?php comments_template( '', true ); ?>
</div><!--issue-content-->
<?php endwhile; // end of the loop. ?>
i tried with the following code
<?php query_posts(array('post_type'=>array('a','b','c'), 'posts_per_page' => 1)); ?>
i cant get from all the post type. How can i get the latest post from all the post type.
You should use multiple query_posts()
query_posts('post_type=a&posts_per_page=1');
query_posts('post_type=b&posts_per_page=1');
query_posts('post_type=c&posts_per_page=1');
You can also use, get_posts(); function for getting all posts from every post types.