知道了!
function suIsValidString( &$s, &$iLen = null, $minLen = null, $maxLen = null )
{
if( !is_string( $s ) || !isset( $s{0} ))
{ return false; }
if( $iLen !== null )
{ $iLen = strlen( $s ); }
return (( $minLen===null?true:($minLen > 0 && isset( $s{$minLen-1} ))) &&
$maxLen===null?true:($maxLen >= $minLen && !isset( $s{$maxLen})));
}
// -- ** -- Template functions
function wptGetCategoryIdByName( $sName )
{
$oTerm = get_term_by( 'name', $sName, 'category' );
return (is_object( $oTerm ) && isset( $oTerm->term_id ))?$oTerm->term_id:null;
}
function wptGetPostData( $sCategory, $sField = null, $iIndex = null )
{
global $WPT_POST_DATA;
$bCategory = ( suIsValidString( $sCategory ) );
$bIsInit = ( is_array( $WPT_POST_DATA ));
$uIndex = $bCategory?$sCategory:0;
$sFieldName = ( suIsValidString( $sField )?$sField:'content' );
if( !$bIsInit || !isset( $WPT_POST_DATA[$uIndex] ))
{
if( !$bIsInit )
{ $WPT_POST_DATA = array(); }
// wp_reset_query();
//$category_query = new WP_Query( array( 'cat' => wptGetCategoryIdByName($sCategory)) );
$bValid = true;
$iCategory = null;
if( $bCategory )
{
$iCategory = wptGetCategoryIdByName($sCategory);
$bValid = ( is_numeric( $iCategory ) && $iCategory >= 0 );
if( !$bValid )
{ $iCategory = null; }
}
$sQuery = 'numberposts=-1'.(($iCategory !== null)?('&cat='.$iCategory):null);
$WPT_POST_DATA[$uIndex] = $bValid?get_posts( $sQuery ):array();
/*
if( $bCategory )
{
var_dump( $uIndex );
var_dump( $WPT_POST_DATA[$uIndex] ); }
*/
if( !is_array( $WPT_POST_DATA[$uIndex] ))
{ $WPT_POST_DATA[$uIndex] = array(); }
}
if( $sFieldName === 'count' )
{
//var_dump( $WPT_POST_DATA[$uIndex] ); die;
return count( $WPT_POST_DATA[$uIndex] );
}
$iAutoIndex = 0;
if( !isset( $WPT_POST_DATA[$uIndex]['autocounter'] ))
{ $WPT_POST_DATA[$uIndex]['autocounter'] = $iAutoIndex; }
else { $iAutoIndex=(++$WPT_POST_DATA[$uIndex]['autocounter']); }
if( !is_numeric( $iIndex ))
{ $iIndex = $iAutoIndex; }
$uResult = null;
// var_dump( $WPT_POST_DATA[$uIndex][$iIndex]->post_content );
if( isset( $WPT_POST_DATA[$uIndex][$iIndex]) && is_object( $WPT_POST_DATA[$uIndex][$iIndex] ))
{
$sPostPrefixName = 'post_'.$sFieldName;
if( isset( $WPT_POST_DATA[$uIndex][$iIndex]->$sPostPrefixName ))
{ $uResult = &$WPT_POST_DATA[$uIndex][$iIndex]->$sPostPrefixName; }
elseif( isset( $WPT_POST_DATA[$uIndex][$iIndex]->$sFieldName ))
{ $uResult = &$WPT_POST_DATA[$uIndex][$iIndex]->$sFieldName; }
}
if( suIsValidString( $uResult ))
{
$uResult = utf8_decode( $uResult );
suSpecialCharsToHtml( $uResult );
}
//var_dump( $uResult );
return $uResult;
}
用法:
$sMyString = wptGetPostData( 'mycat', 'content' );
var_dump( $sMyString );
而已!