본문 바로가기
WP

카테고리 최근글 쿼리, 갯수는 사용자정의에서

by 영감은어디에 2025. 2. 27.
 <?php
            $args = array(
                'category_name' => 'recom', 
                'posts_per_page' =>  get_theme_mod( 'mainslidimg_num', 8 ), // 갯수는 사용자정의
                'orderby' => 'date', 
                'order' => 'DESC'  
            );
            $query = new WP_Query($args);
            if ($query->have_posts()) {
                echo '<ul class="swiper-wrapper">';
                while ($query->have_posts()) {
                    $query->the_post();
                    ?>
                    <li class="scrollTop col swiper-slide" data-animate="movetop">
                        <a href="<?php echo get_permalink(); ?>">
                            <div class="imgbox">
                                <?php 
                                if (has_post_thumbnail()) {
                                    the_post_thumbnail('post-thumbnail-img');
                                } else {
                                    $content = apply_filters('the_content', get_the_content());
                                    preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
                                    $first_image_url = isset($matches[1]) ? $matches[1] : '';
                                    if ($first_image_url) {
                                        echo '<img src="' . esc_url($first_image_url) . '" alt="' . get_the_title() . '" />';
                                    } else {
                                        echo '<img src="' . esc_url(get_template_directory_uri() . '/img/default-thumbnail.jpg') . '" alt="Default Thumbnail" />';
                                    }
                                }
                                ?>
                            </div>
                            <div class="textbox">
                                <p class="title"><?php the_title(); ?></p>
                                <p class="excerpt">
                                    <?php 
                                    if (has_excerpt()) {
                                        the_excerpt();
                                    } else {
                                        $content = wp_trim_words(get_the_content(), 30, '...');
                                        echo $content;
                                    }
                                    ?>
                                </p>
                            </div>
                        </a>
                    </li>
                    <?php
                }
                echo '</ul>';
                wp_reset_postdata(); 
            } else {
                echo '글이 없습니다.';
            }
                ?>