Posts tagged ‘HTML’

Random Content in Wordpress

this example is for WordPress, but the concepts should work fine anywhere.

a quick note, though they are not in the code examples, it is important that the code go between <? and ?> bits. any syntax highlighting editor should make it plain where they are needed.

 

the simplest version of this would be to add the following to your template where you want the random content to show up:

//this line is only needed for PHP versions prior to 4.2
//srand((float) microtime() * 10000000);

$content = array(
	"just some text",
	"some more text",
	"yet more text",
);

$key = array_rand($content);
echo( "<p>" . $content[$key] . "</p>" );

 

to turn this into a function available throughout the theme, add the following to the end of the “functions.php” (it is in your theme folder).
if this file does not exist, you can create it.

//this line is only needed for PHP versions prior to 4.2
//srand((float) microtime() * 10000000);

$content = array(
	"just some text",
	"some more text",
	"yet more text",
);

function get_random_content() {
	global $content;
	if( is_array($content) ) {
		$key = array_rand($content);
		return $content[$key];
	}
	else {
		return "";
	}
}

function echo_random_content() {
	echo( "<p>" . get_random_content() . "</p>" );
}

this will allow you to add the following to any spot in your templates:

<? echo_random_content(); ?>

or better yet, wrap the call in a check:

<? if( function_exists("echo_random_content") ) { echo_random_content(); } ?>

 
 

if you want to extend this idea further, for example into different “databases” of content:

//this line is only needed for PHP versions prior to 4.2
//srand((float) microtime() * 10000000);

$content = array(
	"text" => array(
		"just some text",
		"some more text",
		"yet more text",
	),
	"other" => array(
		"just some other text",
		"some more other text",
		"yet more other text",
	),
);

function get_random_content($from) {
	//this is the default if no "$from" is set
	return get_random_content("text");
}

function get_random_content($from) {
	global $content;
	if( is_array($content) ) {
		if( isset($quote[$from]) ) {
			$key = array_rand($content[$from]);
			return $content[$key][$from];
		else {
			//this is the default if "$from" does not exist
			$key = array_rand($content["text"]);
			return $content[$key]["text"];
		}
	}
	else {
		return "";
	}
}

function echo_random_content() {
	echo( "<p>" . get_random_content() . "</p>" );
}

all that has been added is another dimention to the array, some error checking, and the ability to select the array to draw from.

this version will allow you to add the following to any spot in your templates:

<? echo_random_content(); ?>
<? echo_random_content("text"); ?>
<? echo_random_content("other"); ?>

or even better:

<? if( function_exists("echo_random_content") ) { echo_random_content(); } ?>
<? if( function_exists("echo_random_content") ) { echo_random_content("text"); } ?>
<? if( function_exists("echo_random_content") ) { echo_random_content("other"); } ?>

 

the code still does not check for the possibility of empty or erroneous content, but if you create a variable in echo_random_content() and use it to hold the results of get_random_content(), you can check if it is empty, contains bad characters, swap newlines out for break tags, or whatever else you would like.

JQuery and “Automagic” toggling div tags

inserting the following jquery code in the page:

$(document).ready(function(){
	$("a[name^='pack-']").click(function () {
		var _name = $(this).attr('name');
		//$("div[name='"+_name+"']").slideToggle("slow", function () {
		$("div[name='"+_name+"']").toggle("slow", function () {
			if( $("a[name^='"+_name+"']").text()=='[-]' ) {
				$("a[name^='"+_name+"']").text('[+]');
			}
			else {
				$("a[name^='"+_name+"']").text('[-]');
			}
		});
	});
});

the div will toggle it’s visibility as per the toggle() or slideToggle() methods, and alter the link text used to activate the behavior.

this allows the following simple HTML to be used to enable the jquery element toggling effect.

<span>Title<a name="pack-1">[-]</a></span>
<div name="pack-1">
content content content content content.
</div>

even better, disabling it is as simple as commenting out the chunk in the $(document).ready()), or by not including the javascript at all.

  • Tags

  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Archives