You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义文章类型分类模板中Footer不显示问题排查

自定义分类法模板中页脚WP_Query查询失效

我创建了一个ID为21、名为Footer的页面,用来存储页脚的所有信息;同时将category.php作为自定义文章类型对应的自定义分类法模板。输出页脚数据的WP_Query查询在其他所有页面都能正常运行,但在自定义分类法页面上完全无法生效,尝试多种方法后仍找不到原因。


<?php

$args = array(
    'page_id' => 21
);

$footerQuery = new WP_Query($args);
if ($footerQuery->have_posts()) {
    // 循环遍历文章(此处仅一篇)
    while ($footerQuery->have_posts()) {
        $footerQuery->the_post();
?>

<footer class="">
    <div class="container mx-auto">
        <div class="footerGrid grid lg:grid-cols-3 gap-4 py-10 mx-10">
            <!-- 左侧区域 -->
            <div class="col-span-2">
                <div class="logoWrapper">
                    <a href="<?php echo site_url(); ?>">
                        <?php 
                            $custom_logo_id = get_theme_mod( 'custom_logo' );
                            $logo = wp_get_attachment_image_src( $custom_logo_id , 'logo-size' );
                            if ( has_custom_logo() ) {
                                echo '<img class="logo" src="' . esc_url( $logo[0] ) . '" alt="' . get_bloginfo( 'name' ) . '">';
                            } else {
                                echo '<h1>' . get_bloginfo('name') . '</h1>';
                            }
                        ?>
                    </a>
                </div>
                <div class="footer_leftSide_content max-w-[420px]">
                    <?php if( have_rows('footer_content_left_side') ): ?>
                        <?php while( have_rows('footer_content_left_side') ): the_row(); 

                            // 获取子字段值
                            $heading = get_sub_field('footer_content_left_side_heading');
                            $subheading = get_sub_field('footer_content_left_side_subheading');
                            ?>

                            <h3 class="text-white font-prompt font-normal text-2xl mt-12 mb-4"><?php echo $heading; ?></h3>
                            <h4 class="text-white font-prompt font-normal text-lg"><?php echo $subheading; ?></h4>

                            <a class="getInTouch_button py-2 px-5 bg-green text-brown font-jost font-normal text-lg rounded-lg mt-6" type="button" href="<?php echo site_url('/contact')?>" target="_blank">Get in touch →</a>
                            
                        <?php endwhile; ?>
                    <?php endif; ?>
                </div>
            </div>
            <!-- 右侧区域 -->
            <div class="hidden lg:block col-span-1 mx-auto my-auto">
                <div class="footer_rightSide_content">
                <?php if( have_rows('footer_content_right_side') ): ?>
                        <?php while( have_rows('footer_content_right_side') ): the_row(); 

                            // 获取子字段值
                            $email = get_sub_field('footer_content_right_side_email');
                            $phoneNumber = get_sub_field('footer_content_right_side_phone_number');
                            ?>

                            <a href="mailto:<?php echo $email; ?>" class="footerLinks text-white font-prompt font-normal text-lg my-1"><?php echo $email; ?></a>
                            <a href="tel:<?php echo $phoneNumber; ?>" class="footerLinks text-white font-prompt font-normal text-lg"><?php echo $phoneNumber; ?></a>
                            <div class="footerRight_socialMedia flex justify-start mt-5">
                            <?php
                            if( have_rows('footer_content_right_side_social_media_icons') ):
                                while( have_rows('footer_content_right_side_social_media_icons') ) : the_row();

                                    $socialMedia_icon = get_sub_field('footer_content_right_side_repeater_icon');
                                    $socialMedia_link = get_sub_field('footer_content_right_side_repeater_link');
                                    $size = 'social-media-icons';
                                    if( $socialMedia_icon ) { ?>

                                    <a href="<?php echo $socialMedia_link; ?>" target="_blank"><?php echo wp_get_attachment_image( $socialMedia_icon, $size ); ?></a>

                                    <?php 
                                    }

                                endwhile;
                            endif;
                            ?>
                            </div>
                        <?php endwhile; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</footer>

<?php
        }

    } else {
        // 未找到页面
        echo '<p>页面未找到。</p>';
    }
    wp_reset_postdata();
?>

<?php wp_footer(); ?>
</body>
</html>

category.php 代码

<?php get_header(); ?>

<!-- 项目标题区域 -->
<section id="aboutUs_heading">
    <div class="container mx-auto">
        <div class="mx-10">
            <div class="aboutUs_heading__wrapper pt-24 pb-1">
                <h1 class="thentwrktheme_page__heading text-center lg:text-left text-white font-prompt lg:ml-14">作品</h1>
            </div>
        </div>
    </div>
</section>

<!-- 项目分类区域 -->
<section id="projectCategories">
    <div class="container mx-auto">
        <div class="mx-10">
            <div class="projectCategories_wrapper flex justify-between lg:justify-start text-center lg:text-left items-center my-10">
                <a class="cat-list_item text-white cat-list_item text-white mx-1 lg:mx-3 py-1.5 px-3 lg:px-5 bg-zinc-500 uppercase font-jost" href="<?php echo get_post_type_archive_link('project');?>">全部</a>
                <?php 
                    $cat_args = get_terms(array(
                        'taxonomy' => 'category',
                        'order' => 'DSC',
                    ));

                    $categories = $cat_args;
                    $current_category_slug = get_queried_object()->slug;

                    foreach ($categories as $term) : 
                        $active_class = ($current_category_slug === $term->slug) ? 'active' : '';
                    ?>
                        <li class="project_categories__wrapper w-fit">
                            <a class="cat-list_item text-white mx-1 lg:mx-3 py-1.5 px-3 lg:px-5 bg-zinc-500 uppercase font-jost <?= $active_class; ?>" href="<?php echo site_url('/category/' . $term->slug)?>">
                                <?= $term->name; ?>
                            </a>
                        </li>
                    <?php 
                    endforeach; 
                ?>
            </div>
        </div>
    </div>
</section>

<!-- 项目文章区域 -->
<section id="projectPosts" class="my-10">
    <div class="container mx-auto">
        <div class="mx-10">
        <div class="grid-wrapper">
            <?php

                $i = 0;
                // WordPress循环
                $category = get_queried_object(); // 获取当前分类对象

                $args = array(
                    'post_type'      => 'post', // 需要时改为自定义文章类型
                    'posts_per_page' => -1, // 显示分类下所有文章
                    'category_name'  => $category->slug, // 使用分类别名查询
                );
                
                $categoryQuery = new WP_Query($args);
                
                if ($categoryQuery->have_posts()) :
                    while ($categoryQuery->have_posts()) : $categoryQuery->the_post();

                        $thumb = get_the_post_thumbnail_url(); 
                        ?>
                                <div class="workGrid-item projectPosts_post__card-<?php echo $i; ?> relative" style="background-image: url('<?php echo $thumb;?>')">
                                    <h3 class="projectCard_title font-prompt text-white absolute bottom-3 left-5 z-20"><?php the_title(); ?></h3>
                                    <a href="<?php the_permalink(); ?>" target="_blank" class="w-full h-full absolute z-30"></a>
                                </div>
                        <?php
                        $i++;
                    endwhile;
                else :
                    ?>
                    <p>未找到文章</p>
                <?php endif; 
                wp_reset_postdata();
                ?>
            </div>
        </div>
    </div>
</section>

<?php get_footer() ?>

自定义分类法使用category.php模板的过滤函数

// 向查询对象添加自定义文章类型
function my_query_post_type($query) {
   if ( is_category() && ( ! isset( $query->query_vars['suppress_filters'] ) || false == $query->query_vars['suppress_filters'] ) ) {
       $query->set( 'post_type', array( 'post', 'project' ) );
       return $query;
   }
}
add_filter('pre_get_posts', 'my_query_post_type');

内容的提问来源于stack exchange,提问作者COBNETCKNN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 13:40:57