I was wondering how to get the title/img/content of some random article, but from specific category.
For an example:
I have 3 categories A,B and C and I have a image slider on my blog.
I want to show on the slider ONLY those articles who are in category A, not B and C.
How can I make that happen ? :)
The below example should get you started. It basically calls the get_posts() function with some criteria.
Return 5 Posts
Random Order
From Specificed Category
Then we run a foreach on the returned posts to do what we want. You don't have to run a foreach, in the below example $rand_posts will hold an array post objects with which you can do what you want.
You can take a look at the codex and change the arguments, criteria, to whatever you'd like.
Wordpress Codex - Get Posts
<?php
$cat_id = // Your category ID.
$args = array('numberposts' => 5, 'orderby' => 'rand', category => $cat_id);
$rand_posts = get_posts($args);
foreach($rand_posts as $post) : ?>
<li><?php the_title(); ?></li>
// Access all other post information here just like in a normal look. (Ex. the_content(), the_excerpt(), etc, etc
<?php endforeach; ?>
Related
For example I've got the Metals category in my WordPress installation with different metals names posted here and there within this category in different posts, like Metal (brass), Metal (aluminum) and so on.
What I need is to output all unique mentions of all metals from all posts from that category, skipping specific ones.
As far as I understand, I should create an array with all metal mentions, sort it like I want, keep only unique names and then output the array's content using
<?php foreach($my_array as $key => $value) {echo $value.', ';} ?>
There is no problem for me to prepare the final array using something like
<?php $my_array = array_unique(($metals_array), SORT_REGULAR); ?>
but I have no idea how to fill this array with required data, searching posts for Metal (*), and skipping, say, Metal (steel) in the output.
Please try this:
<?php
$title_array = array();
$args = array('post_type' => 'post', 'category_name' => 'metals', 'orderby'=> 'title', 'order'=>'ASC' );
$query = new WP_Query($args);
if($query->have_posts()):
while($query->have_posts()) : $query->the_post();
$title_array[] = get_the_title();
endwhile;
endif;
$unique_array = array_unique($title_array);
if($unique_array):
foreach($unique_array as $unique) {
echo $unique;
}
endif;
?>
It is the query for simple posts based on category but if you have created a 'Custom Post Type' then the query will be changed, otherwise it will work for you.
'category_name' => 'category-slug'. Category name consists the category slug.
Hope this may help you.
I use this code to show different posts in different divs.
I have 2 problems:
I can't show the correct category name to current post.
The same category name is applied to 2 post when I use:
get_category_link($recent['ID'])
Clicking on the post title redirect me to home page instead post's page!
<div class="modulex">
<?php
$args = array('numberposts' => '1', 'post_status' => 'publish', 'offset' => '2');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {?>
<div><?php echo get_the_post_thumbnail($recent['ID'],'small', array('class'=>'img-fluid')); ?></div>
<div class="spanlike"><h6><?php echo $recent["post_title"] ?></h6></div>
<?php } ?>
</div><?php
wp_reset_query();
?>
The problem with this is that get_category_link requires a category ID as argument, not post ID. To get around this, you'll have to perform several steps:
retrieve a list of categories for your post
decide which category to use from the list (assuming you only have one category per post, just use the first/only one)
get the link for this category
I would suggest using a custom function for this. Example below.
Here I think your problem is that get_permalink returns rather than echos. You could use the permalink instead:
<div class="spanlike"><h6><?php echo $recent["post_title"] ?></h6></div>
Example function for retrieving category link from post ID:
function get_cat_link_from_postID($postID) {
$categories = get_the_category($postID);
$catID = $categories[0]->term_id;
return get_category_link($catID);
}
I'm running Wordpress 4.1. I have two blog pages on my site, and though I don't really know php, I've done some tinkering and figured out how to modify the page templates so each page only displays posts for a specific category. That bit of code looks like this:
<?php query_posts('cat=2'); ?>
That works fine. Page A displays posts from category 1, and Page B displays posts from category 2.
What I'd like to do is disable post title links for one specific category. In other words, Page A would display posts from category 1 (with standard clickable title links), and while Page B would display posts from category 2 (with non-clickable title links).
I'm an HTML/CSS guy, so really out of my depth here, but if there's a way to modify the loop to achieve this, I'd love to learn how. Thanks in advance for any help.
Yes, you can do this using the category.php theme file. When this page is hit, it loads a specific category requested and the posts that fall into that category.
Your theme and loop may look something like this:
<?php single_cat_title(); ?>
<?php echo category_description(); ?>
if (have_posts()) : while (have_posts()) : the_post();
/// display posts from specific category
endwhile; endif;
Or if you don't want to use that page which is designed for that, you can create your own loop:
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => 50 ) );
All together like this:
<?php
/* retrieve unlimited # of posts with an category slug of music */
query_posts( array ( 'category_name' => 'music', 'posts_per_page' => -1 ) );
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
the_content( 'Read the full post ยป' );
endwhile;
?>
I'm trying to get some posts from a specific category into a multidimensional array like so:
wp_reset_query();
query_posts();
while (have_posts()) : the_post();
if (in_category('videos')) {
$postAttribute["permalink"] = get_permalink();
$postAttribute["image_url"] = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
$postAttribute["title"] = get_the_title();
$postAttribute["comments_number"] = get_comments_number();
array_push($videos, $postAttribute);
};
endwhile;
and then to check the array I run:
echo count($videos);
I keep getting 2 as a result, even though I know there are way more posts than that in the interview category.
I checked the max number of posts setting, set it higher just to see, but still got nothing.
Any idea what I could be missing?
It looks like you're relying on the main query, which by default gets you only 20 posts per page or so.
Do a custom query instead:
$my_query = new WP_Query(array(
'posts_per_page' => -1, // all
'category_name' => 'videos',
));
while($my_query->have_posts()){
$my_query->the_post();
// do your thing here
}
in_category() check is not required, because the query will get you only posts from the "videos" category
I have a category page called 'features' this is all fine, but the posts in the features category also belong to a certain genre of film, and it's this what I want to order the posts by on the actual features category template.
eg
features cat template brings in all features posts.
then what I want to do is display in alpha order by whatever genre it also belongs to
features
action
post
post
post
comedy
post
post
sci-fi
post
post
etc.
this is what I have at the moment ( the cat numbers relate to the genres = action=10 etc)
query_posts('cat=10,11,12,13,14,15,16,17,18&orderby=title&order=DESC' );
while (have_posts()) : the_post();
How can I list all the posts (group them) by genre ? when I use title here i guess it's using the posts title.
Playing around if I stick this in the post loop
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(4, $childcat)) {
echo $childcat->cat_name;
}
}
this returns the actual genre cat for each post while in the loop, but I'm not sure how to stick it together so I can state the ordering of the posts by groups of genre, I was hoping I could do this in the query_posts?
Any help in the right direction would be greatly appreciated.
I would consider first doing a query for your sub categories, and then loop through your sub categories querying the posts for each. Like so:
$categories = get_categories( array ( 'child_of' => 10, 'orderby' => 'name' ) );
foreach( $categories as $category ){
// query_posts for category by $category->term_id
// Display posts for this category
}
Does that work for what you are wanting to do?