categories - How to add Pagination to foreach loop to page

admin2025-01-07  8

I create a page in wordpress series.php and here it the code where I block!

    <?php 
        $categories=get_categories(
            array( 'parent' => $cat->cat_ID)
        );

        foreach ($categories as $c) {
            // what you really want instead of var_dump is something to
            // to create markup-- list items maybe, For example
            $idp = $c->term_taxonomy_id;
            //then i get the data from the database
            $cat_data = get_option("category_".$idp);
        ?>      

        <div class="wide-box">
            <!-- Content Detail Box -->
            <a href="<?php echo get_category_link( $idp ); ?>" class="wb-image"><img src="<?php echo $cat_data['original_img']; ?>"></a>
            <!-- Wb Center -->
            <div class="wb-center">
                <h3 class="wb-name"><a href="<?php echo get_category_link( $idp ); ?>"><?php echo $c->name; ?></a></h3>
                <div class="wb-details">
                    Genres : <?php echo $cat_data['genres']; ?><br>
                    Country : <?php echo $cat_data['country']; ?><br>
                    Language : <?php echo $cat_data['language']; ?><br>
                    Year : <?php echo $cat_data['year']; ?>
                </div>
                <div class="latest-label">Latest Episode:</div>
                <?php
                    $catquery = new WP_Query( 'cat='.$idp.'&posts_per_page=1' );
                    while($catquery->have_posts()) : $catquery->the_post();
                ?>
                    <a href="<?php the_permalink() ?>" class="latest-ep"><?php the_title(); ?></a>
                <?php endwhile; ?>
            </div>
            <!-- Wb Center -->
        </div>      

    <?php                   
        }
    ?>
    </div>

So how to add paging for loop to show it?

I create a page in wordpress series.php and here it the code where I block!

    <?php 
        $categories=get_categories(
            array( 'parent' => $cat->cat_ID)
        );

        foreach ($categories as $c) {
            // what you really want instead of var_dump is something to
            // to create markup-- list items maybe, For example
            $idp = $c->term_taxonomy_id;
            //then i get the data from the database
            $cat_data = get_option("category_".$idp);
        ?>      

        <div class="wide-box">
            <!-- Content Detail Box -->
            <a href="<?php echo get_category_link( $idp ); ?>" class="wb-image"><img src="<?php echo $cat_data['original_img']; ?>"></a>
            <!-- Wb Center -->
            <div class="wb-center">
                <h3 class="wb-name"><a href="<?php echo get_category_link( $idp ); ?>"><?php echo $c->name; ?></a></h3>
                <div class="wb-details">
                    Genres : <?php echo $cat_data['genres']; ?><br>
                    Country : <?php echo $cat_data['country']; ?><br>
                    Language : <?php echo $cat_data['language']; ?><br>
                    Year : <?php echo $cat_data['year']; ?>
                </div>
                <div class="latest-label">Latest Episode:</div>
                <?php
                    $catquery = new WP_Query( 'cat='.$idp.'&posts_per_page=1' );
                    while($catquery->have_posts()) : $catquery->the_post();
                ?>
                    <a href="<?php the_permalink() ?>" class="latest-ep"><?php the_title(); ?></a>
                <?php endwhile; ?>
            </div>
            <!-- Wb Center -->
        </div>      

    <?php                   
        }
    ?>
    </div>

So how to add paging for loop to show it?

Share Improve this question edited Oct 2, 2016 at 7:27 bravokeyl 3,3776 gold badges27 silver badges33 bronze badges asked Oct 2, 2016 at 6:59 Nitin PoojaryNitin Poojary 11 silver badge3 bronze badges 1
  • Please Refer: wordpress.stackexchange.com/questions/304006/… – Jignesh Patel Commented Nov 2, 2018 at 13:15
Add a comment  | 

2 Answers 2

Reset to default 0

Add this argument to your WP_Query.

'paged' => get_query_var( 'paged' ),

And use a array style of arguments rather than a query post style.

you can used the paginate_links() function along with the paged parameter in your query.

<?php 
    $categories=get_categories(
        array( 'parent' => $cat->cat_ID)
    );

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Get current page or set to 1 if not set

    foreach ($categories as $c) {
        // what you really want instead of var_dump is something to
        // to create markup-- list items maybe, For example
        $idp = $c->term_taxonomy_id;
        //then i get the data from the database
        $cat_data = get_option("category_".$idp);
    ?>      

    <div class="wide-box">
        <!-- Content Detail Box -->
        <a href="<?php echo get_category_link( $idp ); ?>" class="wb-image"><img src="<?php echo $cat_data['original_img']; ?>"></a>
        <!-- Wb Center -->
        <div class="wb-center">
            <h3 class="wb-name"><a href="<?php echo get_category_link( $idp ); ?>"><?php echo $c->name; ?></a></h3>
            <div class="wb-details">
                Genres : <?php echo $cat_data['genres']; ?><br>
                Country : <?php echo $cat_data['country']; ?><br>
                Language : <?php echo $cat_data['language']; ?><br>
                Year : <?php echo $cat_data['year']; ?>
            </div>
            <div class="latest-label">Latest Episode:</div>
            <?php
                $category_query = new WP_Query( array(
                    'cat' => $idp,
                    'posts_per_page' => 1,
                    'paged' => $paged
                ) );
                while($category_query->have_posts()) : $category_query->the_post();
            ?>
                <a href="<?php the_permalink() ?>" class="latest-ep"><?php the_title(); ?></a>
            <?php endwhile; ?>
        </div>
        <!-- Wb Center -->
    </div>      

<?php                   
    }
    
    // Pagination
    echo paginate_links( array(
        'total' => $category_query->max_num_pages
    ) );
?>
</div>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262878a847.html

最新回复(0)