Wordpress loop > unique loop renders slightly wrong results - wordpress

A few things to understand before my question will make sense:
I use a hidden category called 'Unique' to specify if the post will use the single.php or a special one used for the unique ones.
I want the index to act as a single: showing only one post, displaying next/prev post links, and comments also.
I need the index.php to say if the post is in category 15 (unique) than <the_unique_content>, else; <the_default_content>
My loop does all this, but the problem is that if the current post is unique, it also displays 1 additional post below the unique post.
Here is the loop >
<?php $wp_query->is_single = true; ?>
<?php $post_count = 0; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ($post_count == 0) : ?>
<?php if (in_category('15')) { ?>
<?php the_content(); ?>
<?php } else { ?>
<?php the_content(); ?>
<?php $post_count++; ?>
Thanks for any help!

I don't think you are setting the query correctly to return a single post. Your code is limiting the number of posts through your $post_count variable, but in the case where the post is "unique", it only increments to 1 on the second post.
Here is one way to limit the number of posts to a single post. It involves modifying the loop query to set the number of posts per page to one.
<?php
global $wp_query;
$new_query = array_merge( array( 'posts_per_page' => 1 ), $wp_query->query );
query_posts( $new_query );
if (have_posts()) : while (have_posts()) : the_post(); ?>
etc...

Related

wordpress, get second newest post

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.

wordpress do not duplicate post

I know how to use few loops without duplicate posts.
But My question is:
Suppose I have two loops, each two loop shows 1 post only, both of them have the same newest post. if I use the code below, the duplicate post will be not shown at the second loop, but it is also stop to continue with the next post. how to solve it. million thx!
code:
<?php $my_query = new WP_Query('cat=1,2&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
first loop
<?php endwhile; ?>
<?php query_posts('cat=10&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); if (in_array($post->ID, $do_not_duplicate)) continue;?>
second loop
<?php endwhile; endif; ?>
I think you need to clean the example up a little bit:
You have $do_not_duplicate = $post->ID;, assuming your variable is the String post_id, then you check if (in_array($post->ID, $do_not_duplicate)) continue;
At this point $do_not_duplicate is NOT an array().
If you want to store an array(), try this: $do_not_duplicate[] = $post->ID;, then you can perform your current check.

Loop in custom taxonomy page not picking up any post

I have created a custom post type calles articles and a custom "category like" taxonomy called areas which seem to be working fine. The problem is that in my taxonomy-areas.php page they don't show up if I use the regular loop, I have to explicitly append "post_type=articles" to the query string for it to work. Shouldn't this be picked up by default?
My taxonomy page looks like this:
<?php $paged = (get_query_var('page')) ? get_query_var('page') : 1; ?>
<?php global $query_string; query_posts($query_string . '&post_type=articles&paged=' . $paged); ?>
<?php if (have_posts()) while (have_posts()) : the_post(); ?>
// Handle loop
<?php endwhile; ?>
I just fixed it with this: http://walrusinacanoe.com/web-development/742
And removed the 2 extra lines on top of the loop :)

Dynamically display posts by one author AND from one category on the same page?

I'm trying to edit my author.php wordpress template so that it shows posts by any one author, but only from one particular category. So far, I've been trying the query_posts function which fetches the category okay, but not the author. Depending on which way I do it, so far the posts either don't display at all or all posts in that category appear regardless of the author.
This is the appropriate code which I've seen quoted by a wordpress.org admin, but it doesn't work for me and I can't find any other examples. Any ideas why that doesn't work? Thanks for your help in advance.
//Gets author info to display on page and for use in query
<?php
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
?>
//Queries by category and author and starts the loop
<?php
query_posts('category_name=blog&author=$curauth->ID;');
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
//HTML for each post
<?php endwhile; else: ?>
<?php echo "<p>". $curauth->display_name ."hasn't written any articles yet.</p>"; ?>
<?php endif; ?>
============ ALSO TRIED ============
<?php
new WP_Query( array( 'category_name' => 'blog', 'author' => $curauth->ID ) );
?>
This doesn't work either, however it does filter the posts by author, just not by category! What am I doing wrong?
Thanks!
This task can be done using pre_get_posts filter. By this way it's also possible to filter for author in addition than for category:
// functions.php
add_action( 'pre_get_posts', 'wpcf_filter_author_posts' );
function wpcf_filter_author_posts( $query ){
// We're not on admin panel and this is the main query
if ( !is_admin() && $query->is_main_query() ) {
// We're displaying an author post lists
// Here you can set also a specific author by id or slug
if( $query->is_author() ){
// Here only the category ID or IDs from which retrieve the posts
$query->set( 'category__in', array ( 2 ) );
}
}
}
I just had this same issue, which I solved using a check for if(in_category('blog')) after the loop started, like this:
if ( have_posts() ) : while ( have_posts() ) : the_post();
if(in_category('blog')) {
?>
<!-- Loop HTML -->
<?php } endwhile; else: ?>
<p><?php _e('No posts by this author.'); ?></p>
<?php endif; ?>
Of course the $curauth check would come before this.

How to Sort Wordpress Posts Horizontally, Calling by Category

I am using the following code to try and display posts from only a certain category horizontally in three rows. I have the horizontal display issue figured out (using css) but with the following code it displays all posts and not posts from specific category.
<?php query_posts('showposts=5'); ?>
<?php query_posts('cat=7'); ?>
<?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
Any help would be greatly appreciated.
You're misunderstanding some concepts in query_posts and get_posts.
query_posts is to be used inside the loop. get_pages isn't. If you want to use query_posts, you don't need to create the get_pages call. Use query_posts or get_pages to accomplish what you're trying to do.
You need to combine your category parameters in query_posts.
<?php
query_posts('showposts=5&cat=7');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
If you want to do the same logic but without The Loop, just call
$posts = get_posts('numberposts=5&offset=0&category=7').
Read the links I provided. They have all information you need to understand how to do what you need.

Resources