1

I'm currently redesigning a local news website. The old design used a whole lot of categories to handle the position of the content in the static homepage as in "top news", "middle box", "left box" and so on.

This is working in someway but it's very ugly, in my opinion, very wrong on a semantic level: categories should be used to categorize.

Another option could be using sticky posts, but you can have only a "sticky" vs "non sticky" situation which is kind of limited.

I wouldn't use custom post types: basically all post are news and it would be overkill have a separate CPT to position my content on the homepage.

The perfect solution would be:

  • being able to choose between n position (I need 4)
  • having a default status that puts the post on the "other news" box
  • control the number of post going into a specific box: the main news box contains only one news, if there is a new "main news" the old one shouldn't disappear from the homepage but switch to a lower position.

To recap, I've 5 boxes, 4 of them are a "higher level" single news container, the last one holds 10 news in a reverse chronological order. These 10 news should be "normal" news and "higher level" news pushed out from the upper boxes.

I was thinking about Advanced Custom Field as a solution to let the editor choose the position but I'm not sure it's doable on the front end.

Thanks!

0

I would propose adding Custom Fields to the posts (either be convention or by creating a dedicated meta box) and then create the logic in front-page.php how to display them.

  1. Add Custom Fields to posts, e.g. "main_news"
  2. Have multiple loops on front-page.php just filtering those posts that fit your criteria to be put in a box.

EDIT: To skip some posts in a query use the offset parameter:

$query = new WP_Query( 'offset=1' ) );

More info in the Pagination Section of the Codex on WP_Query

  • Thanks! Could you make an example of the logic you would use in the loops? I'm concerned on the how to skip the first ones with -main_news- custom field activated in the "normal loop". – molokom Feb 19 '14 at 10:47
  • thanks for the update. I'm sorry but maybe I wasn't that clear: I know how to offset a loop, what I don't know is how to have a loop with normal posts and "offsetted" posts with specific custom field. – molokom Feb 19 '14 at 10:59
  • Right...okay this would need some further SQL-magic I guess. Or just multiple queries getting merged on PHP-level even though this feels dirty. Need to think about it... – kraftner Feb 19 '14 at 11:04
0

After a lot of digging and trial-error I think I found a solution.

It involves custom fields for which I used Advanced Custom Field but it's optional.

To make things easy on the site author I put a metabox before the actual news

enter image description here

Now, in my homepage I have 5 areas: 4 of them contains just one news and the 5th is a "other news" box with ten new.

So the first 4 boxes are pretty easy:

$recentPosts = new WP_Query();
$recentPosts->query(array( 
    'meta_key' => 'position',
    'meta_value' => 'Top1',
    'showposts' => 1
    )
);

with the meta value with the correct position I need.

The main issue was: how can I push older "Top1" news into the "other news" box without having to manually switch the custom field?

$top1 = get_posts(array(
 'meta_key' => 'position',
 'meta_value' => 'Top1',
 'offset' => 1
));
$top2 = get_posts(array(
 'meta_key' => 'position',
 'meta_value' => 'Top2',
 'offset' => 1
));
$top3 = get_posts(array(
 'meta_key' => 'position',
 'meta_value' => 'Top3',
 'offset' => 1
));
$top4 = get_posts(array(
 'meta_key' => 'position',
 'meta_value' => 'Top4',
 'offset' => 1
));
$other = get_posts(array(
 'meta_key' => 'position',
 'meta_value' => 'Other news'
));
$mergedposts = array_merge( $top1, $top2, $top3, $top4, $other ); 
$postids = array();
foreach( $mergedposts as $item ) { $postids[]=$item->ID; }
$uniqueposts = array_unique($postids);

$posts = get_posts(array(
 'post__in' => $uniqueposts,  
 'showposts' => 3
));
foreach( $posts as $post ) :
setup_postdata($post);

Maybe the code could be cleaned up a bit, but it works as I needed. I hope that's useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.