I would like to have my page show the 2 most recent posts, so I'm using WP_Query and setting posts_per_page to 2, which works great, but it kills the pagination. Here is my code. How do I alter it to show two most recent posts and keep pagination?
<?php $wp_query = new WP_Query( array( 'posts_per_page' =>2 ));?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div id="ind_post">
<h2 class="post-title"><?php the_title(); ?></h2>
<div id="entry">
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<p><?php the_excerpt(); ?></p>
</div>
<h4 class="more-post">continue reading…</h4>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>No results found</h2>
<?php endif; ?>
<?php wp_reset_query(); ?>
This should do it
<?php $wp_query = new WP_Query( array( 'posts_per_page' =>2, 'paged=' . get_query_var( 'page' ) ));?>
Related
I want to add a link when my post gets click on. The link has to go to the category of the post. I've got this far but now I'm stuck. Can anyone show me how I can do this?
<?php
$args = array(
'category_name' => 'portriats',
'posts_per_page' => 1
);
$qry = new WP_Query($args);
if ( $qry->have_posts() ) :
while ( $qry->have_posts() ) : $qry->the_post();
$postcat = get_the_category( $post->ID );
?>
<div class="hometile">
<a href="<?php get_category_link( $postcat) ?>">
==> **I need to get the category of the post and then let PHP print the link of the categorey in the href**
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php
endwhile;
endif;
?>
It will display primary category of post with link
<?php $qry = new WP_Query($args ); ?>
<?php if ( $qry->have_posts() ) : ?>
<?php while ( $qry->have_posts() ) : $qry->the_post(); $postcat = get_the_category();?>
<div class="hometile">
<a href="<?php echo get_category_link( $postcat[0]->term_id ); ?>">
<?php echo $postcat[0]->name; ?>
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
I am using WordPress 3.8. I want to get query post from a specific category. To do this, I have used the following code
<?php query_posts('post_type=post&category_id=3&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php endif; ?>
I am getting all post instead of a specific category. What is wrong with this code.
category_id=3 should just be cat=3
<?php query_posts('post_type=post&cat=3&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php endif; ?>
In general avoid using query_posts because it is altering the globals inside the main loop.
You can use get_posts():
<?php
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endforeach;
wp_reset_postdata();?>
take in acount that 1 is the id of category (look the id for the category you are interested to fetch on your DB)
Here you will find more info
I have been trying to alter my current code, but it makes no sense to me (it doesn't do what I was told it does). All I want to do is only display posts with category 13 on my homepage and I want to be able to add multiple categories to a post.
Post 1 (Category 13 and 1) = displayed on homepage
Post 2 (Category 13 and 4 and 5) = displayed on homepage
Post 3 (Category 6 and 1) = not visible on homepage
Post 4 (Category 2) = not visible on homepage
This is my current code to only show category 13 on my homepage, if another category is added to the post it won't be displayed at all.
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
query_posts("cat=-6,-4,-1,-11");
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat' => 13,
'posts_per_page' => 5,
'paged' => $paged);
query_posts($args);
?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
You're looking way too hard at this code. You don't need to exclude anything, you just need to include cat 13, which will include everything that is in cat 13; even if it is in other categories as well. Just run a regular WP_Query() like so:
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
// The Query
$the_query = new WP_Query("cat=13, paged=".get_query_var('paged'));
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
(had to add a few charachters or I could not edit, sorry :P)
Try this...
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&cat=13'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Your stuff goes here...
<?php endwhile; ?>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Try this code
<?php
global $post;
if (is_home()) {
$args = array( 'numberposts' => 5, 'category' => 13 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content( 'Continue reading <span class="meta-nav">»</span>); ?>
</div>
<?php endforeach;
}
?>
This will display the title and content. If you want additional information like date, author, category, tag etc, you may have to use other template tags. Also if you want to style these differently, then you have to use appropriate CSS.
I have this code:
<?php $q = new WP_Query(array(
'post_type' => 'oferty'
));
?>
<?php while ($q -> have_posts()) : $q -> the_post(); ?>
<!-- .post | id: <? echo $post->ID; ?> -->
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry">
<h1><?php the_title(); ?></h1>
<small><?php echo get_field('bank'); ?></small>
<?php the_content(); ?>
</div>
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<?php if($thumb) : ?>
<div class="bankimg" style="background-image: url('<?php echo $thumb[0];?>')"></div>
<?php endif; ?>
<div class="clear"></div>
</article>
<!-- /.post | id: <? echo $post->ID; ?> -->
<?php endwhile; wp_reset_query(); ?>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
It uses wp_query to list all the posts from post type oferty. I set in WordPress options limit of posts to 2 and it works - but no pagination shows up. I tried WP PageNavi, WP Pagination and normal WordPress' prev/next linsk.
Take a look at the pagination parameters when querying for custom post types:
https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
Specifically, try this:
$q = new WP_Query(array(
'post_type' => 'oferty',
'posts_per_page' => 2
));
I apologize for my lack of understanding of such a key concept, but it will sink in eventually!
This is the code on my index.php page. I want each portfolio "item" to link to it's appropriate single.php page which will show more info/images.
<?php
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 12));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>
<div class="item">
<?php the_post_thumbnail(); ?>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
<p>This is a link</p>
<p>Read more</p>
</div>
<?php endwhile; ?>
This is the code on my single.php page. I think my loop lines are wrong, as it's pulling in the same one entry no matter what item i click on on the homepage, but I don't know what they should be.
<?php $loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 1));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>
<div class="left">
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
</div>
<?php endwhile; ?>
Any tips for this Wordpress newbie would be greatly appreciated! NOTE: I know basic HTML and CSS, but PHP is a new concept for me.
You do still use the loop to load the_post() with the current post, you just don't need to run a custom query.
try:
<?php global $wp_query; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>
<div class="left">
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>