Ajax wordpress get_posts()返回空的\u ID()
我在通过AJAX加载特定类别的帖子列表时遇到了一个问题。有趣的是,我得到了适量的帖子,每个帖子的摘录都是正确的,但是ID和标题都是空的,加上日期是错误的(1970年1月1日)。这是functions.php中的我的函数:Ajax wordpress get_posts()返回空的\u ID(),ajax,wordpress,Ajax,Wordpress,我在通过AJAX加载特定类别的帖子列表时遇到了一个问题。有趣的是,我得到了适量的帖子,每个帖子的摘录都是正确的,但是ID和标题都是空的,加上日期是错误的(1970年1月1日)。这是functions.php中的我的函数: function ajax_cat(){ if( isset($_GET['action'])&& $_GET['action'] == 'ajax_cat'){ $my_id = htmlspecialchars($_GET["id"]); $args
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();
}
}
函数ajax\u cat(){
如果(isset($\u GET['action'])&&&$\u GET['action']=='ajax\u cat'){
$my_id=htmlspecialchars($_GET[“id”]);
$args=数组(
“偏移量”=>0,
“类别”=>$my_id,
'orderby'=>'post_date',
“订单”=>“描述”,
“post_type”=>“post”,
“发布状态”=>“发布”);
$myposts=get_posts($args);
foreach($myposts作为$post):设置_postdata($post);?>
您不能在循环之外使用
函数。请尝试使用和类似的get
函数。请注意get
函数不显示值,而是返回值,因此您必须使用echo get\u title($postid);
编辑:没关系,我看到您正在使用setup\u postdata()
。但是,仍然值得使用传入ID的函数,而不是更改全局设置的函数。请尝试使用print\r
检查$post
,查看您的foreach
循环中已经有哪些数据,而无需调用一大堆其他数据库查询。print\r()输出:stdClass对象([ID]=>11[post\u author]=>1[post\u date]=>2012-02-07 17:29:03[post\u date\u gmt]=>2012-02-07 17:29:03[post\u content]=>Lorem ipsum dolor sit amet…[post\u title]=>skuska 3test 3[post\u摘录]=>post\u状态]=>publish[comment\u状态]=>open[ping\u状态]=>open[发布密码]=>[发布名称]=>测试-3[发送到发布]=>[点击]=>[发布修改]=>2012-02-08 22:51:19[发布修改的内容]=>2012-02-08 22:51:19[发布内容过滤]=>[发布父项]=>0[guid]=>[菜单顺序]=>0[发布类型]=>发布[发布mime类型]=>0[评论数量]=>0[过滤器]=>raw)好的,当我这样做时它会起作用:echo$post->ID;
,然而,我得到的是这样的原始数据:skuska 3test 3
,这次我没有得到摘录:/
<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