I am trying to display posts ordered alphabetically by title, only for a certain category. I have tried to follow the instructions in the Codex but I am confused because my code in the template pages looks quite different from the examples in the Codex.
I have a category named "designers" so I duplicated the category.php and named it category-designers.php. Inside, there is a call to a loop-designers.php
Inside the category-designers, I have tried the Codex pice of code:
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
$glossaryposts = get_posts( $args );
foreach( $glossaryposts as $post ) : setup_postdata($post);
get_template_part('loop', 'designers');
endforeach;
But the output is weird: first it displays a list of posts ordered by date, then it displays the same posts but ordered alphabetically, only that the alphabetically ordered list is repeated as many times as posts are (9 in this case).
I know I must be doing something terribly wrong but can't find examples using the get_template_part, they all use a foreach just like in the Codex.
Thanks for your answers.
Edited: in my loop-designers.php I have basically the same as in the loop.php, but with modifications so for that category no date, tags or other info are shown. I pasted the HTML here http://jsfiddle.net/6qdvF/
With the piece of code you have there you are including a loop in a loop. Using get_posts() with a foreach loop is essentially a WP loop, then you are including get_template_part('loop', 'designers'); which is more than likely another loop.
You can remove get_template_part('loop', 'designers'); from your piece of code and just stick in your tags to get the desired content, (i.e the_content(); the_title; the_excerpt; etc..) or you can create a new loop with wp_query() like the below example.
<?php
$query = new WP_Query(array('post_type' => 'post', 'cat' => 1, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC'));
while ( $query->have_posts() ) : $query->the_post();
?>
// put your content template tags here
<?php endwhile; wp_reset_postdata(); ?>
This loop will query "Posts" and category 1. You can change 'post_type' => 'post', to query other post types by changing the word post to the post type name. Or change the category number to the desired category ID you wish to query.
Related
Is there a way to programmatically trigger the standard displaying post list from a custom WP_Query, just like a "category" menu item does?
To clarify:
i'm not looking for plugins like List category posts that do the work but with a custom built-in template..
I need the wp's(theme's) standard post list rendering loop to be triggered !
Thanks!
Put this code where ever you need to render list of post from category "example_category_slug", (or any other taxonomy).
$wpq = array ('taxonomy'=>'category','term'=>'example_category_slug');
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<ul>";
if ($article_count){
while ($myquery->have_posts()) : $myquery->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
}
echo "</ul>";
or use simpler query, like $myquery = new WP_Query ('cat= 11, 9'); for cat ids, or any other wp_query
But while choosing where to put it, consider Template Hierarchy as a set of rules which template file is right to use.
If you need a list of categoires insted post use wp_list_categories
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list'
);
wp_list_categories($args);
?>
ful ref. in wp codex
Here is a way to do it..
inside a shortcode's function, or in a function that act ajax style, put a code like that:
global $wp_query;
$cat = $_GET['categoria'];
$wp_query->init();
$wp_query->query(array(
'posts_per_page' => 4,
'category_name' => $cat
));
get_template_part( 'blog', 'columns' );
wp_reset_query();
die();
the get_template_part arguments are theme dependent and can be desumed from php template names from theme's root directory.
in this case wp will call [theme's_dir]/blog-columns.php..
i reccomend the lecture #Michal reccomended me
I am trying to find a way to achive the following: I'd like to make a custom template. Within this template I'd like to list the name of each category on my site that has at least 1 item assigned to it. Beneath the category name (and link) I'd like to insert some content from the first x number of items that have that particular category assigned.
Just in case it makes a massive difference the items in question are not posts, but custom items.
Can anyone give me some pointers or help? I assume there will be something I can do with the wp_query function, but I'm not really sure how to inject it between each of the category titles, or indeed how to make it work with a category for which I can't explicitly provide an id in the code).
Thank you.
get_posts() is your ideal solution. For example:
<?php $posts_array = get_posts( $args ); ?>
<?php $args = array(
'posts_per_page' => 1,
'category' => $post_cat,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'yourcustompostname',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );
///respective loop here ie : foreach $myposts as $mypost ... etc
?>
Where $post_cat is the id of your category and post_type is the custom post type.
To keep these 'items' organized I would create a custom post and then assign a category to those posts, for each respective category (if there are multiple). If there is just one, than you could live with just using a single category.
EDIT:
to get the categories assigned to this custom post take a look at this link
Using the results from this you could assign the cat id's to the variable created earlier: $post_cat.
You have to use query_posts function
query_posts(array('category_name'=>'category-slug','posts_per_page'=>'5'));
then you need to do
while ( have_posts() ) : the_post();
and you can get the post id using:
$p_id = get_the_ID();
for more information : query_posts
You can use get_categories()
http://codex.wordpress.org/Function_Reference/get_categories
But, then you need to find out where the data for 'items' is stored, and query depending on that.
Here is the answer of your problem. You need to change the category id of own post category id .
Here is the code. just copy and past it.
$query1 = new WP_Query( array( 'post_type' => 'post','cat' =>'10,9') );
Use the above query in loop and after loop,reset your query . see the below code.
wp_reset_query();
Please use it and let me know if needs anything else.
I'm making my own template for my portfolio. This is my first from scratch up theme in Wordpress so bear over with me, if I don't talk the lingo or know what things are called... ;-)
I've made a frontpage where I want to loop through the last posts in category "Featured" and the last pages with parent page "Cases", limited with 5.
I know how to loop through a list of category posts, but how do I combine that with latest pages?
Here is a loop to get posts that are a child of:
$args = array(
'post_type' => 'page',
'numberposts' => 5,
'post_status' => 'publish',
'post_parent' => 33, // change this to the ID of the page you need
);
$posts = get_posts($args);
if ($posts) {
foreach ($posts as $post) {
setup_postdata($post);
// Your PHP code here.
}
}
All of the Wordpress post getting functions will return the posts in an array. If you want to "combine" them you can either do an array_merge and put the posts from the category loop and the page loop in the same array and iterate through them, or you can do multiple foreach or while loops.
I have used GD Star Rating plugin with my Wordpress installation, for creating 2 different sets of Ratings (Posts and Videos). (I was not able to find any other plugin that supports this feature).
I have used following arguments in one wp_query to display a list of top rated videos, and it's working fine.
$args=array(
'post_type' => 'youtube_videos',
'post_status' => 'publish',
'posts_per_page' => 10,
'gdsr_sort' => 'rating',
'gdsr_multi' => 3
);
On the same page, i am trying to show a list of top rated posts, with following arguments in wp_query, but it's not working.
$args=array(
'posts_per_page' => 4,
'post_type' => 'post',
'gdsr_sort' => 'rating',
'gdsr_multi' => 0
);
I found that issue is with value passes in 'gdsr_multi' varibale, if i remove this variable from the top rated videos 2nd part of code works fine.
I am not being able to run both together, any suggestions please?
For gdsr_multi variable documentation of GD Star rating plugin says:
For standard rating data, don't use this variable, or set it to zero.
To get multi ratings data this variable must be set to multi set id to use.
You should reset your query before start a new one. wp_reset_query(); will destroys the previous query used on a custom Loop. This function should be called after The Loop to ensure conditional tags work as expected. As example:
<?php
query_posts( 'post_parent=5' );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
?><?php the_title() ?><br /><?php
endwhile;
endif;
wp_reset_query();
//your second query will be here.
?>
For more details, you can check Wordpress official codex: wp reset query
I am busy building a small theme options page for one of clients and need some help with an issue.
currently i have the option to manually put in IDS of wordpress pages to extract the data with query_posts
based on the theme options is creates a variable called $euro_box_1_vehicles;
my options are filled in as 32,39,43,54 in the input, and when I print this statement with echo, I get the same result.
When I just replace array(32,39,43,45) with array($euro_box_1_vehicles) it only returns one result.
<?php
$vehicle1 = array(
'post__in' => array(32,39,43,45),
'post_type' => 'page',
);
query_posts( $vehicle1 );
while (have_posts()) : the_post();
?>
When I echo var_dump = string(11) "32,39,43,45"
In which case, you need to explode $vehicle1, since post__in expects an array;
query_posts(array(
'post_type' => 'page',
'post__in' => #explode(',', $vehicle1)
));
Update
When I just replace array(32,39,43,45) with array($euro_box_1_vehicles) it only returns one result.
Shouldn't you replace array(32,39,43,45) with $euro_box_1_vehicles not array($euro_box_1_vehicles)? The latter seems it would make a nested array with one argument, i.e. array(array(32,39,43,45)). Which is not what you want.
Old Answer....
If I read you right then query_posts() expects a list of IDs? (32,39,43,45)
But when you pass it $vehicle1 you are not giving it a list of IDs, but a 2-dimensional array.
<?php
$vehicle1 = array(
'post__in' => array(32,39,43,45),
'post_type' => 'page',
);
query_posts( $vehicle1['post_in'] ); //use sub-array that contains list
while (have_posts()) : the_post();
?>