Add Snippet To Project
Sometimes, all we need is a little bit of encouragement to brighten our day. Whether it’s a challenging work project or a personal setback, positive words can make a big difference. If you have a WordPress website and want to spread some positivity to your visitors, why not display random messages of encouragement? In this article, we’ll show you how to set up a system that randomly selects and displays uplifting messages to inspire and motivate your audience. Let’s bring some positivity to your website!
function wpturbo_display_random_encouragement_message() {
$args = array(
'post_type' => 'encouragement',
'posts_per_page' => 1,
'orderby' => 'rand'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<div class="wpturbo-encouragement-message">
<?php the_content(); ?>
</div>
<?php
}
} else {
echo '<p>No encouragement messages found.</p>';
}
wp_reset_postdata();
}
add_action( 'wp', 'wpturbo_display_random_encouragement_message' );
The code snippet provided is used to display random messages of encouragement posts on a WordPress website. Let’s break down how it works step by step.
First, we define a function called wpturbo_display_random_encouragement_message()
. This function will handle the task of fetching and displaying the random encouragement message.
Inside the function, we have an array called $args
, which holds the arguments for the WP_Query that will fetch the encouragement posts. In this case, we set the post type to "encouragement", and the number of posts per page to 1. We also set the orderby parameter to "rand", meaning that the posts will be ordered randomly.
Next, we create a new instance of the WP_Query class, passing in the $args
array. This query will fetch the random encouragement post.
We then have an if statement that checks if the query has any posts. If it does, we enter a while loop that iterates over each post. Inside the loop, we call the the_post()
function, which sets up the current post for use within the loop.
Inside the loop, we output the content of the post using the the_content()
function. This will display the actual text of the encouragement message within a <div>
element with a class of "wpturbo-encouragement-message".
If the query does not have any posts, we output a message saying "No encouragement messages found".
After displaying the encouragement message or the "not found" message, we call the wp_reset_postdata()
function. This function is used to reset the WP_Query object and restore the global post data to the original post.
Finally, we hook the wpturbo_display_random_encouragement_message()
function to the wp
action. This ensures that the function will be executed when the WordPress page is rendered.
With these steps in place, whenever a page is loaded on the WordPress website, the wpturbo_display_random_encouragement_message()
function will be invoked, fetching a random encouragement message and displaying it on the webpage.