我有这个功能:
function movie_cast( $atts, $content = null ) {
$url = '/?s=';
$str = '';
foreach (explode(", ",$content) as $content)
{
if ($str != "") $str .= ", ";
$str .= '<a href=\'' . $url . $content . '\'>' . $content . '</a>';
}
return '<div class="movie_cast" style="display: block; margin-left: 180px;"><strong>Cast: </strong>'.$str.'<br><br></div>';
}
add_shortcode( 'movie_cast', 'movie_cast' );
这是打破帖子的短代码(帖子是空的):
[movie_cast] James Marsden,Joel Rogers,Will Ferrell,Christina Applegate,Paul Rudd,Steve Carell,David Koechner,Fred Willard,Bill Kurtis,Luke Wilson,Vince Vaughn,Dylan Baker,Liam Neeson,Harrison Ford,Steve Coulter ,Sacha Baron Cohen,Greg Kinnear,Jim Carrey,John C. Reilly,Kristen Wiig,L。Warren Young,Arvell Poe,Kirsten Dunst,Shade Rupe,Amy Poehler,Meagan Good,Nicole Kidman,Josh Lawson,Brian F. Durkin, Wilbur Fitzgerald,Tina Fey,Ron Prather,Kanye West,Karen Beyer,Sarah E. Spencer,June Diane Raphael,Lori Beth Edgeman,Zoe Myers,Matthew Stanton,Kate Nowlin,Gary Weeks,Chris Gethard,Charles Casey,Dane Davenport,Lauren Henneberg,Victor Gage,Nickolas Wolf,Clark Sarullo,Dave Pileggi,Ken Venzke,David Silverman,James Smith,Randall Taylor,Tom Clark,Judah Nelson,CC Ice,King,William Frasca,David Pascua,Amanda Q Williams,Gina Santiago,Jeff Matthew Glover,Gary Miller,John J. Palomino,Christopher Payne,Nancy Sandlin,Don Teems,Rachel Acuna,Paul Chappell,Fred Galle,Dawn Hamil, Gelin DiGennaro,Jennifer Gullick,Rose Hughes,Natalie Taylor,Dave Simmons,Rob Tunstall,Gary Hardt,Clay Stapleford,Robert F. Nixon,David Itchkawitz,Jessica Brown,Courtney Patterson,Alberto David Mercado,Joseph Nelson,Olivia Austin,Kevin Tomkins ,Colin Lacativa,Matt Amick,Lane Gamble,Zeb Austin,Larry A. Brewer,Ellie Decker,Lisa Finlayson,Daniel T. Martin,Courtney Pennington,Brandi Quinn,Paul Vincent Rapisarda [/ movie_cast]
如果我在短代码中编写较少的演员,那么它的工作效果要比。 这个限制来自哪里? 我怎么解决这个问题?
更新: 正如评论中所建议的那样,我试图回应$ str,似乎有效。 此功能适用于无限字符除以逗号:
function movie_cast( $atts, $content = null ) {
$url = '/?s=';
$str = '';
foreach (explode(", ",$content) as $content)
{
if ($str != "") $str .= ", ";
$str .= '<a href=\'' . $url . $content . '\'>' . $content . '</a>';
}
echo $str;
}
add_shortcode( 'movie_cast', 'movie_cast' );
答案 0 :(得分:1)
正如评论中所讨论的,当你返回结果时,WordPress似乎是一个问题 - 91很好,92不是。我的建议是重新编写循环,以便它最多只执行91次迭代:
function movie_cast( $atts, $content = null ) {
$url = '/?s=';
$str = '';
$output = array();
$count = 0;
foreach (explode(", ",$content) as $content) {
$count++;
$output[] = "<a href='" . $url . $content . "'>" . $content . "</a>";
if ($count == 91) {
break;
}
}
return implode(", ", $output);
}
我已经修改了一些代码 - 它将所有内容存储在一个数组中,因此您不需要每次都检查并单独添加逗号;相反,它会在最后生成数组以生成字符串。
我还交换了字符串,因此用双引号分隔,这样就不需要转义了。