Posts tagged ‘Wordpress’

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.

Method to get data of a single wordpress post

function get_post_content($id) {
	global $wpdb;
	$query =
		" SELECT ID, post_title, post_excerpt, post_content, post_content_filtered "
		."FROM $wpdb->posts "
		."WHERE ID = '$id'";
	$pages = $wpdb->get_results($query);
	if( ! empty($pages) && isset($pages[0]) ) {
		//echo( 'Title: '.$pages[0]->post_title."\n" );
		//echo( 'ID: '.$pages[0]->ID."\n" );
		return $pages[0]->post_content;
	}
	return '';
}

note that this method does not distinguish between posts that have or have not been published, this allows the content of an unpublished post to be used eleswhere.

ExpLists


This Plugin Breaks With WordPress 2.3!


About

A wordpress plugin that uses Geir Landrö’s excellent jTree javascript to generate persistant (via cookies), collapsable tree widgets for categories, pages, post archives, and links.

as indicated by the zero in the major version, this is a beta release. most of the bugs are out, but there will most likely be more still hiding, please comment on any you may find.

examples can be seen on this site.

Documentation

from explists-README in the package:

the plugin must be enabled in the admin plugins tab
the widgets must be enabled in the admin presentation>widgets tab

this package should contain the following:

explists-X.X.X
 +-explists-README            this file
 +-\explists                  folder for web-accessable content for plugin
     +-(lots of images)       images for dtree
     +-dtree.css              stylesheet for dtree
     +-dtree.js               javascript for dtree
 +-\wp-content                existing folder
     +-\plugins               existing folder
         +-\widgets           existing folder
             +-\explists      folder for plugin code
             +-explists.php   file for plugin init and rendering code

this package was designed for unrar deployment,
but can be manually installed as follows:
  copy explists-X.X.X/explists
       to WP_ROOT/explists
  copy explists-X.X.X/explists/wp-content/plugins/widgets/explists
       to WP_ROOT/explists/wp-content/plugins/widgets/explists
  copy explists-X.X.X/explists/wp-content/plugins/widgets/explists.php
       to WP_ROOT/explists/wp-content/plugins/widgets/explists.php

  where:
    WP_ROOT is the root of your wordpress installation.
    explists-X.X.X is the root of the unrared (or unzipped) package.

Download

ExpLists-0.1.3

To Do List

  • fix title in Admin>Options>ExpLists (wrong version)
  • add message for incorrect resource installation

WordPress IE CSS (or other content for that matter) Fixes

insert the following in one of your templates:

<?php
if( function_exists(’insert_ie_fixes’) ) {
  insert_ie_fixes();
}
?>

I put it at the bottom of footer.php to make sure it overrides prior declarations, but it may work in header.php as well.

then add the following to functions.php in the theme you are using:

function insert_ie_fixes() {
?>
<!–[if IE]>
<style>
RULES SPECIFIC TO IE GO HERE
</style>
<![endif]–>
<?php
}

this could also be used to insert general styles or scripts.

  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Tags

  • Archives