Loop in custom taxonomy page not picking up any post - wordpress

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 :)

Related

Page with posts of specific category

I need the option to build a page showing all posts of a specific category.
Showing all posts of a category can be done out-of-the-box by wordpress, I know. But I need the possibility to put some information about all those posts.
I know there's a plugin called "List category posts" (http://wordpress.org/plugins/list-category-posts/). It works but it's only showing the links to the posts. I need the full posts (like they are shown on the "blog page").
If you need to "do something" to results, look at
query_posts
via http://codex.wordpress.org/Function_Reference/query_posts
Here is a sketch that I think leans towards your needs using a custom loop. This can be inserted as needed via simple logic in your template:
// this sets up the filter parameters for a category id some_cat_id sorting asc
$args = array(
'cat' => $some_cat_id,
'order' => 'ASC'
);
// The query is applied via a conditional
if($some_conditional) { // for what ever reason we use the filter+args
query_posts( $args );
// this is also an opportunity to "do stuff" before the loop/output
}
// The Loop (simple example)
while ( have_posts() ) :
the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
As a long time WP user I avoid plugins at all costs in preference of writing sustainable code. Plugins are a point of failure and some of the biggest plugin factories out there are nothing but security issues wrapped in sugar.
Custom loops via conditionals using query "filtering" is amazing and this pattern can be extended to category, search, tags, and meta key:value pairs.
Additionally, by understanding the loop the formatting and output can be controlled in a manner that is easy to sustain. Some of the plugin logic is horrid and very inefficient, so always investigate any and all plugins when performance and security are important.
Here's what I find to be the most simple way to do this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//You can change up the format below any way you'd like.
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
</li>
<?php endwhile; endif; ?>
You can add this to a theme template file and all you need to change is the category id to the category you are trying get posts from. For example if your category id is '114' and you would like to show 9 posts it would look like the following:
<?php query_posts('cat=114&showposts=9'); ?>
If you need to add more info to the posts you should consider using custom fields to do that. Check out the plugin called Advanced Custom Fields.
Here is an example of a custom field being used in a loop:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
<?php $article_link=get_post_meta($post->ID, 'article-link', true);?>
<?php if ( $article_link ) : ?>
<?php else : ?>
<?php endif; ?>
</li>
<?php endwhile; endif; ?>
In the above example, if the custom field 'article-link' has a value, then that value (a URL) is used as the href in a link instead of the permalink of the article.
Hope I have helped!

Restrict a Wordpress Template to show only one category

I tried everything found in the web, but i'm getting errors or that's not what I am searching for...
I have to make a PAGE in wordpress, to show ONLY the posts of the category with a certain ID ( in my case id=8 )
i tryed to edit the loop-xxxx.php .. the template file... everything but I get always a problem
navigation system doesn't work. I mean... getting back to older posts won't work cause the output shows the last posts instead of older one.
The code I'm using in the loop or in the template file is:
<?php
query_posts('cat=8');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
i tried inserting it before the
<?php while ( have_posts() ) : the_post(); ?>
in loop.php
or before the call of loop inside the index.php
please help me :\
One solution is to use a custom WP_Query. In the custom page's TEMPLATE file, where ID is the id of the targeted category:
<?php $tmp_query = new WP_Query('cat=ID');
while ( $tmp_query->have_posts() ) : $tmp_query->the_post();
the_content();
endwhile;
wp_reset_postdata();
?>
Check this.
<?php query_posts($query_string . '&cat=8'); ?>
<?php if (have_posts()) : ?>
<optional> You can write here: "You are in category X". </optional>
<?php while (have_posts()) : the_post(); ?>
Good luck.

Page template, single page display

I've built a custom page.php template. Very simple, essentially:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php get_footer(); ?>
I've created a few pages, and if I visit their url, I just get the same page title.
I read up in the documentation, and it says to use the_title() and such only when in "the loop".
So presumably, I'm just being shown the first page in the "array".
Is there any way I get get the contents of a single page based on the url?
Edit: In fact, should I even need to do this? Refering to example templates, it looks like I'm doing everything right?
In my sidebar I was using a custom query.
This was called before trying to access the main page content, without resetting.
When doing a custom query you must reset after you've finished your loop like so:
$originalPost = $post;
$sidePosts = get_posts($queryArgs);
foreach($sidePosts as $post) {
setup_postdata($post);
// echo it out like a normal post.
}
$post = $originalPost;
or if you are using query_posts() (which you shouldn't in a sidebar):
wp_reset_query();
Which will take your post back to it's previous value.
For a custom page template please use the following to get everything correct
<?php
/* Template name: My custom template */
get_header();
if ( have_posts() ) while ( have_posts() ) : the_post();
the_title();
the_content()
endwhile;
get_sidebar();
get_footer();
?>

Wordpress loop > unique loop renders slightly wrong results

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...

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