计数每一次并在菜单php中显示

时间:2016-11-14 作者:Nikita Pankratz

如何计算foreach中的元素并在wp菜单中显示数量。我有以下代码来计算元素,但无法在菜单中找到相应的解决方案

        <?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 个回复
SO网友:jgraup

您正在递增$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>