【问题标题】:Add class to div wrapped around custom post type based off posts category将类添加到基于帖子类别的自定义帖子类型周围的 div
【发布时间】:2017-07-26 17:42:21
【问题描述】:

我正在寻找一种方法,将一个类注入到一个 div 中,该 div 包含基于帖子所属类别的自定义帖子类型。将分为三个类别;

电子书(类名cat1
信息图(类名cat2
案例研究(类名cat3

到目前为止,这是我调用自定义帖子类型的代码。目前,该页面将所有帖子显示为最后一个类别 - 案例研究 (cat3)。

        <?php $loop = new WP_Query( array( 'post_type' => 'resources', 'posts_per_page' => -1 ) ); ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

            <?php
            $post = $wp_query->post;

            if ( in_category('ebook', $post->ID) ) { ?>
                <div <?php body_class('tile scale-anm cat1 all end'); ?>>
                    <section class="small-12 medium-4 large-3 columns end">
                        <section class="grid">
                            <figure class="effect-sarah">
                                <img src="<?php the_field('img'); ?>" alt="img13"/>
                                <figcaption>
                                    <h2>Resource</h2>
                                    <p class="signika"><?php the_field('desc'); ?></p>
                                    <p class="bold"><?php the_field('btnText'); ?></p>
                                    <a href="<?php echo esc_url( get_permalink() ); ?>" target="_blank"><?php the_field('btnText'); ?></a>
                                </figcaption>           
                            </figure>
                        </section>
                    </section>
                </div> 
            <?php
            } 


            elseif ( in_category('infographic', $post->ID) ) { ?>
                <div <?php body_class('tile scale-anm cat2 all end'); ?>>
                    <section class="small-12 medium-4 large-3 columns end">
                        <section class="grid">
                            <figure class="effect-sarah">
                                <img src="<?php the_field('img'); ?>" alt="img13"/>
                                <figcaption>
                                    <h2>Resource</h2>
                                    <p class="signika"><?php the_field('desc'); ?></p>
                                    <p class="bold"><?php the_field('btnText'); ?></p>
                                    <a href="<?php echo esc_url( get_permalink() ); ?>" target="_blank"><?php the_field('btnText'); ?></a>
                                </figcaption>           
                            </figure>
                        </section>
                    </section>
                </div> 
            <?php
            } 


            elseif ( in_category('casestudy', $post->ID) ) { ?>
                <div <?php body_class('tile scale-anm cat3 all end'); ?>>
                    <section class="small-12 medium-4 large-3 columns end">
                        <section class="grid">
                            <figure class="effect-sarah">
                                <img src="<?php the_field('img'); ?>" alt="img13"/>
                                <figcaption>
                                    <h2>Resource</h2>
                                    <p class="signika"><?php the_field('desc'); ?></p>
                                    <p class="bold"><?php the_field('btnText'); ?></p>
                                    <a href="<?php echo esc_url( get_permalink() ); ?>" target="_blank"><?php the_field('btnText'); ?></a>
                                </figcaption>           
                            </figure>
                        </section>
                    </section>
                </div> 
            <?php
            }

            ?>          

        <?php endwhile; wp_reset_query(); ?>

【问题讨论】:

    标签: php jquery html css wordpress


    【解决方案1】:

    DRY,不要重复自己。你的循环也有一些问题。

    使用 WP_Query 后,使用wp_reset_postdata() 而不是wp_reset_query()

    摆脱$post = $wp_query-&gt;post;,使用get_the_ID()从循环中获取id

    然后我们检查类别并将类名分配给变量$catclass,我们将其插入到body_class()的参数中,注意使用"而不是'

    试试这个:

    <?php $loop = new WP_Query( array( 'post_type' => 'resources', 'posts_per_page' => -1 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php
        $catclass = "";
        if (in_category('ebook', get_the_ID())) {
            $catclass="cat1";
        } else if(in_category('infographic', get_the_ID())) {
            $catclass="cat2";
        } else if(in_category('casestudy', get_the_ID())){
            $catclass="cat3";
        }
    ?>
    
        <div <?php body_class("tile scale-anm {$catclass} all end"); ?>>
            <section class="small-12 medium-4 large-3 columns end">
                <section class="grid">
                    <figure class="effect-sarah">
                        <img src="<?php the_field('img'); ?>" alt="img13"/>
                        <figcaption>
                            <h2>Resource</h2>
                            <p class="signika"><?php the_field('desc'); ?></p>
                            <p class="bold"><?php the_field('btnText'); ?></p>
                            <a href="<?php echo esc_url( get_permalink() ); ?>" target="_blank"><?php the_field('btnText'); ?></a>
                        </figcaption>           
                    </figure>
                </section>
            </section>
        </div> 
    <?php
    endwhile; 
    wp_reset_postdata(); ?>
    

    如果您真的很兴奋,可以将 if/else 语句替换为:

    switch(true){
        case in_category('ebook', get_the_ID()):
        $catclass = "cat1";
        break;
        case in_category('infographic', get_the_ID()):
        $catclass = "cat2";
        break;
        case in_category('casestudy', get_the_ID()):
        $catclass = "cat3";
        break;
        default:
        $catclass = "";
    }
    

    【讨论】:

    • @WheatBreak 真漂亮....谢谢!效果很好!对此,我真的非常感激。还有一件事,我正在使用; data-rel="cat1" 过滤页面上的项目,但我认为这将不再起作用。有没有办法使用这个新系统过滤页面上现在显示的内容?
    • 那很模糊...我有三个按钮,每个类别一个。一旦单击不在所选类别中的帖子类型隐藏。我使用了 data-rel,当它只是 HTML 时它工作得很好。并不是说我要转移到自定义帖子类型。我需要找到一种方法来过滤按钮单击时的项目​​。提前致谢!
    • 不确定我是否完全理解,但如果你使用 jQuery,你可以像 jQuery(".cat1").hide() 一样隐藏它们
    • 我会研究 jQuery,再次感谢 @WheatBeak Suuuper 的帮助。
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多