【问题标题】:wordpress get_posts() returns empty the_ID()wordpress get_posts() 返回空 the_ID()
【发布时间】:2012-02-11 01:37:03
【问题描述】:

我在通过 AJAX 加载某个类别的帖子列表时遇到问题。有趣的是,我收到了适量的帖子,每个帖子的摘录都是正确的,但是 ID 和标题是空的,加上日期是错误的(1970 年 1 月 1 日)。这是我在functions.php中的函数:

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

>

感谢您的任何建议!

【问题讨论】:

    标签: ajax wordpress


    【解决方案1】:

    您不能在 The Loop 之外使用 the_ 函数。尝试使用get_the_title($postid); 和类似的get_ 函数。请注意,get_ 函数不显示值,而是返回值,因此您必须使用 echo get_the_title($postid);

    编辑:没关系,我看到你使用的是setup_postdata()。但是,使用传入 ID 的函数而不是更改全局设置的函数仍然是值得的。尝试检查 $postprint_r 以查看您在 foreach 循环中已有哪些数据,而无需调用一大堆其他数据库查询。

    【讨论】:

    • print_r() 输出:stdClass Object ([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 ipsum dolor sit amet... [post_title] => skuska 3test 3 [post_excerpt] => [post_status] => 发布 [comment_status] => 打开 [ ping_status] => 打开 [post_password] => [post_name] => test-3 [to_ping] =>
    • [pinged] => [post_modified] => 2012-02-08 22:51:19 [post_modified_gmt] => 2012-02-08 22:51:19 [post_content_filtered] => [post_parent ] => 0 [guid] => new.rese-arch.org/?p=11 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw )
    • 好的,当我这样做时它可以工作:echo $post-&gt;ID;,但是,我得到了像这样的非常原始的数据:&lt;!--:sk--&gt;skuska 3&lt;!--:--&gt;&lt;!--:en--&gt;test 3&lt;!--:--&gt;,我没有得到摘录 这次:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2012-08-14
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多