Eureka! (I got it!)
The following function gets a random page AND trims the content to a specific number of words passed as a parameter AND not counting HTML tags, just pure ‘the_content’ words, there are some really minor bugs that somebody could help to fix (try it to find out)
This function is very useful when you don’t want to mess with categories or tags to get content from static pages to fill out columns in a dynamic front page; the output includes the page title plus the permalink and a customizable ‘Keep reading →’ link at the bottom
/**
* Function: get_random_page_trim()
* Author: Jedi Master S-Ha-Dum & the Padawan Jorge Jaral.
* Task: Outputs a random page along with its title.
* Description: $child_of is the page ID (number) from which you want to select the
* random page. If $child_of is not set then it'll pick a random page from
* all your pages.
* Usage: To make it work you should insert in your template the instruction:
* <?php get_random_page_trim(172, 200); ?>
* where 172 is the parent page ID from which you want to get its child pages
* randomly to show inside a specific space, i.e. as a column on a static page;
* and 200 is the number of words that you want to obtain form the page post.
*/
function get_random_page_trim($child_of = 0, $all_words = 45)
{
$allowed_tags = array( 'a', 'abbr', 'b', 'blockquote', 'b', 'cite', 'code', 'div', 'em', 'fon', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'label', 'i', 'p', 'pre', 'span', 'strong', 'title', 'ul', 'ol', 'li', 'object', 'embed', 'param' );
$pages = &get_pages("child_of=".$child_of);
$pageInd = rand(0, count($pages) - 1);
$ga_title = apply_filters('the_title',$pages[$pageInd]->post_title);
$ga_permalink = get_permalink($pages[$pageInd]);
echo '<a href="';
echo $ga_permalink;
echo '">';
echo $ga_title;
echo '</a>';
$ga_content = apply_filters('the_content',$pages[$pageInd]->post_content);
if($ga_content != '' && $all_words > 0)
{
// process allowed tags
$allowed_tags = '<' . implode( '><', $allowed_tags ) . '>';
$ga_content = str_replace( ' ]]>', ' ]]>', $ga_content );
$ga_content = strip_tags( $ga_content, $allowed_tags );
// exclude HTML from counting words
if( $all_words > count( preg_split( '/[\s]+/', strip_tags( $ga_content ), -1 ) ) ) return $ga_content;
// count all
$all_chunks = preg_split( '/([\s]+)/', $ga_content, -1, PREG_SPLIT_DELIM_CAPTURE );
$ga_content = '';
$count_words = 0;
$enclosed_by_tag = false;
foreach( $all_chunks as $chunk )
{
// is tag opened?
if( 0 < preg_match( '/<[^>]*$/s', $chunk ) ) $enclosed_by_tag = true;
elseif( 0 < preg_match( '/>[^<]*$/s', $chunk ) ) $enclosed_by_tag = false;
// get entire word
if( !$enclosed_by_tag && '' != trim( $chunk ) && substr( $chunk, -1, 1 ) != '>' ) $count_words ++;
$ga_content .= $chunk;
if( $count_words >= $all_words && !$enclosed_by_tag ) break;
}
// native WordPress check for unclosed tags
$ga_content = force_balance_tags( $ga_content );
}
echo $ga_content;
echo '<a href="';
echo $ga_permalink;
echo '">Sigue leyendo →</a>'; //This means 'Keep reading' in spanish :)
}
Thanks to Jedi Master S-Ha-Dum (see s_ha_dum above) and SofaRider for the code, as of me, I’m just an assembler.
JJ