2012-02-11 62 views
0

我有一個問題,通過AJAX加載一個特定類別的職位列表的問題。有趣的是,我獲得了適量的帖子,每篇帖子我都會得到摘錄,但是ID和標題都是空的,加上日期錯誤(1.1.1970)。這是我的函數的functions.php中:wordpress get_posts()返回空the_ID()

function ajax_cat(){ 

if(isset($_GET['action'])&& $_GET['action'] == 'ajax_cat'){ 
$my_id = htmlspecialchars($_GET["id"]); 
$args = array(
       'offset'   => 0, 
       'category'  => $my_id, 
       'orderby'   => 'post_date', 
       'order'   => 'DESC', 
       'post_type'  => 'post', 
       'post_status'  => 'publish'); 

      $myposts = get_posts($args); 
      foreach($myposts as $post) : setup_postdata($post); ?> 
       <div class="nadpis1" id="<?php the_ID(); ?>" > 
       <a href="#<?php the_ID(); ?>"><?php the_title(); ?></a> 
       <span class="sipka"><?php the_date("d.m. Y"); ?></span> 
       </div> 
       <div class="vnutro" ><?php echo the_excerpt();?></div> 
      <?php endforeach; 
    die(); 
    } 
} 

原來這代碼爲內環路和運作良好。這是我現在得到:

<div class="nadpis1" id="" > 
    <a href="#"></a> 
    <span class="sipka">01.01. 1970</span> 
</div> 
<div class="vnutro" ><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></div> 

<div class="nadpis1" id="" > 
    <a href="#"></a> 
    <span class="sipka"></span> 
</div> 
<div class="vnutro" ><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec adipiscing ultrices posuere. Aenean et egestas enim. Phasellus sit amet nisl elit. Sed pulvinar, purus nec commodo condimentum, lectus risus lacinia urna, sed ultrices magna est vitae turpis. Sed convallis pulvinar gravida. Sed non sem sem, at lobortis tellus. Etiam commodo risus vitae diam cursus volutpat. [...]</p></div> 

<div class="nadpis1" id="" > 
    <a href="#"></a> 
    <span class="sipka"></span> 
</div> 
<div class="vnutro" ><p>gfdgdfsgngfjty ty jghj ty jtyhjghj dh gfdj5 fgjfdthbkdfgxhjsrgv ,f xfhbtyj dc rtjdtxhvcntydxvhctr shxfc</p></div> 

<div class="nadpis1" id="" > 
    <a href="#"></a> 
    <span class="sipka">01.01. 1970</span> 
</div> 
<div class="vnutro" ><p>Vitajte vo WordPress. Toto je váš prvý článok. Môžete ho upraviť alebo vymazať a potom už len začať písať!</p></div 

>

感謝您的任何建議!

回答

0

您不能在The Loop之外使用the_函數。嘗試使用get_the_title($postid);和類似的get_函數。請注意,get_函數不顯示值,它們會返回值,因此您必須使用echo get_the_title($postid);

編輯:沒關係,我看到你使用的是setup_postdata()。但是,使用ID中傳遞的函數而不是更改全局設置的函數仍值得使用。嘗試使用print_r檢查$post,以查看您的foreach循環中已有的數據,而無需調用大量其他數據庫查詢。

+0

所述的print_r()輸出: stdClass的對象 ( [ID] => 11 [post_author] => 1 [POST_DATE] => 2012-02-07 17時29分03秒 [post_date_gmt] => 2012-02-07 17時29分03秒 [POST_CONTENT] => Lorem存有悲坐阿梅特... [POST_TITLE] => skuska 3 測試3 [post_excerpt] => [post_status] =>發佈 [comment_status] =>打開 [ping_status] =>打開 [post_password] => [POST_NAME] =>測試-3 [to_ping] => – janper 2012-02-11 01:53:21

+0

[ping操作] => [post_modified] => 2012-02-08 22時51分19秒 [post_modified_gmt] => 2012 -02-08 22:51:19 [post_content_filtered] => [post_parent] => 0 [guid] => http://new.rese-arch.org/?p=11 [menu_order] => 0 [post_type] =>發佈 [post_mime_type] => [COMMENT_COUNT] => 0 [過濾器] =>原料 ) – janper 2012-02-11 01:54:11

+0

好吧,當我正在做這樣工作的:'回聲​​$後> ID;',但是,我得到這樣的非常原始的數據:' skuska 3 測試3 '我沒有**摘錄**這次:/ – janper 2012-02-11 02:01:27