我是using this code here 用于重复CPT中的字段(是的,我知道ACF和其他一些),并尝试添加第三个字段。
该字段当前显示在元框中,并在前端输出。然而,在元框字段中(当它为空时),是一个
“未定义索引的通知:第235行上的角色…”(我将指出下面的哪一行),
前端写着
“注意:未定义索引:第29行的角色…”
在我的单个discog项目上。php文件。
我可以在后端更新和保存该字段,并在前端显示它,但我无法让这些通知消失,即使在网络上嗅探一下。
有没有关于如何解决这个问题的建议
add_action(\'admin_init\', \'add_meta_boxes\', 1);
function add_meta_boxes() {
add_meta_box( \'repeatable-fields\',
\'Personnel Listing:\',
\'awc_discog_meta_two\',
\'discog-item\',
\'normal\',
\'high\');
}
function awc_discog_meta_two() {
global $post;
$repeatable_names = get_post_meta($post->ID, \'repeatable_names\', true);
wp_nonce_field( \'repeatable_meta_box_nonce\', \'repeatable_meta_box_nonce\' );
?>
<script type="text/javascript">
//THE JS WORKS FINE
</script>
<table id="repeatable-fieldset-one" width="100%">
<thead>
<tr>
<th width="2%"></th>
<th width="30%">Name</th>
<th width="20%">Role</th>
<th width="40%">URL</th>
<th width="2%"></th>
</tr>
</thead>
<tbody>
<?php
if ( $repeatable_names ) :
foreach ( $repeatable_names as $field ) {
?>
<tr>
<td><a class="button remove-row" href="#" title="Remove This Item">-</a></td>
<td><input type="text" class="widefat" name="name[]" value="<?php if($field[\'name\'] != \'\') echo esc_attr( $field[\'name\'] ); ?>" placeholder="Charles Mingus"/></td>
//THE NEXT LINE IS LINE 235
<td><input type="text" class="widefat" name="role[]" value="<?php if($field[\'role\'] != \'\') echo esc_attr( $field[\'role\'] ); ?>" placeholder="guitar/oud"/></td>
<td><input type="text" class="widefat" name="url[]" value="<?php if ($field[\'url\'] != \'\') echo esc_attr( $field[\'url\'] ); ?>" placeholder="http://"/></td>
<td><a class="sort">Sort</a></td>
</tr>
<?php
}
else :
// show a blank one
?>
<tr>
<td><a class="button remove-row" href="#" title="Remove This Item">-</a></td>
<td><input type="text" class="widefat" name="name[]" placeholder="Charles Mingus"/></td>
<td><input type="text" class="widefat" name="role[]" placeholder="guitar/oud" /></td>
<td><input type="text" class="widefat" name="url[]" placeholder="http://" /></td>
<td><a class="sort">Sort</a></td>
</tr>
<?php endif; ?>
<!-- empty hidden one for jQuery -->
<tr class="empty-row screen-reader-text">
<td><a class="button remove-row" href="#">-</a></td>
<td><input type="text" class="widefat" name="name[]" placeholder="Charles Mingus"/></td>
<td><input type="text" class="widefat" name="role[]" placeholder="guitar/oud" /></td>
<td><input type="text" class="widefat" name="url[]" placeholder="http://" /></td>
<td><a class="sort">Sort</a></td>
</tr>
</tbody>
</table>
<p><a id="add-row" class="button" href="#">Add another</a>
<input type="submit" class="metabox_submit" value="Save" />
</p>
<?php
}
add_action(\'save_post\', \'repeatable_meta_box_save\');
function repeatable_meta_box_save($post_id) {
if ( ! isset( $_POST[\'repeatable_meta_box_nonce\'] ) ||
! wp_verify_nonce( $_POST[\'repeatable_meta_box_nonce\'], \'repeatable_meta_box_nonce\' ) )
return;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return;
if (!current_user_can(\'edit_post\', $post_id))
return;
$old = get_post_meta($post_id, \'repeatable_names\', true);
$new = array();
$names = $_POST[\'name\'];
$roles = $_POST[\'role\'];
$urls = $_POST[\'url\'];
$count = count( $names );
for ( $i = 0; $i < $count; $i++ ) {
if ( $roles[$i] != \'\' )
$new[$i][\'role\'] = stripslashes( strip_tags( $roles[$i] ) );
if ( $names[$i] != \'\' ) :
$new[$i][\'name\'] = stripslashes( strip_tags( $names[$i] ) );
if ( $urls[$i] == \'http://\' )
$new[$i][\'url\'] = \'\';
else
$new[$i][\'url\'] = stripslashes( $urls[$i] ); // and however you want to sanitize
endif;
}
if ( !empty( $new ) && $new != $old )
update_post_meta( $post_id, \'repeatable_names\', $new );
elseif ( empty($new) && $old )
delete_post_meta( $post_id, \'repeatable_names\', $old );
}
这是来自单个discog项目。php
<?php
$args = array( \'post_type\' => \'discog-item\', \'posts_per_page\' => -1 );
$loop = new WP_Query( $args );
$repeatable_names = get_post_meta($post->ID, \'repeatable_names\', true);
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo \'<div class="entry-content">
if (!empty($repeatable_names)) {
echo \'<ul class="personnel">\';
foreach ($repeatable_names as $field) {
$the_name = $field[\'name\'];
//THE NEXT LINE IS LINE 29
$the_role = $field[\'role\'];
$the_url = $field[\'url\'];
echo \'<li>\';
if (!empty($the_name)) {
echo $the_name;
}
if (!empty($the_role)) {
echo $the_role;
}
if (!empty($the_url)) {
echo \'<a href="\' . $the_url . \'" target="_blank">Visit Website</a>\';
}
echo \'</li>\';
}
echo \'</ul>\';
} else {
echo "Nothing to see here...";
}
the_content();
echo \'</div>\';
endwhile;
?>
最合适的回答,由SO网友:Pieter Goosen 整理而成
根据您的评论,只有当字段为空时才会发生这种情况。这意味着未设置特定字段。
如我在评论中所述,在尝试使用字段之前,您应该首先检查字段是否已设置。在第一段代码中,有以下三行
<td><input type="text" class="widefat" name="name[]" value="<?php if($field[\'name\'] != \'\') echo esc_attr( $field[\'name\'] ); ?>" placeholder="Charles Mingus"/></td>
<td><input type="text" class="widefat" name="role[]" value="<?php if($field[\'role\'] != \'\') echo esc_attr( $field[\'role\'] ); ?>" placeholder="guitar/oud"/></td>
<td><input type="text" class="widefat" name="url[]" value="<?php if ($field[\'url\'] != \'\') echo esc_attr( $field[\'url\'] ); ?>" placeholder="http://"/></td>
对于失败保存,如果使用
isset
在显示它们之前。要修复后端错误,请将这三行更改为
<td><input type="text" class="widefat" name="name[]" value="<?php if( isset( $field[\'name\'] ) ) echo esc_attr( $field[\'name\'] ); ?>" placeholder="Charles Mingus"/></td>
<td><input type="text" class="widefat" name="role[]" value="<?php if( isset( $field[\'role\'] ) ) echo esc_attr( $field[\'role\'] ); ?>" placeholder="guitar/oud"/></td>
<td><input type="text" class="widefat" name="url[]" value="<?php if( isset( $field[\'url\'] ) ) echo esc_attr( $field[\'url\'] ); ?>" placeholder="http://"/></td>
您需要在单个页面(第二块代码)中执行完全相同的操作。正如评论中指出的,您有一个语法错误,我不知道这是否只是一个复制粘贴错误,但是
echo \'<div class="entry-content">
应该是
echo \'<div class="entry-content">\';
还有
$repeatable_names = get_post_meta($post->ID, \'repeatable_names\', true);
应该在你的圈子里,而不是外面
这就是您的代码在单页模板中的外观
<?php
$args = array(
\'post_type\' => \'discog-item\',
\'posts_per_page\' => -1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
the_title();
echo \'<div class="entry-content">\';
$repeatable_names = get_post_meta($post->ID, \'repeatable_names\', true);
if (!empty($repeatable_names)) {
echo \'<ul class="personnel">\';
foreach ($repeatable_names as $field) {
echo \'<li>\';
if(isset($field[\'name\'])){
echo $field[\'name\'];
}
if(isset($field[\'role\'])){
echo $field[\'role\'];
}
if(isset($field[\'url\'])){
echo \'<a href="\' . $field[\'url\'] . \'" target="_blank">Visit Website</a>\';
}
echo \'</li>\';
}
echo \'</ul>\';
} else {
echo "Nothing to see here...";
}
the_content();
echo \'</div>\';
}
?>