I need to exclude first post from wordpress loop on archive.php page. The loop code is this.
<div class="article-container">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'archive' ); ?>
<?php endwhile; ?>
</div>
There are several ways. You can use a counter or boolean variable. This example uses the latter (I don't know what you are using 'global $post_i; $post_i = 1;' for but you could use $post_i as a counter, increment it in the while loop and use a condition, if that was your intention):
<div class="article-container">
<?php global $post_i; $post_i = 1; ?>
<?php $show_post = false; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if($show_post === false): ?>
<?php $show_post = true; ?>
<?php else: ?>
<?php get_template_part( 'content', 'archive' ); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
Then hook into the 'pre_get_posts' and offset the query by 1. Add this to your functions file:
add_action('pre_get_posts', 'offset_posts_wordpress_archive', 50);
function offset_posts_wordpress_archive($query){
if(!is_admin() && $query->is_archive()){
$query->set('offset', 1);
}
}
Related
I am using wp-paginate plugin for pagination on my wordpress site. The pagination links are showing fine. The problem is every page shows a blank page.
I am using following code to call pagination. I am new to wordpress so sorry for any mistakes..
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php //twentytwelve_content_nav( 'nav-below' ); ?>
<?php if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentytwelve_content_nav( 'nav-below' );
}
?>
You have stared if block but not close it. Enable errors you will get error in php
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php //twentytwelve_content_nav( 'nav-below' ); ?>
<?php if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentytwelve_content_nav( 'nav-below' );
}
endif; //<--------------add end if
?>
Finally I found the solution. There was a page named page.php on my server's root folder. I deleted it and problem solved.
I need to display separate post with category id after 5th and 10th place. I searched for a query and the query shows,
<?php $postnum++;
if ($postnum == 5) { ?>
<div class="block">
<h2>Blog</h2></div>
<?php }
if ($postnum == 10) { ?>
<div class="block"><h2>References</h2></div>
<?php }
After putting the above code before endwhile, i can see the Blog and References after 5th and 10th place. But while inserting,
<?php $postnum++;
if ($postnum == 5) { ?>
<div class="block">
<?php
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while ( have_posts() ) : the_post();
?>
<?php the_title();
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<?php }
if ($postnum == 10) { ?>
<div class="block"><?php
query_posts( array(cat=>4, orderby=>post_date, order=>desc) );
while ( have_posts() ) : the_post();
?>
<?php the_title();
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<?php } </div>
<?php }
The above code doesnot seems to work correctly. Please anyone help me..
Could be a few things. Check your server logs, but at the very least
<?php the_title();
<?php endwhile; ?>
is wrong - either you need to add ?> after the_title(); or remove <?php before endwhile;
Similarly, this:
<?php } </div>
Looks like it's missing a ?>.
I'd probably add quotes around the query_posts() parameters as well, for clarity:
query_posts( array('cat'=>3, 'orderby'=>'post_date', 'order'=>'desc') );
And I'd read this too, just so you're confident query_posts is the right approach for you.
I don't know why but when I want to display the posts with comment opened, it shows posts which are among the last 10 posts (depend how much is in READING menu), so posts which are in the first page.
My code :
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post(); ?>
<?php
$PostID = get_the_ID();
if (comments_open( $post_id )) { ?>
<?php p2_load_entry(); ?>
<?php } ?>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
This is my file :
<?php
/**
* Static page template.
Template Name: test
*
* #package P2
*/
?>
<?php get_header(); ?>
<ul id="postlist">
<?php
query_posts( $args );
while ( have_posts() ) : the_post(); ?>
<?php
$PostID = get_the_ID();
if (comments_open( $post_id )) { ?><?php p2_load_entry(); ?>
<?php } ?>
<?php endwhile;
wp_reset_query();
?>
</ul>
<?php get_footer(); ?>
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();}
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; ?>