WooCommerce Custom Pagination

时间:2018-11-15 作者:JoeCold

有人能帮我解决我的定制归档产品的问题吗。php?

我正在尝试将分页添加到我制作的网格中,但不知道从何处开始。这是我的产品代码:

<?php $the_query = new WP_Query( array(
   \'posts_per_page\'=>12,
   \'post_type\'=>\'product\',
   \'paged\' => get_query_var(\'paged\') ? get_query_var(\'paged\') : 1)
   ); 
?>

<div class="container marginT10P">
    <div id="products" class="row view-group">
        <?php $query = new WP_Query( $args );
            if ( $the_query->have_posts() ) {
            global $wpdb;
            while ( $the_query->have_posts() ) : $the_query->the_post();
            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $the_query->post->ID ), \'single-post-thumbnail\' );
        ?>

  <div class="item col-xs-4 col-md-6 col-lg-4">
    <div class="thumbnail card">
      <div class="img-event">
        <a href="<?php the_permalink(); ?>"><img class="group list-group-image img-fluid propertysize" src="<?php echo $image[0]; ?>" alt="<?php get_post_meta( $attachment_id, \'_wp_attachment_image_alt\', true ); ?>" alt="" /></a>
    </div>
    <div class="caption card-body">
        <?php global $woocommerce;
            $currency = get_woocommerce_currency_symbol();
            $price = get_post_meta( get_the_ID(), \'_regular_price\', true); 
        ?>
        <h4 class="group card-title inner list-group-item-heading text-grey"><?php echo $currency; echo $price; ?> pcm</h4>
        <i class="fa fa-bath"></i> <?php echo $product->get_attribute( \'bathrooms\' ); ?>
        <div class="icon-marginL10"><i class="fa fa-bed"></i> <?php echo $product->get_attribute( \'bedrooms\' ); ?></div>
        <div class="icon-marginL10"><i class="fa fa-couch"></i> <?php echo $product->get_attribute( \'lounges\' ); ?></div>
        <p class="group inner list-group-item-text bold no-margin font-size14 text-grey"><?php the_title(); ?></p>
        <p class="group inner list-group-item-text font-size14"><?php echo get_post_meta( get_the_ID(), \'grid-description\', true ); ?></p>
        <div class="row">
            <div class="col-xs-12 col-md-6 col-lg-6 text-left tabletL-margin">
                <a class="btn btn-primary padding40 bold" href="<?php the_permalink(); ?>">Full Details</a>
            </div>
            <div class="col-xs-12 col-md-6 col-lg-6 text-right">
                <a class="btn btn-success padding20 bold" href="<?php echo esc_url( home_url( \'/request-call-back\' ) ); ?>">Arrange To View</a>
            </div>
        </div>
    </div>
    <div class="img-event">
        <a class="btn btn-danger padding40 width100 bold" href="tel:01472222333">Call 01472 222333</a>  
    </div>
</div>
</div>
  <?php endwhile;
    } else {
      echo __( \'No products found\' );
    }
  ?>
</div>
</div>
抱歉,代码太乱了,我会在完成后对其进行优化

1 个回复
SO网友:Baikare Sandeep

希望你做得很好。

您可以使用以下示例代码:

Updated: 我已经更新了我的要点链接,添加了模板的代码。创建名为的模板template-product.php

<?php
/**
 * @author Baikare Sandeep
 * 
 * Template Name: Product
 */


get_header(); ?>

<?php 
    $args = array(
        \'posts_per_page\'=> 12,
        \'post_type\' => \'product\',
        \'post_status\'   => \'publish\',
        \'paged\'     => get_query_var(\'paged\') ? get_query_var(\'paged\') : 1
    ); 
?>

<div class="container marginT10P">
    <div id="products" class="row view-group">
        <?php $the_query = new WP_Query( $args );
            if ( $the_query->have_posts() ) {
            global $wpdb;
            while ( $the_query->have_posts() ) : 
                $the_query->the_post();
                $image = wp_get_attachment_image_src( get_post_thumbnail_id( $the_query->post->ID ), \'single-post-thumbnail\' );
                ?>

                  <div class="item col-xs-4 col-md-6 col-lg-4">
                    <div class="thumbnail card">
                      <div class="img-event">
                        <a href="<?php the_permalink(); ?>"><img class="group list-group-image img-fluid propertysize" src="<?php echo $image[0]; ?>" alt="<?php get_post_meta( $attachment_id, \'_wp_attachment_image_alt\', true ); ?>" alt="" /></a>
                    </div>
                    <div class="caption card-body">
                        <?php global $woocommerce;
                            $currency = get_woocommerce_currency_symbol();
                            $price = get_post_meta( get_the_ID(), \'_regular_price\', true); 
                        ?>
                        <h4 class="group card-title inner list-group-item-heading text-grey"><?php echo $currency; echo $price; ?> pcm</h4>
                        <i class="fa fa-bath"></i> <?php echo $product->get_attribute( \'bathrooms\' ); ?>
                        <div class="icon-marginL10"><i class="fa fa-bed"></i> <?php echo $product->get_attribute( \'bedrooms\' ); ?></div>
                        <div class="icon-marginL10"><i class="fa fa-couch"></i> <?php echo $product->get_attribute( \'lounges\' ); ?></div>
                        <p class="group inner list-group-item-text bold no-margin font-size14 text-grey"><?php the_title(); ?></p>
                        <p class="group inner list-group-item-text font-size14"><?php echo get_post_meta( get_the_ID(), \'grid-description\', true ); ?></p>
                        <div class="row">
                            <div class="col-xs-12 col-md-6 col-lg-6 text-left tabletL-margin">
                                <a class="btn btn-primary padding40 bold" href="<?php the_permalink(); ?>">Full Details</a>
                            </div>
                            <div class="col-xs-12 col-md-6 col-lg-6 text-right">
                                <a class="btn btn-success padding20 bold" href="<?php echo esc_url( home_url( \'/request-call-back\' ) ); ?>">Arrange To View</a>
                            </div>
                        </div>
                    </div>
                    <div class="img-event">
                        <a class="btn btn-danger padding40 width100 bold" href="tel:01472222333">Call 01472 222333</a>  
                    </div>
                </div>
                </div>
                <?php 
            endwhile;

            $big = 999999999; // need an unlikely integer
             echo paginate_links( array(
                \'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
                \'format\' => \'?paged=%#%\',
                \'current\' => max( 1, get_query_var(\'paged\') ),
                \'total\' => $the_query->max_num_pages
            ) );

            wp_reset_postdata();
    } else {
      echo __( \'No products found\' );
    }
  ?>
</div>
</div>


<?php
get_footer();
我已经对它进行了测试,并且正在使用分页。

希望这对你有帮助。

结束

相关推荐

如何将php添加到主题以显示alt属性

我正试图找出如何将PHP/代码添加到模板中,以便在图像中显示alt标记。这个主题已经被一家不再存在的公司修改了,所以我不能问如何纠正这个问题。这是主题开发人员给我的代码,但它与主题附带的文件不对应。<?php echo \'<img src=\"\' . $src[0] . \'\" alt=\"\' . get_the_title() . \'\" />\'; ?> 我想我的问题是,我如何使这项工作,使我的alt标记显示出来。这是网站https://www.emabar