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?
Related
I have created Custom Post Type "Music" and artists taxonomy related to it, I used the below code to list all artists but I what I want is, If any artist was clicked a list of his songs should be shown only.
<?php $terms = get_terms([
'taxonomy' => 'artists',
]);
if (!empty($terms)) {
foreach ($terms as $term) {
echo $term->name;
echo "<br><br>";
}
}
?>
Any one knows how can I display the songs for a specific artist on click?
In other words, how to get the id of the clicked element in order to iterate over the taxonomy and get the specified element?
As far as I am concerned, the elements found inside a taxonomy don't have an id?
Example: The user clicked on Roddy Rich, How to iterate over the taxonomy and get that element?
I've some custom taxonomies (doors qty) for vehicle (post type) and brands (another taxonomy).
When user click on Taxonomy: 2 or 4 doors, i need display on post type archive page all brands with 2 or 4 doors, for example: Honda, Toyota, Hyundai.
Unfortunately WordPress can't filter with this data to display the terms.
I try this:
<?php $args = array(
'taxonomy' => 'brand',
); ?>
<?php $terms = get_terms( $args ); ?>
<?php foreach ($terms as $term ) : ?>
<?php /* Ok, here display a name of brand from vehicles with 4 doors */ ?>
<?php echo $term->name; ?>
<?php endforeach; ?>
This code display all brands, but i need display only brands with 4 doors.
Watching documentation here: https://developer.wordpress.org/reference/functions/get_terms/ i see this:
'meta_query' => '',
'meta_key' => array(),
'meta_value'=> '',
Is there the correct way to filter brands with 4 doors?
Please consider that i use this only on archive page (archive.php) and i can use queried object to pass any data ;)
Okay
I resolve this, thinking terms wasn't correct way. The correct way is make a Custom Post Type called Brands and make it relational with another Custom Post Type or Term (in my case: Door Qty).
If you are interesting in Custom Post Types, please see this documentation: https://codex.wordpress.org/Post_Types
Excuse my english.
I have Two parent category on my website, and each of those category have children category, like this :
constructions corollaires
01
02
03
04
Between two suns
I
II
III
IV
I'm trying to display the parent category on a archive page, and also the children category.
here is my code for the parent category :
<?php
$category = get_the_category();
echo $category[0]->name;
?>
and for the children category :
<?php
$category = get_the_category();
echo $category[1]->name;
?>
what I don't understand, is that for posts who are in inside "Between two suns" category, first code echoes "Between two suns" which is the correct value, but posts who are inside "constructions corollaires", it echoes "I", the sub category instead of "constructions corollaires".
and it's the same problem with my second code, for posts who are in inside "Between two suns" category, second code echo "I" which is the correct value (1st children category), but posts who are inside "constructions corollaires", it echoes "constructions corollaires", the parent category instead of "01" for example (the children category.
I don't understand what I'm doing wrong...
all my categories are sorted in the back end.
can anybody help me with this ? It looks like an ordering issue but I really don't know how to fix this !
For only Top label category
$categories = get_terms(
'category',
array('parent' => 0)
);
And get children category just pass parent category id in parent item
$categories = get_terms(
'category',
array('parent' => $cat->ID)
);
I am wondering if there is a way to list posts from 2 categories in wordpress such that every fifth post is from different category. This is what I mean
Category B
Category A
Category A
Category A
Category A
Category B
and so on....
The sorting for each category will be based on date (descending)
Well its much complicated then its sounds, you wont find an exact method to do this, but you can work around it. For instance in your query you can get posts from 2 categories and then in the loop you can count them and if count is less than 5 display category A, else display category B. But the problem with this is that you dont know how many posts is it going to fetch from each category. It might fetch 2 posts from Category A and 3 From category B. So Instead its better if you use 2 different loops and save all the data in an array.
<?php
$args1 = array( 'posts_per_page' => -1, 'category' => 1 );
$args2 = array( 'posts_per_page' => -1, 'category' => 2 );
$posts_of_category_A = get_posts( $args1 );
$posts_of_category_B = get_posts( $args2 );
$check = 0;
foreach($posts_of_category_B as $posts_B){
// This will only show 1 post of category B;
// echo $posts_B->title (var_dump it)
for($i = $check; $i<$check+4; $i++ ){
// This will Show only first 4 posts of Categroy A
// Convert object to array, something $posts_B[$check]->title
}
$check += 3;
}
Its just a concept, there might be several changes to be made to this script. I know its kind of a hacky way to do this, but i cant think of a better way.
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; ?>