where is the mistake in PHP Warning: Invalid argument supplied for foreach() in line 3

**strong text******post-tag****function asr_tags() { $asrtags =get_the_tags( get_the_ID());foreach($asrtags as $t

**strong text**/****post-tag****/
function asr_tags() { 
$asrtags =  get_the_tags( get_the_ID());
foreach($asrtags as $tag){$string .= '<span class="post-tag"><a class="p-tag" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a></span>' . "\n";
} 
return $string;
} 
add_shortcode('asrtags' , 'asr_tags' );

/*****post-tag*****/

**strong text**/****post-tag****/
function asr_tags() { 
$asrtags =  get_the_tags( get_the_ID());
foreach($asrtags as $tag){$string .= '<span class="post-tag"><a class="p-tag" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a></span>' . "\n";
} 
return $string;
} 
add_shortcode('asrtags' , 'asr_tags' );

/*****post-tag*****/

Share Improve this question edited Aug 7, 2019 at 8:39 abdullahsk asked Aug 6, 2019 at 18:47 abdullahskabdullahsk 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

get_the_tags() returns an array or false on failure. (It appears it may also return a WP_Error object under certain circumstances.)

It also requires a parameter: the post ID. You're not passing that, so you're not going to get the array your code expects.

Edit

You can't just call get_the_tags() -- that'll just get you a value of false. You need to provide a post ID.

This might work: replace

$asrtags =  get_the_tags();

with

$asrtags =  get_the_tags( get_the_ID() );

...but note that this will only work if you're in The Loop.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744860923a4597696.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信