0

我在获取帖子的所有数据时遇到了一些问题。我已经尝试了几件事,但无法让它工作。我能做些什么?

这是我编写的用于获取帖子数据的函数:

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 个元素)。有人可以帮我吗?

4

2 回答 2

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(); ?>

谢谢。

于 2013-10-01T12:22:37.130 回答
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 );

而已!

于 2013-10-02T12:03:14.963 回答