We have selected to show 5 posts per page in admin panel.
And we want to show 10 posts per page in specific category (for example, "projects" width id=2).
How would we do it?
Change the normal loop to a query post. Like
if ( is_category(2) ){
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else:
echo'Nothing here...';
endif;
//Reset Query
wp_reset_query();
}
This should be used in the main loop only. . If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead.
Cheers
Pali Madra
I had the same problem.
This worked for me:
if ( is_category(2) ){
global $query_string;
query_posts( $query_string . "&posts_per_page=500" );
Related
In my theme, I have a custom post type called sliders. Here the user is allowed to upload images in a meta-box. So, the images are saved as meta data in wp_post_meta table.
Now what I want to do is, displaying a slider using its ID.
I did like following but no result
$my_query = new WP_Query('post_type=sliders&p=411');
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
the_post();
endwhile;
}
This doesn't display anything. No even errors. Anyway, If I used the_title() instead of the_post(), it shows the title of the slider fine. Same for the the_author() It shows the author without error.
Why is this weird ?
According to Codex the_post(); :
the_post(); iterate the post index in The Loop. Retrieves the next
post, sets up the post, sets the 'in the loop' property to true.
This function does not return any values.
Example for how we use this function:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
he_title();
the_content();
}
}
?>
You didn't explain well you question. anyway to display your metabox just use API function
echo get_post_meta( $id, 'metabox_name', true );
This is code from my wordpress homepage template. There are 3 same code, like this one bellow. The question is how can I put different categories to show different news when I post. At the moment code is same on my homepage and it show only category named World, it's the first category created. How can I setup the rest of code with different categories.
Currently it's like this : http://zaslike.com/files/os8hvxg38oendfwcvrz6.png
I need to put other categories instead same three.
http://justpaste.it/e0nh - Here is the code of homepage template.
I believe you are asking for this:
<!-- Your Query -->
<?php // The Query
query_posts( array ( 'category_name' => 'change-to-category-name', 'posts_per_page' => 1 ) ); ?>
<?php // The Loop
while ( have_posts() ) : the_post();?>
// YOUR HTML GOES HERE
<?php endwhile; ?>
<?php // Reset Query
wp_reset_query(); ?> // DON'T FORGET THIS
Use this code at the parts of the site that display your categories change the categories name to the ones you want to display and you are good to go.
Good luck welcome to SO.
I am trying to build a kind of archive of my client's projects, to display at the bottom of each page, exactly like is done in the 'Experience' section here: http://toth.com/#experience - except that in my case I only need the full list of projects, not the sub-headings or any other structure.
I have things setup so that each of my client's projects is a post. So I need a way to display the titles of the posts, from the category I've created 'Work Archive' (so that the client can add and remove things from the archive easily, by checking/unchecking the category box in each post), in vertical alphabetical order, across four columns which automatically resize to fill the container equally. Each post title in the archive also needs to link to the post, obviously.
I have been scouring the net for this for days, and while I've found pieces of code which look like they'll help, it seems impossible (with my limited PHP knowledge) to integrate them to fulfill all of my requirements. I have also looked into many WordPress plugins, with again no success. While I'll accept any solution, this is something that ideally I'd rather solve at the PHP/template level, to keep things as hidden from the client backend as possible.
Any help on this VERY much appreciated.
It sounds like the best way to do this might be to set up a new WP Query object. More info on this here:
http://codex.wordpress.org/Class_Reference/WP_Query
<?php
$args = 'category_name=work-archive&orderby=title&order=asc&posts_per_page=9999';
// assuming 'work-archive' is the slug to your category, we are also doing ascending order by title (a,b,c,d), and pulling 9999 posts (hopefully that is more than the number of posts you have!)
// The Query
$query = new WP_Query( $args );
// Keeping track of the count
$count = 0;
// Number of items per column
$num_per_column = round($query->post_count / 4); // dividing total by columns
// The Loop
if ( $query->have_posts() ) : ?>
<ul>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( $count % $num_per_column == 0 ) : // If the current count is up to it's limit throw in a new column ?>
</ul>
<ul>
<?php endif; ?>
<li><?php the_title() ?></li>
<?php $count++; // Increment counter ?>
<?php endwhile; ?>
</ul>
<?php endif;
/* Restore original Post Data */
wp_reset_postdata();
?>
Finish it off w/ some CSS!
To fix the opened ul change the condition for this :
if ( $count % $num_per_column == 0 && $count != 0)
It will prevent the closing of the ul on the first pass
I am using a while loop
while (have_posts()) : the_post();
to get all the post and displaying a page. How to randomise the post. But the problem is, in the page I am working on there is no sign of 'query'. Also I tried to use some solutions mentioned but no result.
You can alter the main query with query_posts($args) this way:
// Alter the query
query_posts( 'orderby=RAND' );
while ( have_posts() ) : the_post();
// Do your stuff inside the loop
endwhile;
// Reset the query
wp_reset_query();
I create a loop in Wordpress with the condition like this:
Display a specific post format (example: video)
Limiting the post number. In this case, I only want to display 2 posts.
Here my code:
<?php $i = 1; if (have_posts()) : while (have_posts() && $i < 3) : the_post(); ?>
<?php get_template_part( 'content-video', get_post_format() ); ?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I already have file name content-video.php.
Unfortunately, the loop does not work. Only displayed the first post, not a specific post (video post format) with template from content-video.php.
Appreciate for any help, or alternative code. Thanks in advance.
My remarks about your code:
I think you have bad wrapping for if else while statements. You are missing an endif at the end in the case your approach is correct.
Why use inside your code the i variable since you can customize any query in WP, especially for number of post using the parameter posts_per_page.
Why not to use the loop inside content-video.php and write only:get_template_part('content-video', get_post_format()); Like in single.php and loop-single.php the themes provided with wordpress installation (twenty themes) .
Good luck
the best way would be to get posts of "video" post format from a query, but I'm not sure how:)
As a workaround you could do this inside the loop:
if (!has_post_format('video'))
continue;