TheJoe.it Into the (open) source

1Jul/130

WordPress: alert the reader to an article dated

clocky

Today I illustrate a small process, which I highly recommend to those bloggers linked to the development of a certain topic, to automatically warn the reader that the article you are reading is more old man a time of our choosing, and possibly belonging to a certain category. I am thinking especially to my blog, in which information technology can go well today, but it may already be obsolete with the next release of a software, no longer supported by the operating system or ingetrata.

The result will be completely customizable: appear a block (DIV) in the top of the article, with a class of our choice. We will then give us a style to the DIV.

You first need to change the theme that we are using the WordPress administration panel, walking su "Appearance" - "Editor". We select from the menu on the right side file "single.php".

wp_editor

Inside the file "single.php" will find all the various calls to PHP functions corresponding to the different sections of the template, this order is different for all subjects then there is no precise rule. Have the foresight to place the code after the closing of the header of the blog, immediately before the beginning part, or under the title. Always better in top of the article so that readers can see immediately that what he is about to read is an article already some time.

The PHP code to insert is this:

<?php
    // date('U')        = Current date in seconds since 1 January 1970 (Unix time)
    // get_the_time('U')    = Date of publication of the article in seconds 1 January 1970 (Unix time)
    // 31557600     = Seconds in a year (365 days and 6 hours)
    if ((date('U') - get_the_time('U')) >= 31557600) { ?>
        <div class="notice">
        Caution! This is an article of at least one year ago!<br />
        The instructions may not be the most appropriate!
        </div>
<?php } ?>

There is very little to explain. The fourth line calculates backwards 315.57.600 seconds from now (Exactly one year), if they are more shows the DIV corresponding to successive lines.

To specify instead Category want to assign the DIV will need to change the fourth line in this way:

<?php
    // date('U')        = Current date in seconds since 1 January 1970 (Unix time)
    // get_the_time('U')    = Date of publication of the article in seconds 1 January 1970 (Unix time)
    // 31557600     = Seconds in a year ((date('U') - get_the_time('U')) >= 31557600 && in_category('4')) { ?>
        <div class="notice">
        Caution! This is a technical article of at least a year ago!<br />
        The instructions may not be the most appropriate!
        </div>
<?php } ?>

So we will not apply the DIV of obsolescence Articles "eternal", that we will never need revision.

About

I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.