Forum
duplicating results
I have a small section on the home page which reveals the first 3 posts in each category : https://gochattervideos.com however to do I original used this code :
<?php
// defaults for all modes
$posts_per_page = 3;
$template = 'home_post_in_list.html';
$sort_order = 'DESC';
$sort_by = 'postDateTime';
$seasonal = 'seasonal';
$news = 'news-events';
$topic = 'topics';
$real = 'real-lives';
$christmas = 'christmas';
// Have we displayed any posts yet?
$posts_displayed = false;
if ($posts_displayed == false) {
// No other options have been used; no posts have been displayed yet.
// So display all posts.
perch_blog_custom(array(
'template' => $template,
'count' => $posts_per_page,
'sort' => $sort_by,
'sort-order' => $sort_order,
'category' => $seasonal,
));
}
perch_blog_custom(array(
'template' => $template,
'count' => $posts_per_page,
'sort' => $sort_by,
'sort-order' => $sort_order,
'category' => $news,
));
}
perch_blog_custom(array(
'template' => $template,
'count' => $posts_per_page,
'sort' => $sort_by,
'sort-order' => $sort_order,
'category' => $topic,
));
}
perch_blog_custom(array(
'template' => $template,
'count' => $posts_per_page,
'sort' => $sort_by,
'sort-order' => $sort_order,
'category' => $real,
));
}
perch_blog_custom(array(
'template' => $template,
'count' => $posts_per_page,
'sort' => $sort_by,
'sort-order' => $sort_order,
'category' => $christmas,
));
}
?>
The problem with this is that if a post has more than one category then it is repeated on the page and displays incorrectly. How do I call 3 post from each category but without duplicating the results. for the time being I have put this fixed in place
<?php
// defaults for all modes
$posts_per_page = 15;
$template = 'home_post_in_list.html';
$sort_order = 'DESC';
$sort_by = 'postDateTime';
$seasonal = 'seasonal';
$news = 'news-events';
$topic = 'topics';
$real = 'real-lives';
$christmas = 'christmas';
// Have we displayed any posts yet?
$posts_displayed = false;
if ($posts_displayed == false) {
// No other options have been used; no posts have been displayed yet.
// So display all posts.
perch_blog_custom(array(
'template' => $template,
'count' => $posts_per_page,
'sort' => $sort_by,
'sort-order' => $sort_order,
));
}
?>
I have called the amount I need a hoped that it pulls three from each category and used an overflow:hidden on the parent div to hide more than 4 results but this is a terrible fix.
You can pass in an array of categories, see the docs here: https://docs.grabaperch.com/functions/blog/perch-blog-custom/
please could you show me ?
All of the *_custom functions work in the same way as
perch_content_custom
. There are examples in the documentation: https://docs.grabaperch.com/functions/content/perch-content-custom/We've written about the small amount of PHP you need ot know to work with custom functions here: https://docs.grabaperch.com/perch/building/servers/how-do-i-get-started-with-php/
I do try reading all this but Im unsure of how to fix this issue, please could you show me how to fix it?
Are you trying to get the first three posts from a selection of categories, or the first post from each of a selection of categories?
I am trying to get three post from each category to echo but don't want any post to repeat, so I have 5 different categories therefore I need 15 unique posts but three post for each category. if you view it on the web page you should understand https://gochattervideos.com
Any ideas on a fix ? I can't get it working.
I'm not sure there's a "fix" as such - you're probably asking for more than the built-in filtering can provide. You're probably going to have to get your hands dirty with some PHP!
There's a couple of ways to get the IDs and remove from the array, but they involve some PHP work.
I haven't tried this myself, but I think the theory is sound...
Actually, my previous answer is a bit rubbish. You can probably do it with one query utilising "each" and a closure to update a counter for each category. It adds a bit more complexity but might be a neater solution than multiple queries. Either way, coding is needed.