2 colums plugin
-
I’ve created a 2 column layout plugin for my needs. Maybe someone will find it usefull. The purpose was to create a newspaper like layout automatically.
It’s a very small and easy plugin so it won’t be hard to modify.
This is, for my purpose, to be complemented with a plugin (I’ll write next), that automatically divides a post into pages.
What it does: it takes the the_content output and splits it into two more or less (words counted) equal columns. There are 2 options:
1. Whether or not this layout should be only on the permalink of the article in question. For myself I don’t use the 2 column layout on my news homepage, only on the page that shows only the article.
2. You can choose whether the output is as a table, or as divs. There are id’s everywhere for easy styling (which you should do yourself, especially for divs this needs some work and css knowledge to make it happen, but ofcourse it’s worht it).
disclaimer: I’m rather a php newbie, so my coding may be sloppy, not work for you, or scare away your computer.
I also have no space to upload the file to so you’ll have to manually copy and paste the following code and put it into a .php file.
The code:
<?php
/*
Plugin Name: 2 columns
Plugin URI: localhost
Description: creates a 2 column layout, like in a newspaper
Version: 0.1
Author: dissurion
Author URI: localhost
*/
/*
Puts the output of the page in 2 seperate divs, contained in a general div.
The divs should be styled to achieve a 2 column layout (using css). The tables have all necessary id's too.
There's one option to set. See below.
*/
//--------code--------------------//
//the function//
function two_colums($content) {
//SET OPTION 1: should the content be split on all pages? Choose "no" for only when the post/article itself is viewed, anything else counts as "yes"
$all = "no";
//set option 2: use divs (new style webdesign, cool, webstandards etc) or tables (not cool, old fashioned etc): choose "divs" or, for tables, anything else
$style = "divsd";
$allwords = explode(" ", $content);
$numberofwords = count(explode(" ", $content));
for ($i=0; $i < $numberofwords/2; $i++) { $column1 = $column1 . $allwords[$i] . " "; }
for ($i=($numberofwords/2)+1; $i < $numberofwords; $i++) { $column2 = $column2 . $allwords[$i] . " "; }
if ($style=="divs") { $twocolumns = "<div id=\"2columns\"><div id=\"column1\">" . $column1 . "</div><div id=\"column2\">" . $column2 . "</div></div>"; }
else { $twocolumns = "<table id=\"2columns\"><tr colspan=\"2\" id=\"2columnstr\"><td id=\"2columnstd1\">". $column1 ."</td><td id=\"2columnstd2\">". $column2 ."</td></tr></table>"; }
if ($all=="no") { if(isset($_GET['p'])) { print $twocolumns; } else { print $content; } }
else { print $twocolumns; }
}
//the registering//
add_filter('the_content', 'two_colums');
?>
If someone is not ashamed of hosting this I would be thankfull.
The topic ‘2 colums plugin’ is closed to new replies.