Does the title ring a bell? I bet it does. It must remind you of the article i wrote quite some time ago titled “Get rid of the sociable plugins. Do it yourself!“. It helped a lot of people save some CPU time and memory usage. Now it’s time for the newsflash! The idea came to me after i read this article on a modification for the theme. It actually gives a very nice little admin area on the theme options but it fiddles with the database so i decided to show you how you can do the same thing without any query run.

Firstly, i will summarize all the steps we are going to take in order to achieve this:

  1. We will have a plain text file called “newsflash.txt” in our theme folder. This file will actually contain the newsflash we want to display to the visitor. If it’s empty, or not there at all then nothing will be shown.
  2. On the “index.php” file of our theme we will be adding a few extra lines of code that will actually do just that. Check for the existence of the file and if it’s there and has things in it, then a newsflash div will be rendered before the posts do.
  3. Skin the newsflash div box through the style.css file.

The result will look something like the following.

newsflash

Time to get our hands dirty. First of all create a new file called “newsflash.txt” on the home folder of your currently used theme. Then, open the file index.php (or home.php in some themes) and find the place where the loop begins. That usually is just before a line looking like this:

<?php while (have_posts()) : the_post(); ?>

Right before that line inject the following snippet of code:

<?php if (file_exists(TEMPLATEPATH.'/newsflash.txt')):?>
	<?php $newsflash_contents = file_get_contents(TEMPLATEPATH.'/newsflash.txt');?>
	<?php if(!empty($newsflash_contents)): ?>
		<div class="newsflash">
			<?php echo $newsflash_contents; ?>
		</div>
	<?php endif; ?>
<?php endif; ?>

Let’s take the code line by line here so you can understand what’s going on.

  • On the first line the check for the existence of the file is made. If it’s not there, then the whole code segment is not executed, thus nothing is displayed.
  • Second line loads the contents of the file into the variable “newsflash_contents“.
  • Third line checks if the variable is empty. If it is, then it means that the file had nothing in so no newsflash to display.
  • Four through six display the newsflash.

Now let’s move onto the CSS part. Open the style.css file (or any file that is your theme’s stylesheet) and add the following lines at the bottom:

div.newsflash {
	margin: 10px;
	border: 2px dashed black;
	background: #F3F0A9;
	padding: 10px;
}

This will render a dashed yellow box with the newsflash. A few notes here:

  • The newsflash text can contain HTML since it’s plainly rendered out. So, essentially, you can put just about anything in there.
  • Using your imagination and design skills you can do almost anything with it. You can add an exclamation mark image if you want to drive attention or color / skin it accordingly to fit your theme.

I would like to say that it’s much more preferred to load the newsflash from file rather make a query to the database. Ofcourse it also depends on your needs. If you need complicated management of newsflashes then this is probably not a method for you. On the other hand, if you have basic newsflash needs this is the way i’d recommend. And, as always, if you have trouble implementing, let me know and i’ll do my best to troubleshoot and help you out!