0

how to count the elements inside a foreach and display the amount in wp-menu. I have the following code to count the elements but can't get a solution to echo it in the menu

        <?php
            $count = 0; //Hier ist der Count-Begin-Foreach
            foreach ( $bookmarks as $bookmark ) :                                       

            $count++; //Hier ist der Count-Inner-Foreach

            if ( get_post_status( $bookmark->post_id ) !== 'publish' ) {
                continue;
            }
            $has_bookmark = true;
            ?>
            <tr>
                <td width="50%">

                    <?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 " . $count . " Lesezeichen gesetzt."; //Hier ist der Count-Output?>

1 Answer 1

1

You're incrementing $count but I see you're referencing $counter. Not sure if those are different but here is a version that uses the same variable in all those places.

$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?>

If you want to get the count, you might as well make a function to do just that. Then call it directly. I have no idea where $bookmarks is coming from so you'll have to figure that one out.

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>
11
  • hi jgraup, the <? php echo $counter ?> in line 14 was wrong and i removed it. do you have an idea how to call the php and echo the counter in the menu? brg, nikita Nov 15, 2016 at 12:36
  • I'm not really sure what I'm looking at here. Is this code to make a menu? Looks like a table with an unordered list. Can you make a sample of what the HTML output should be and where you expect the counter?
    – jgraup
    Nov 15, 2016 at 13:04
  • You are right. This code generates a table and count the elements inside of it. I would like to grab the counted amount and view it on the menu... Nov 15, 2016 at 13:13
  • Cool, but without you showing that menu it's impossible to help you. The word menu can take thousands of different meanings in code. You will need to show all your code for further assistance.
    – jgraup
    Nov 15, 2016 at 13:17
  • Ouh.. i see. With "menu" i don't mean a specific code or class. Just the usual primary navigation menu at the top of each site. Nov 15, 2016 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.