您正在递增$count
但我看到你在引用$counter
. 不确定这些是否不同,但这里有一个在所有这些地方使用相同变量的版本。
$counter = 0; //Hier ist der Count-Begin-Foreach
foreach ( $bookmarks as $bookmark ) :
if ( get_post_status( $bookmark->post_id ) !== \'publish\' ) {
continue;
}
// moving below the if statement
$counter ++; //Hier ist der Count-Inner-Foreach
$has_bookmark = true;
?>
<tr>
<td width="50%">
<?php echo $counter ?>
<?php echo \'<a href="\' . get_permalink( $bookmark->post_id ) . \'">\' . get_the_title( $bookmark->post_id ) . \'</a>\'; ?>
<ul class="job-manager-bookmark-actions">
<?php
$actions = apply_filters( \'job_manager_bookmark_actions\', array (
\'delete\' => array (
\'label\' => __( \'Delete\', \'wp-job-manager-bookmarks\' ),
\'url\' => wp_nonce_url( add_query_arg( \'remove_bookmark\', $bookmark->post_id ), \'remove_bookmark\' ),
),
), $bookmark );
foreach ( $actions as $action => $value ) {
echo \'<li><a href="\' . esc_url( $value[ \'url\' ] ) . \'" class="job-manager-bookmark-action-\' . $action . \'">\' . $value[ \'label\' ] . \'</a></li>\';
}
?>
</ul>
</td>
<td width="50%">
<?php echo wpautop( wp_kses_post( $bookmark->bookmark_note ) ); ?>
</td>
</tr>
<?php endforeach; ?>
<?php print "Sie haben " . $counter . " Lesezeichen gesetzt."; //Hier ist der Count-Output?>
如果要获取计数,不妨创建一个函数来实现这一点。然后直接给它打电话。我不知道在哪里
$bookmarks
是从哪里来的,所以你必须弄清楚。
function get_number_of_bookmarks( $bookmarks = array () ) {
$counter = 0;
foreach ( $bookmarks as $bookmark ) :
if ( get_post_status( $bookmark->post_id ) !== \'publish\' ) {
continue;
}
$counter ++;
endforeach;
return $counter;
}
?>
<div class="primary nav-menu">
<?php
// do your menu
echo primary_nav_menu();
// Example with an empty array, but this should be your bookmarks array
$bookmarks_array = array();
// echo the count
echo get_number_of_bookmarks( $bookmarks_array );
?>
</div>