wordpress代码

wordpress new WP_Query 不影响当前产品的the_post

在菜单要显示产品,这时就会影响single product。

所以改用下面代码:

 

<?php 
    $args22         = array(
      'post_type' => 'products',
            'posts_per_page'      => 8,
            //'cat'                 => $cats,
            'orderby'             => 'date',
            'ignore_sticky_posts' => true,
            // 'post__not_in'        => array( $post->ID ),
        );
     

        $custom_query = new WP_Query( $args22 );
        

   

if ($custom_query->have_posts()) {
    foreach ($custom_query->posts as $custom_post) {
        // 直接访问post对象属性而不使用the_post()
       // echo $custom_post->post_title;
        // ...
 //pre($custom_post);

  $title = $custom_post->post_title;
  $excerpt = $custom_post->post_excerpt;
  $link = $custom_post->guid;  //nou use the_permalink()

  $image = '';
   
   if ( has_post_thumbnail() ) {
   
     $image = get_the_post_thumbnail( $custom_post->ID, 'medium', array(
       //  'title' => $title_attribute,
         'alt'   => $title,'loading'   => 'lazy',
       ) ) ;
     }
  


 
?>

 
      <a  href="<?php echo $link ?>">
              <?php echo $image?> 
                 <?php echo $title?>
                  
             </a>
         
        

        <?php    
          }
}
  ?>