i want to show 4 posts in page and after 4 posts it will show next button to read next posts.
<?php while (have_posts()) : the_post(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&order=ASC&category=3');
foreach($myposts as $post) :
setup_postdata($post);
?>
here is content with html
<?php endforeach; ?>
<?php endwhile; ?>
<?php endif; ?>
You just need to add paged parameter to your query_posts:
$myposts = get_posts('numberposts=4&order=ASC&category=3&paged' . get_query_var('paged'));
And add post_nav_link() to display next and prev link.
<?php posts_nav_link(); ?>
Cheers, oh, btw, you can post specific wordpress question at wordpress.stackexchange.com.
Related
I am doing theme integration in wordpress.I created home.php (template page) and select from backend(for front end page).
Now i want to show posts on front page.I put following code for display post in front page.But code is not working.Where i am wrong ?
Here is my code
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile;
endif;
?>
You can use following code -
<?php
$args = array('posts_per_page' => 3, 'post__not_in' => get_option('sticky_posts'));
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php esc_html(the_title()); ?>
<?php the_excerpt(); ?>
<?php
endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
In sigle.php page I need to display a list of post that belong to the category of the post and the I have to display the post content.
So I did a query to get the posts tat belong to the post category:
<?php
$cat = get_the_category();
query_posts('cat='.$cat[0]->cat_ID);
while (have_posts()) : the_post();?>
<li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><?php echo get_the_title();?></li>
<?php endwhile;?>
Now how can I retrive the current post data?
If I do like this
<?php while ( have_posts() ) : the_post(); ?>
<?php echo the_post_thumbnail(get_the_ID());?>
<?php endwhile;?>
I still display query_posts.
I solved using WP_Query instead of query_posts();
<?php
$query = new WP_Query('cat='.$cat[0]->cat_ID);
while ($query->have_posts()) : $query->the_post();?>
<li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><?php echo get_the_title();?></li>
<?php endwhile;?>
Currently I am querying a post from my custom post type to my custom page template. This is the code I am using
<?php query_posts('post_type=testimonial&post_status=publish&posts_per_page=10&paged='.
get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
But I want to query a post from a specific category. For eg. my custom post type is "testimonial" it has 3 categories like category1, category2and category3 . I want to show only the post of category3 in my page template. How can I do that? thanks
Use this
<?php $posts = get_posts('category=3&orderby=rand&numberposts=5');
foreach($posts as $post) { ?>
<?php the_title(); ?>
<?php } ?>
Or
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
query_posts( $args );
Uncomment either one of the below as per you requirement.
If you want to query using the id of the category use the first one and replace 99 with your category id.
If you know the slug of your category use the second one with your category slug(not name).
<?php
//query_posts('cat=99&post_type=testimonial&post_status=publish&posts_per_page=10&paged='.get_query_var('paged'));
//query_posts('category_name=your_category_slug&post_type=testimonial&post_status=publish&posts_per_page=10&paged='.get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
P.S: I recommend you to use wp_query() instead of query_post().
HI I have edited and make a template page of responsive theme to make some thumbnails of the features pics of the posts.
ALl it's okay I can see them, but even if the posts are 9 I could see only 5. If I add one ony I see the new one, there is something like "show only latest 5 posts" but I can't understand WHERE!
get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part( 'loop-header' ); ?>
<?php responsive_entry_before(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php responsive_entry_top(); ?>
<?php get_template_part( 'post-meta-page' ); ?>
<div class="post-entry">
<?php the_content(__('Read more ›', 'responsive')); ?>
<?php wp_link_pages(array('before' => '<div class="pagination">' . __('Pages:', 'responsive'), 'after' => '</div>')); ?>
</div><!-- end of .post-entry -->
(this is my added code)
<ul>
<?php
$posts = get_posts();
foreach($posts as $post) : setup_postdata($post);
?>
<li><div class="fotoBoxContent"><a class="fotoBox" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); the_title(); ?></a></div></li>
<?php endforeach; ?>
</ul>
<?php responsive_entry_bottom(); ?>
</div><!-- end of #post-<?php the_ID(); ?> -->
<?php responsive_entry_after(); ?>
<?php responsive_comments_before(); ?>
<?php comments_template( '', true ); ?>
<?php responsive_comments_after(); ?>
<?php
endwhile;
get_template_part( 'loop-nav' );
else :
get_template_part( 'loop-no-posts' );
endif;
?>
Try adding query_posts( 'posts_per_page=NUMBER_GOES_HERE' ); immediately before the <?php while (have_posts()) : the_post(); ?>
Replacing NUMBER_GOES_HERE with the amount of posts you want displayed. Use -1 to show all posts
Also within Wordpress itself Settings->Reading has a field where you can set Blog pages show at most
I'm still not sure what exactly you want to achieve but if you just want to have thumbnails of posts that are in the main loop, then you do not need to do additional query.
All you need to do is something like this:
1.) Before <?php if (have_posts()) : ?> you initialize some variable:
$thumb_data='';
2.) After <?php if (have_posts()) : ?>
$thumb_data='<ul>';
3.) replace your "added code" with this:
$thumb_data.='<li><div class="fotoBoxContent"><a class="fotoBox" href="'.get_the_permalink().'">'.get_the_post_thumbnail()." ".get_the_title().'</a></div></li>';
4.) After the main while loop, add:
$thumb_data='</ul>';
5.) Mow all the HTML code for the list of thumbnails will be in $thumb_data, so just echo this variable in the template where you want the HTML code to appear.
I am using a wordpress query to display 3 posts at random from a custom post type. I am using the following code which is working fine:
<?php $my_query = new WP_Query('post_type=my_post_type&orderby=rand&showposts=3'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php endwhile; ?>
<?wp_reset_query(); ?>
However, I want to mirror the same query below to show the same items once again. So two wordpress queries on one page, the first query picking 3 random posts and the second query showing the exact same results of the first query. Any help would be appreciated. Thanks
try this out :)
<?php $my_query = new WP_Query('post_type=post&orderby=rand&showposts=3'); ?>
<?php $i=0; ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php
$myPostVar[$i] = array (
'title' => get_the_title(),
'content' => get_the_content()
);
$i++;
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php foreach ($myPostVar as $Postvar) : ?>
<h2><?php echo $Postvar['title']; ?></h2>
<p><?php echo $Postvar['content']; ?></p>
<?php endforeach; ?>