1

I have a child theme in which I created a page template. I am able to show different content based on time of day by writing some PHP inside the actual page e.g.

if ( $time < '10:00'){
echo "The time is 10am and good morning.";
}
else
{
//show the page loop. 
}

I would like to show the message if it matches the condition if not then the usual page loop should be executed.

Will it be possible to do that using a plugin instead of creating page templates for each of my sites? It will be a lot easier just to install the plugins.

1

It depends on where you want to put the 'hello' thing. For instance, you could use the_content filter to put it before or after the content:

function wp_add_to_the_content($content) {
$content = 'Text before the content.' . $content . 'Text after the content';
return $content

add_filter('the_content', 'wp_add_to_the_content',10,1);

You'd need to add any CSS and other tags to format thing.

There are also filters for other parts of the post- like the post title, etc.

Added The OP added to the question indicating he wants the message to be displayed before the loop is processed.

The answer to this question will provide guidance: https://stackoverflow.com/questions/23741354/adding-content-before-the-loop ; use the loop_start filter which 'fires' before the loop is started.

2
  • Thanks Rick. I'd like to skip the loop if the conditions are matched. The content is in the loop isn't it? – Abu Nooh Aug 28 '18 at 9:07
  • Yes, the_content is within the loop. It wasn't clear from your question exactly where you wanted to intercept/adjust things. There are many filters you can use to change things. You can use a filter that works before the loop, or one after. So, need more info on exactly where you want to display your message. – Rick Hellewell Aug 28 '18 at 19:14

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.