وردبريس loops
Ahmed Mohamed منذ 9 أشهر
عشان نوصل لمحتوي الموقع سواء بوستات او منتجات بنستعمل loop
فبنقدر نصمم شكل product card او بوست واحد ونستعمل ال loop عشان تظهر كل البيانات او البيانات ال احنا محتاجينها
في اكتر من طريقة لعمل loop
بشكل عام بنشوف الاول لو في بوستات او لا
if ( have_posts() ){}
بعد كدا طالما في بوستات بنعرضها
while ( have_posts() ){
the_post();
}
وبعد كدا بشوف محتاجين ايه من البوست زي الاسم الرابط الصورة المحتوي او المحتوي القصير والتاريخ وغيره
the_permalink();
the_title();
the_post_thumbnail('');
the_content();
the_category(', ');
get_the_date();
the_excerpt();
وبنبدأ نستعملهم فالمحتوي زي
<a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <h2> <?php the_title(); ?> </h2>
مثال
<?php while ( have_posts() ) : the_post(); ?> <h2> <?php the_title(); ?> </h2> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php endwhile; ?>
ولو هنظهر منتجات woocommerce الموضوع نفسه لكن هنوضح الاول نوع البوست
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
);
$products = new WP_Query( $args );
بكدا وصلنا للمنتجات وبعد كدا هنعمل نفس ال loop
<?php while ( $products->have_posts() ) : $products->the_post(); ?> <h2> <?php the_title(); ?> </h2> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php endwhile; wp_reset_postdata(); ?>

