php 如何选择我的循环的元素编号10和25,删除< a>< /a>?

8xiog9wr  于 2023-03-21  发布在  PHP
关注(0)|答案(1)|浏览(102)

我需要你的帮助。我怎样才能删除我的循环中的数字10和25的标签?下面是代码,提前谢谢你。

function myFunction() {
        $markup = '';
        
        $args = array(  
            'post_type' => 'partner',
            'post_status' => 'publish',
            'posts_per_page' => -1, 
            'orderby' => 'menu_order', 
            'order' => 'ASC',
            'tax_query' => array(
                array (
                    'taxonomy' => 'partner_type',
                    'field' => 'slug',
                    'terms' => 'national'
                )
            )
        );
        
        $loop = new WP_Query( $args );
        
        if ($loop->found_posts > 0) {
            $markup .= '<div class="container ap-national-partners">';
            $markup .= '<div class="row d-flex align-items-center justify-content-center">';
            
            while ( $loop->have_posts() ) : $loop->the_post();
                $imageId = get_field("logo");
            
                if ($imageId) {
                    $imageDetails = wp_get_attachment_image_src($imageId, 'full');
                    
                    if ($imageDetails !== false) {
                        $markup .= '<div class="col-2 mt-5">';
                        $markup .= '<a href="' . get_field("url") . '" target="_blank">';
                        $markup .= '<img src="' . $imageDetails[0] . '" class="img-fluid" alt="' . get_the_title() . '" width="' . $imageDetails[1] . '" height="' . $imageDetails[2] . '" />';
                        $markup .= '</a>';
                    }
                }
                
                $markup .= '</a>';
                $markup .= '</div>';
            endwhile;
            
            wp_reset_postdata();
            
            $markup .= '</div>';
            $markup .= '</div>';
        }
        
        return $markup;
    }

我试图在循环中添加下面的代码,但它不起作用。

if ($imageDetails == 25) {
     $markup .= '<div class="col-2 mt-5">';
     $markup .= '<img src="' . $imageDetails[0] . '" class="img-fluid" alt="' . get_the_title() . '" width="' . $imageDetails[1] . '" height="' . $imageDetails[2] . '" />';
     $markup .= '</a>';
}
ibrsph3r

ibrsph3r1#

在循环外放置一个计数器,在循环内递增。在循环内比较10或25。

function myFunction() {
    $markup = '';

    $args = array(
        'post_type' => 'partner',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'tax_query' => array(
            array (
                'taxonomy' => 'partner_type',
                'field' => 'slug',
                'terms' => 'national'
            )
        )
    );

    $loop = new WP_Query( $args );

    if ($loop->found_posts > 0) {
        $markup .= '<div class="container ap-national-partners">';
        $markup .= '<div class="row d-flex align-items-center justify-content-center">';

        $articleCounter = 1;
        while ( $loop->have_posts() ) : $loop->the_post();
            $imageId = get_field("logo");

            if ($imageId) {
                $imageDetails = wp_get_attachment_image_src($imageId, 'full');

                if ($imageDetails !== false) {
                    $imageMarkUp = '<img src="' . $imageDetails[0] . '" class="img-fluid" alt="' . get_the_title() . '" width="' . $imageDetails[1] . '" height="' . $imageDetails[2] . '" />';
                    $markup .= '<div class="col-2 mt-5">';
                    if (in_array($articleCounter, [10, 25], true)) {
                        $markup .= $imageMarkUp;
                    } else {
                        $markup .= '<a href="' . get_field("url") . '" target="_blank">';
                        $markup .= $imageMarkUp;
                        $markup .= '</a>';
                    }
                    $markup .= '</div>';
                }
            }

            $articleCounter++;
        endwhile;

        wp_reset_postdata();

        $markup .= '</div>';
        $markup .= '</div>';
    }

    return $markup;
}

相关问题