I need your kind help. I want to seperate sticky posts from main loop (tempate : index.php) and want to add something after sticky posts before rest of loop. Please see the screenshot. Any tip for this custom loop? I'll be gratful.
Screenshot : http://imgur.com/a/e37Pj
Structure is something like :
— These are sticky posts
– sticky post one
– sticky post two
– sticky post three
++ Add something after sticky posts.
— The rest of main loop.
– normal post one
– normal post two.
– normal post three.
Thanks in advance.
Regards
– Pomy
Before standard loop in index you create your area create place where you want to load that "sticky" posts
in post page write this
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 'Sticky' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
This will get you posts from category you want, in this case you will get 5 posts from "sticky" category
Try this:
<?php
$args = array('posts_per_page' => 12, 'post__in' => get_option('sticky_posts'));
$stickyposts = new WP_Query($args);
while($stickyposts->have_posts()) : $stickyposts->the_post();
?>
<!-- sticky posts (max. 12) -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- -->
<!-- here you can add random text or a horizontal line with a new headline or s.th.
<!-- like that. This is the exactly place between sticky and normal posts -->
<!-- -->
<?php
$args = array('posts_per_page' => 12, 'post__not_in' => get_option('sticky_posts'));
$normalposts = new WP_Query($args);
while($normalposts->have_posts()) : $normalposts->the_post();
?>
<!-- normal posts without sticky posts (max. 12) -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
This will show 12 sticky posts on top of your page. After that you can enter some random text, or s.th. like that, followed by 12 "normal" posts, that are not sticky posts.
Try using an incrementing variable (ie. $counter++) along with a conditional statement within the loop to trigger what you want after looping pass all(3) sticky posts.
<?php
$count=0; //Count is 0 outside of loop
while ( have_posts() ) : the_post(); // Begin WP loop
if(!is_sticky()) : $count++; endif; // Start count for nonsticky loops
if(!is_sticky() && $count==1){
// Do stuff when the first none-sticky post is discovered inside the loop
// ie. Add a div, or insert a class name, etc.
}
// Here is an example of an actual implementation
if(!is_sticky() && $count==1): echo '<div class="not-sticky">'; endif;
// Loop countinues
endwhile; // Loop ends
if(!is_sticky(): echo '</div><!-- .not-sticky -->'; // Placed outside of loop or add inside a conditional statement within the loop to only run once
?>
...Years later, but I hope it may still help someone else who might be facing the same issue.
Related
I'm currently working on my first WordPress theme and I want to create a "Highlighted Post" section on my index.php. There are a lot of scripts for this but all seem to apply a permanent filter over the entire index.php. That is problematic since i want to show the whole posts on my index.php below. Is there a way to apply this special filter only for this section and not for the whole index.php?
If you could give us more info what determines this "Highlighted" posts we might be able to be more specific in solving this.
But in the mean time I'll guess it's two latest posts which means you could have two queries in the index.php
First one would be for "Highlighted" ones:
<?php
$args = array( 'numberposts' => 2 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>
<h2 class="news"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endforeach; ?>
This will give you excerpt of your post in this query.
What you could do then is limit excerpt to whatever length you'd like and add read more link if you want :)
function newExcerpt($more) {
global $post;
return '... Read more';
}
add_filter('excerpt_more', 'newExcerptReadMore');
function customExcerptLength( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'customExcerptLength', 999 );
And for other posts you could do another query but not using first two posts like this:
$count = 0;
$lastposts = get_posts();
foreach($lastposts as $post) : setup_postdata($post);
if($count > 1)
?>
<h2 class="news"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
$counter++;
<?php endforeach; ?>
This will just loop through the posts and skip first two posts.
If you have some other term or category or something else that determine Highlighted posts then you can use that in and pass it as an argument to get_posts($args).
I am new too wordpress and have one problem. I finaly understand post loop and it work well but have one problem. For example: I have two pages of my posts (all are 6). In one page i have five posts (excerpt only) and on secound page i have one post only and in this case (when on one of my pages i have only one post it change to content post but it need to stay excerpt). So my question is what can i change in my code to make it work well in this case when on some page left only one post?
It's my loop:
<?php if (have_posts()) : ?>
<?php if (($wp_query->post_count) > 1) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for excerpts-->
<?php the_excerpt() ?>
<!-- Do your post footer stuff here for excerpts-->
<?php endwhile; ?>
<?php else : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for single post-->
<?php the_content() ?>
<!-- Do your post footer stuff here for single post-->
<?php endwhile; ?>
<?php endif; ?>
<?php else : ?>
<!-- Stuff to do if there are no posts-->
<?php endif; ?>
Use the posts_per_page parameter to an array and start the loop.
$args = array( 'post_type' => 'post', 'posts_per_page' => 4,'category' => 2, 'paged' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
$content = get_the_content();
echo $content;
endwhile;
Here I have printed the 4 contents from the archive. So the page will only have 4 posts in it. You can change the value based on your need.
I have used and got the output. Hope this helps you too.
Ur second if condition should be inside while and while shuld be called once ..
Somthing like
If
While
Then if for count
Elseif
Else
End while
End if
I hope it wil help ..
Answer for all: $wp_query->post_count need to be replaced by $wp_query->found_posts and everything working fine now!
Im using advanced custom fields and have setup a custom post type for testimonials, on the testimonial page there is a relationship field (display_on_page) where you would select which page to display the testimonial on.
The issue is that the testimonial displays on everypage when a page is chosen
Does anyone know what I am doing wrong on the below please?
<?php query_posts( 'post_type=Testimonial'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $posts = get_field('display_on_page');
if( $posts ): ?>
<?php the_field('the_testimonial'); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
If I understood correctly, you should check if the value that you get from get_field('display_on_page') matches the current page ID.
(I'm assuming that the page ID is what is stored by the custom field you created).
If that's the case, you may query your posts filtering by custom field values, like such:
<?php
// Fetches current page ID, assuming that this is page.php or similar
$current_page_id = get_the_ID();
query_posts(
'post_type=Testimonial',
'meta_key' => 'display_on_page',
'meta_value' => $current_page_id // Only gets Testimonials that are set for this page
);
while ( have_posts() ) : the_post();
the_field('the_testimonial');
endwhile;
// Resets default WP Post Data *AFTER* the loop is complete
wp_reset_postdata();
?>
I'm attempting to have two loops on my archive-custom.php (it's for a custom post type) - one loop for featured post(s) and another for the rest of the posts.
This is the code I have come up with, however, it's not working correctly. At the moment, it doesn't display either loop and ends up actually breaking other PHP based elements.
Note: These loops are split up into different template parts - not sure if that matters or not. However, I have combined them into one chunk to make it easier to troubleshoot.
<?php $args = array (
'post_type' => 'community',
'category_name' => 'featured',);
// The Query
$community_posts_featured = new WP_Query( $args );
if ($community_posts_featured->have_posts()) : while ($community_posts_featured->have_posts()) : $community_posts_featured->the_post(); ?>
<div id="featured">
<--Featured Stuff Here-->
<?php the_content(); ?>
</div><!--End #featured-->
<?php endwhile; ?>
<?php $args = array (
'post_type' => 'community', );
// The Query
$community_posts = new WP_Query( $args );
if ($community_posts->have_posts()) : while ($community_posts->have_posts()) : $community_posts->the_post(); ?>
<div id="main-content">
<--Main Stuff Here-->
<?php the_content(); ?>
</div><!--#End Main-->
<?php endwhile; ?>
<?php else : ?>
<--Missing Content Stuff-->
<?php endif; ?>
There i can spot two problems:
1) You have opened 2 if statements and have just closed one of them
2) you'd better use wp_reset_query(); after the first loop
I am using this code to show 5 posts (any post's would do)
<div id="featured-post-section">
<div id="post-list">1
<?php query_posts( 'posts_per_page=5' ); ?>2
As you can see, I've added a 1 and 2 numbers.... One before and one after ... I only get 1 2 ... No posts are showing at all.
What am I doing wrong?
That function only tells Wordpress how many posts to output. You still have to use the WP loop to actually perform the output.
<?php
// The Query
query_posts( 'posts_per_page=5' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();