bpilot
El Presidente
Just Getting Started
Member Likes (0)
I can see in WP 3 default theme index.php file the following code is used to display posts:
get_template_part( 'loop', 'index' );
I was wondering, how I can:
1. Display posts from specific category?
2. Display custom fields related to each post?
Thanks in advance for your time
Responses (18)
Member (joined April 2009) Likes (0)
Hiya!
There's actually a few different ways to do this depending on what you're after.
You can create a custom query and put it anywhere within your WordPress theme:
http://codex.wordpress.org/Function_Reference/query_posts#Category_Parameters
You can also create category-specific page templates:
http://codex.wordpress.org/Category_Templates
For display based on a custom field check out this:
http://codex.wordpress.org/Function_Reference/query_posts#Custom_Field_Parameters
Really, the link without the link above is one you'll want to read and keep handy for reference time and time again. It's part of the real power with WordPress:
http://codex.wordpress.org/Function_Reference/query_posts
Thanks! :D
Member (joined September 2010) Likes (0)
Hi Mason,
Thanks a lot for your help. After reading the above links, I've written the following code which unfortunately brought to me the white screen of death whenever I try to load the page containing the code...so any idea what i might be doing wrong here?
Thanks in advance for your time
<?php
//The Query
query_posts('post_type=sofprod&cat=3&posts_per_page=5&offset=1');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
<p>Hello World</p>
endwhile; else:
<p>Hello World - 2</p>
endif;
//Reset Query
wp_reset_query();
?>
Member (joined April 2009) Likes (0)
Hiya,
Many things can cause a PHP error. You'll want to check your server's error logs and see where the problem is coming in. For instance, making sure you're dropping in your new code in the correct area of the php file where it's not in the middle of another function.
You also have to "echo out" html within a php expression or close your php tags before you do the standard html.
Here's an example of the loop using your query. This should work on it's own, but again, you'll have to check your error logs if things go blank. Give us an error code and we'll help ya troubleshoot from there.
Hope this helps. Thanks!
Member (joined April 2009) Likes (0)
Please note: in the post above I put in "." between h.r.e.f. on line 7 - you'll need to remove those 4 dots, but our code editor here was getting angry with me and trying to change it to a link. Sorry for the inconvenience.
Thanks!
Member (joined September 2010) Likes (0)
Hi Mason,
Thanks a lot for your continuous help and support. I've tried your code but unfortunately I always get 'No posts found' even though I am 100% positive there is at least 2 posts under this post type and under the selected category...
So I tried a small modification to code (as shown below) to include 'all categories' there just to make sure nothing slips, but still I always get 'No Posts found' message :( :(
So any ideas what I might be doing wrong here?
Thanks in advance for your time and efforts
<?php
//The Query
$args = array(
'post_type' => 'sofprod',
'numberposts' => 5,
'category' => array(1,3,4)
);
$args = array_merge( $args , $wp_query->query_vars);
query_posts($args);
//The Loop
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2>" rel="bookmark"><?php the_title(); ?></h2>
<p class="meta">Posted on <?php the_time('F jS, Y'); ?></p>
<?php the_content('Read More'); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<h2>No posts found</h2>
<?php endif; ?>
<?php wp_reset_query(); ?>
Member (joined September 2010) Likes (0)
btw I've also tried to write to run under the 'No posts found' message:
<?php echo ($args); ?>
and all I got as output was
Array
So any ideas what I might be doing wrong here?
Thanks again for your time
Member (joined September 2010) Likes (0)
Hi Mason,
After a lot of trials, I made it work through the following line:
query_posts(array('post_type' => array('sofprod','nonsoftp')),'&posts_per_page=5&cat=1,3,4');
Now I have a couple of questions here which I hope you can please help me with:
1. What did I do wrong in the array I posted earlier, as up to this moment I don't understand what did I do wrong there so that it won't work?
2. I have 3 pages, main home page which will display all posts except posts with post_type ('sofprod','nonsoftp') then I have a page that will display only posts with post_type 'sofprod' then a third page that will display posts with post_type 'nonsoftp'
Now I've read in WP website that:
So does this mean that I can use query_posts only once? and if yes what else is recommended to use beside query_posts?
Thanks in advance for your time
Developer (joined August 2010) Likes (0)
Since you were merging your array with the default query vars. Some of these default vars were still used. So WordPress was searching for posts which corresponded to your query + the default query.
No, you can use it several times, but it is not recommended.
Use get_posts or the class WP_Query. See the codex loop page for more details.
Member (joined September 2010) Likes (0)
Hi Ulrich,
Thanks a lot for your help and time
how can I exclude the 'default vars'?
Thanks in advance for your time
Developer (joined August 2010) Likes (0)
Just don't merge your args with the $wp_query->query_vars array.
Member (joined September 2010) Likes (0)
I didn't merge it...
Ulrich I am no expert in WordPress nor in php, so if you have specific comment on the code I posted please let me know as the default vars issue is quite confusing for me
Thanks in advance for your time
Developer (joined August 2010) Likes (0)
On this line:
$args = array_merge( $args , $wp_query->query_vars);your args were overwritten by the default query vars.
Member (joined September 2010) Likes (0)
Hi Ulrich,
I guess the problem is that you assume I am a wordpress / php expert so I must know it but unfortunately I am not :)
Anyway, I've spent two hours searching the web for what you have said but couldn't find anything concrete using the information you provided regarding the error...so thanks anyway.
If anyone else can please elaborate to me how I am doing the default query vars overwriting and how I can rectify this, it will be highly appreciated
Thanks all for your time and efforts
Developer (joined August 2010) Likes (0)
Bpilot, sorry if my explanation was not clear.
If you remove that line (
$args = array_merge( $args , $wp_query->query_vars);) from your code, it should work. That line is not necessary.For your information,
$wp_query->query_varscontains the default WordPress query parameters as an array. You may want to look at the definition of array_merge in the php docs to see why it overwrites your args array (http://php.net/array_merge).This is how I would rewrite your code:
<?php
$args = array(
'post_type' => 'sofprod',
'numberposts' => 5,
'category' => array(1,3,4)
);
$the_query = new WP_Query($args);
if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post();
?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a. .h.r.e.f.="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></h2>
<p class="meta">Posted on <?php the_time('F jS, Y'); ?></p>
<?php the_content('Read More'); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<h2>No posts found</h2>
<?php endif; ?>
I hope I have been more helpful this time!
Member (joined September 2010) Likes (0)
Thanks a lot Ulrich, highly appreciated :) Now everything is pretty clear :)
I've tested the code and now it works perfectly, only a minor problem I am facing in it...
When I tried to place code of page numbers plugin (WP Page Numbers - http://wordpress.org/extend/plugins/wp-page-numbers/) as follows:
<?php if(function_exists('wp_page_numbers')) : wp_page_numbers(); endif; ?>Switching pages doesn't change posts displayed...
I am not sure if this is a plugin or a code problem, so just to avoid searching in the wrong direction, do you believe that such problem is not related to the code we have here?
Once again thanks a lot for your time, highly appreciated
Member (joined September 2010) Likes (0)
Found the solution at http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/
Seems like that there is a long term problem between WP_Query and pagination...anyway the code in the link above solved the problem
Yet the question will remain....is this the best solution to solve the WP_Query problem or is there any other better solutions?
Thanks in advance for your time
Developer (joined August 2010) Likes (0)
You are welcome bpilot!
This issue is due to the fact that the pagination is not passed as argument in the code we have here.
Replace the args array by the following:
global $paged;$args = array(
'post_type' => 'sofprod',
'numberposts' => 5,
'category' => array(1,3,4),
'paged' => $paged
);
Member (joined September 2010) Likes (0)
Thanks a lot Ulrich again for your continuous help, highly appreciated :)
WordPress Questions?
We've got answers!
Find out more »