You can definitely do this now. However, where it gets tricky is how you handle “pagination”. If the notification is still showing 1 at a time (e.g. 'posts_per_page' => 1), then the pagination could conflict with the main loop’s pagination (e.g. if it was on a blog index). Alternately, you could sort of fake the pagination and pre-load x number of posts and then have some other way to navigate through them (maybe there’s some javascript scroll thing, or maybe there’s a button that you can click to see the next notification).
Instead of using the template tag in your code, what you’d need to do is run through a custom loop of notification posts (however many you would like to display), and then deal with the “paginiation” (going from one notification to the next) however you want to handle that.
Your loop might be something like this:
$notifications = new WP_Query( array(
'post_type' => 'notf_notifications',
'posts_per_page' => 5, // assuming you have some way of navigating to the next post, change this to however many posts you want to display
'post_status' => 'publish'
) );
if ( $notifications->have_posts() ) : while ( $notifications->have_posts() ) : $notifications->the_post(); ?>
<div class="notification your-style"><?php echo notf_message(); ?></div>
<?php endwhile; endif; ?>
Thread Starter
padsle
(@padsle)
Thanks for the suggestion. I’m new to coding – should I be in the notifications/notifications.php and should I replace the section below with the code you’ve noted above, or should I add the new code, keeping the below?
Thanks,
/**
* Notification message
* @since 1.0
* @uses get_posts
* @author Chris Reynolds
* returns the most recent notification
*/
function notf_message() {
$args = array(
'post_type' => 'notf_notifications',
'posts_per_page' => 1,
);
$notifications = get_posts( $args );
foreach ( $notifications as $notification ) {
$notf_id = $notification->ID;
$message = get_the_title( $notf_id );
}
if ( $notifications ) {
return $message;
}
}
No, you absolutely never modify the code in the plugin. If you do, and I release an update, and you update to the latest version, your custom code is gone and can’t be recovered.
Besides which, presentational code should not be in plugins anyway, that is the territory of themes. So in your custom theme or child theme, you might create a function that uses the code I posted above or, if you really needed to, you could put that code in your header.php or wherever the notification was going to display (putting it in a function is better practice, though, as template files should mostly be for the markup that displays the content on the page with very little actual code in them, other than template tags).
Keep in mind, though, that the code I posted is untested and just a suggestion for displaying multiple notifications on a single page (and I noticed an error in my code snippet — you wouldn’t use notf_message because that would just return a single notification — you would want to use the_title()). My suggestion doesn’t take care of the scrolling functionality — that would need to be added separately.