我在获取帖子的所有数据时遇到了一些问题。我已经尝试了几件事,但无法让它发挥作用。我该怎么办?
这是我写的一个函数来获取帖子数据:
function wptGetPostData( $sGategory, $sField = null, $iIndex = null )
{
global $WPT_POST_DATA, $wp_query;
if( !is_array( $WPT_POST_DATA ))
{
var_dump( $sGategory );
$WPT_POST_DATA = array();
$oOpt = array(
'category_name' => $sGategory, // get posts by category name
'posts_per_page' => -1 // all posts
);
query_posts( $oOpt );
// var_dump( have_posts() );
while(have_posts())
{
var_dump( 'loop' );
$WPT_POST_DATA[] = $wp_query->get_post_format(); //(array('echo'=>false));
//echo the_title();
//echo the_content();
}
//var_dump( $wp_query->posts ); die;
wp_reset_query();
}
var_dump( $WPT_POST_DATA );
// other code here ...
return null;
}
$WPT_POST_DATA
是一个缓存,必须包含所有帖子(最多5个元素)。有人可以帮我吗?
答案 0 :(得分:1)
试试这个:
// 1) Through Title:
// The function is case insensitive, so we can use lower case
$my_post = get_page_by_title( 'hello world', OBJECT, 'post' );
<h1><?php echo get_the_title( $my_post->ID ); ?></h1>
echo $my_post->post_content;
// OR to Avoid The Pitfall
echo apply_filters( 'the_content', $my_post->post_content );
// 2) Through Category ID:
<?php query_posts('cat=3&posts_per_page=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?wp_reset_query(); ?>
感谢。
答案 1 :(得分:0)
知道了!
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 );
就是这样!