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.
Related
I have a Custom Post Type called portfolio and a custom taxonomy portfolio_category that has a term called narrative.
I'm able to access the archive page using the URL /portfolio-category/narrative/.
I want the homepage to display all the items the same as on the narrative archive page without using a redirect.
I've added the following to functions.php
function custom_front_page($wp_query){
if($wp_query->get('page_id')==get_option('page_on_front')){
$wp_query->set('post_type','portfolio');
$wp_query->set('page_id',''); // empty
// fix conditional functions
$wp_query->is_page = false;
$wp_query->is_archive = true;
$wp_query->is_post_type_archive = true;
}
}
add_action('pre_get_posts','custom_front_page');
This is displaying all of the portfolio items on my homepage, but I would like it to be just showing the narrative items.
I've tried this in the php template file, but it isn't working either
<?php
$custom_query_args = array(
'post_type' => 'portfolio',
'portfolio_category' => 'narrative',
);
$custom_query = new WP_Query( $custom_query_args );
?>
How can I get just the narrative items for to show on the homepage?
You're on the right lines but you are not searching by custom taxonomy correctly. Searching by custom taxonomy is different than searching by categories.
If you take a look at the WP_Query documentation for searching by taxomony, you will see that you need to use tax_query to search posts for a custom taxonomy term.
You can use the code below in the template file you use for your homepage. The comments explain what each part does:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 5, // number of results to get, or -1 to get all of them
'tax_query' => array(
array( // important note, this is an array inside the tax_query arrays
'taxonomy' => 'portfolio_category', // your custom taxonomy
'field' => 'slug', // what to search, e.g. slug, term_id, name
'terms' => 'portfolio' // the term(s) to search for. If you want to search for multiple terms, you can use an array
)
)
)
// do a new query with your arguments
$portfolio_query = new WP_Query($args);
// if the query returns any results, loop through them and process them as required
if( $portfolio_query->have_posts() ):
while ( $portfolio_query->have_posts() ) :
$portfolio_query->the_post();
// do stuff with the post here, e.g. display the post...
endwhile;
endif;
// IMPORTANT! The custom query will overwrite the $post values that are used in the the_post etc.
// This restores the the current post in the main query - i.e. your homepage post.
wp_reset_postdata();
I'm creating a WordPress site for a theater. I have several performer pages featuring performers with images, bios, etc. I would like to feature one performer on the homepage as a "Featured Performer." I created a category in pages called "Featured-HP." I am trying to return just this one performer to use on the homepage. It is not working at all. Can this even be done?
Here is my code:
$args = array(
'post_type' => 'page',
'category' => 231,
'posts_per_page' => -1
);
// The Query
$featured_performer = new WP_Query( $args );
Then to display it...
while ( $featured_performer->have_posts() ) :
$featured_performer->the_post();
// CODE HERE TO SHOW IMAGE AND NAME
endwhile;
Nothing gets returned, though, so, I haven't even gotten to the image and name. Any help or advice would be appreciated. Thanks!
One of the category parameters in WP_Query is cat for single category id. So instead of category replace it with cat.
As follows:
$args = array(
'post_type' => 'page',
'cat' => 231,
'posts_per_page' => -1
);
You can also dig in a little bit deeper into the Category Parameters
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 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.
On one of my Wordpress pages (which is really an image blog site) I'm using masonry.js with the Wordpress function get_posts to dump all attachments to my blog posts and display them in a grid. This works fine. However, there's obviously a lot of images and I was hoping to use the infinitescroll.js with this. The only problem is that the get_posts function, outside the loop, doesn't retain the pagination and therefore the functionality of infinitescroll.js doesn't work.
Here is the code I am using to dump all the attachments:
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_attachment_link($post->ID, true);
the_excerpt();
}
}
?>
Is there anyway of adding in pagination to the original Wordpress get_posts() attachment dump outside of the loop, or can anyone think of a solution?
I've done something similar using the 'offset' parameter for get posts.
Basically with each new call to get posts, simply increase your offset amount by the amount of new thumbnails you want to display each time. When the number of thumbnails returned is less than your offset amount, you have reached the end of your posts.
Another solution is to use the pagination parameters of the Wp_Query class. See here for what these are.