从以下代码开始,我只需要将显示的结果限制为5个作者姓名,但要保持相同的属性。
$value = get_post_meta($post->ID, 'authors_names', true);
$value_array = explode(',', $value);
$num_values = count($value_array);
foreach ($value_array as $key => $array) {
$remove_space = str_replace(' ', '-', $array);
$url = esc_url('http://mywebsite.com/authors/' . $remove_space);
if ('' !== $url) {
$display = esc_html($array);
print"<a href='$url'>$display</a>";
}
if ($key < $num_values - 1) {
echo ',';
};
}
我正在尝试这样,但是似乎无法正常工作...我在哪里出错?
$mpirate = get_post_meta($post->ID, 'authors_names', true);
$mpirate_array = explode(',', $mpirate);
$url = esc_url( 'http://mywebsite.com/authors/' . $mpirate_array[$i]);
$display = esc_html( $mpirate_array[$i]);
for($i = 0; $i < 5; $i++) {
echo $mpirate_array[$i];
if($i < 4) {
echo ',';}};
print "<a href='$url'>$display</a>";
尝试这个:
$value = get_post_meta($post->ID, 'authors_names', true);
$value_array = explode(',', $value);
$hrefs = [];
for($i = 0; $i < 5; $i++) {
$remove_space = str_replace(' ', '-', $value_array[$i]);
$url = esc_url('http://mywebsite.com/authors/' . $remove_space);
if ('' !== $url) {
$display = esc_html($value_array[$i]);
$hrefs[] = "<a href='$url'>$display</a>";
}
}
echo implode(",", $hrefs);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句