Displaying posts of a category wordpress - php

I am developing a wordpress theme from scratch. I have completed the design and am stuck at the listing of posts.
I am currently using:
$args = array( 'posts_per_page' => 10 );
$myposts = get_posts( $args );
It does display all the posts.
How can i display the posts in a category which is clicked?
I have no clue about how to achieve this. I tried to get the category name from URL and the pass it to the get_posts(). But i dont think this will be efficient because using different URL rewrite the URL can be changed.

If you write your code in archive.php wordpress understand is a category and automatically return related posts otherwise by code you need to pass category id.
Example:
$args = array( 'posts_per_page' => 10,'cat'=>1 );
$myposts = get_posts( $args );

Related

How to filter categories based on URL on category.php in WordPress

I have created a category template on my WordPress site that i want to use for all categories, but i having problems to filter the posts using WP_Query.
lets say i have a category named News WordPress will generate the following url for me: example.com/category/news
How can i filter the posts in WP_Loop to only show posts from the news category?
This is what i have:
$category = get_the_category();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'posts_per_page' => 10, 'paged' => $paged,'cat' => $category[0]->id);
$the_query = new WP_Query( $args );
But i'm pretty sure this is not the way to do it.
Thanks in advance for any help!
Get the slug of a current page, as mentioned in the link below. Then query posts with that category slug.
https://wordpress.stackexchange.com/questions/42117/how-to-retrieve-the-slug-of-current-page

Wordpress show multiple post types in loop

I'm creating a Wordpress site were I would like to show "tiles" with content from the site on the front page. These tiles are custom post types from the site like "our services", "consultants", "blog posts" and so on.
I know how to show one custom post type in Wordpress, but the problem is that I need to pull multiple post types in the same loop as I want them to be displayed in a matrix. Another problem is that I need to shuffle all the items in a random order, so that for example not all blogs just show in one place but all objects show after different items in random.
The third problem is that I need to show all items for a certain post type and just the latest for another. For example do I need to show all "our services" tiles, but only a couple of the "blog" tiles.
Is this possible to do, or can you not pull out records in this way using Wordpress?
Thank you for the help!
I suggest reading up on custom wordpress queries https://codex.wordpress.org/Class_Reference/WP_Query
For the first question you just need to specify
'post_type' => array( 'tiles', 'consultants', 'post' )
for the second question
'orderby' => 'rand'
so you will have something like
$args = array(
'post_type' => array( 'tiles', 'consultants', 'post' ),
'orderby' => 'rand'
);
$query = new WP_Query( $args );
For the third question - I'm not sure if it is possible to achieve with one query.
you can customise the things like this ,
$posttypes = array('post_typ1','post_typ2','post_typ3');
$randompost_typs = shuffle($posttypes);
$counter = count($posttypes);
for($i=0; $i<$counter;$i++) {
// suppose you want to show all posts from post_type1 then
if($randompost_typs[$i]=='post_typ1') {
$posts_per_page = -1;
} elseif($randompost_typs[$i]=='post_typ2') { // will work for 2nd post type
$post_per_page = 5; // show 5 posts from this post type
} else {
$post_per_page = 3; // show 3 posts from last post type
}
// here you will use the WP_Query class from wordpress
$args = array(
'post_type' => $posttypes[$i],
'orderby' => 'rand',
'posts_per_page' => $post_per_page
);
$query = new WP_Query( $args );
if($query->have_posts()) : while($query->have_posts()): $query->the_post();
// all the remaining wp loop content for example
the_title();
the_excerpt();
endwhile;
else:
echo 'no posts';
endif;
}
hope this will help, let me know if it has any issue.

Display latest articles inside of a post on wordpress

i was browsing some themes on wordpress, and i found one that is kind of a clone from 9gag.tv
You can see the demo theme here
http://codecanyon.net/item/youtube-viral-videos-9gag-tv-clone/full_screen_preview/6770578
If you see that page, you see the "latest videos" area below, that has the posts arranged by date, but when you open a video, that area changes and it gives you random videos.
Is there a way to arrange the videos like in the home page and allow user to browse to older videos?
I guess its easy to achieve it using the get_posts() loop. You'll also need to register new custom post_type for whatever posts you're going to post and fetch it through a loop like the example below.
I'm sure someone else will give a full answer but this will get you started.
http://codex.wordpress.org/Post_Types
http://codex.wordpress.org/Template_Tags/get_posts
<?php
$args = array( 'post_type' => 'custom_post_type', 'posts_per_page' => 10, 'orderby' => 'rand' );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $post ) {
setup_postdata( $post );
the_attachment_link( $post->ID, false );
the_content();
}
wp_reset_postdata();
}
?>
You can use wordpress php function get_posts() to fetch the posts, then echo the data you want using a foreach. The function also has the offset argument which you can use for the navigation.

Sort by category and then date on Wordpress homepage

My Wordpress site has multiple custom categories and posts types all which need to be displayed on the home page blogroll. But I need the first post on the homepage always to be the most recent post from a specific category.
For example the pst at the top of the page will always be the latest "giraffe" category; and below it; sorted default/chronologically are more "walrus" mixed with "seagull" and "nachos"
What would be the best way to accomplish this?
Without running through the Loop twice, sticky posts are your best option.
Otherwise:
...
$args = array(
'orderby' => 'post_date',
'category'=> 'giraffe',
'numberposts'=> 1,
);
$latest_giraffe_post = get_posts( $args );
...
then do what you're already doing for the rest

How can I loop through posts as well as child pages to display them all by date in Wordpress 2.9

Some background info --
In wordpress I have my portfolio as a parent page with each item of work a child page.
I also have a blog.
I want to display the most recent 6 items on the homepage whether they are from my portfolio or my blog.
I have a loop that displays the posts and on another page I have a loop that displays the child pages of a specific parent page. I only need to display the_title and the_post_thumbnail.
Is it possible to loop through both the posts and the child pages and display them in order of date (recent first).
So the final display would be a mixture of posts and child pages.
Could it be done by having a separate loop for pages and one for posts, then somehow adding the results to an array and then pull them out in date order (recent first).
Any help would be greatful.
Thanks
Use a custom query.
Example
$latest_content = new WP_Query(
array(
'posts_per_page' => 6,
'post_type' => 'any',
'post_status' => 'publish'
)
);
if ( $latest_content->have_posts() )
{
while ( $latest_content->have_posts() )
{
$post = $latest_content->next_post();
// Do anything you know about the loop.
print $post->guid . '<br>';
}
}
rewind_posts();
I have not tried it myself, but I think you can use the get_posts template tag with post_type => 'any'.
Like so (default ordering is date descending):
<?php
$args = array('post_type' => 'any', 'numberposts' => 6);
$posts_and_pages = get_posts($args);
?>

Categories