Wordpress - 如果在特定类别中,则隐藏博客元数据

时间:2018-01-17 16:34:34

标签: php wordpress loops if-statement

我在这里有一个标准的wordpress博客:http://webserver-meetandengage-com.m11e.net/insights/我已经创建了一个名为clients的新类别。

此存档页面上的客户帖子将具有与标准博客帖子不同的元数据,因此我想要删除摘录,日期和作者等。

为了达到这个目的,我尝试添加一个条件位代码,如果该帖子区域的类别是' client'然后echo style =" display:none;"在div里面。

这是我尝试的代码行:

<p<?php if ( in_category( 'client' )) { echo 'style="display:none;"' }?>>This is not client</p>

这是它出现的循环:

<div class="container blog-card-container">

    <div class="row">


        <?php if ( have_posts() ) : ?>

            <?php /* Start the Loop */ ?>

            <?php while ( have_posts() ) : the_post(); ?>

        <div class="col-md-4">  
        <a href="<?php the_permalink(); ?>">
                    <div class="card">

                        <div class="blog-thumb-container">
                            <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
                        </div>




                        <div class="blog-clients-card-block">
                            <?php if( get_field('quote_name') ): ?><p class="client-name" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_name'); ?></p><?php endif; ?>


                                <p<?php if ( in_category( 'client' )) { echo 'style="display:none;"' }?>>This is not client</p>


                            <p class="blog-cat-label"><?php the_category(', '); ?></p>
                            <h2 class="blog-card-title"><?php the_title(); ?></h2>
                            <p class="card-text"><?php the_excerpt(__('(more…)')); ?></p>
                            <p><strong><?php the_author(); ?></strong> | <?php the_date(); ?> </p>
                        </div>
                    </div>
                </a>
        </div>

        <?php understrap_pagination(); ?>

        <?php endwhile; wp_reset_postdata(); endif; ?>

    </div>

</div>

但是在这里包含它会打破循环并且页面无法加载...我不确定我做错了什么,或者即使可能有更好的解决方案?

我基本上想要显示一组用于发布缩略图的元数据&#39;客户端&#39;然后是博客中所有其他类别的另一组。

我想可能是IF类别的容器是客户端然后显示META1其他显示META2。

任何帮助都会受到大力赞赏:)

3 个答案:

答案 0 :(得分:0)

使用两个IF ELSE语句来实现此目的:

<div class="container blog-card-container">
    <div class="card-columns">


        <?php if ( have_posts() ) : ?>

            <?php /* Start the Loop */ ?>

            <?php while ( have_posts() ) : the_post(); ?>


        <a href="<?php the_permalink(); ?>">

                    <div class="card">





                        <!-- Image if loop =========================================== -->

                        <?php if ( in_category('14') ) : ?>


                            <div class="client-header-logo-card" style="background-color: <?php the_field('client_brand_colour'); ?>;">
                                <?php 

                                $image = get_field('client_logo');

                                if( !empty($image) ): ?>

                                    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

                                <?php endif; ?>
                            </div>              

                        <?php else: ?>

                            <div class="blog-thumb-container">
                                <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
                            </div>

                        <?php endif ?>




                        <!-- Meta Data if loop =========================================== -->

                        <div class="blog-clients-card-block">


                            <?php if ( in_category('14') ) : ?>


                                <p class="blog-cat-label"><?php the_category(', '); ?></p>

                                <h2><?php the_title(); ?></h2>

                                <?php if( get_field('quote') ): ?><p class="client-quote"><?php echo custom_field_excerpt(); ?></p><?php endif; ?>


                                <?php if( get_field('quote_name') ): ?><p class="client-name" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_name'); ?></p><?php endif; ?>
                                <?php if( get_field('quote_position') ): ?><p class="client-position" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_position'); ?></p><?php endif; ?>


                                <?php if( get_field('button_text') ): ?>
                                    <a class="btn btn-sm btn-client-archive" href="<?php the_permalink(); ?>" style="background-color:<?php the_field('client_brand_colour'); ?>;" role="button"><?php the_field('button_text'); ?></a>
                                <?php endif; ?>

                                <?php if( get_field('video_url') ): ?>
                                    <div class="embed-container">
                                        <?php the_field('video_url'); ?>
                                    </div>
                                <?php endif; ?>                 


                            <?php else: ?>

                                <p class="blog-cat-label"><?php the_category(', '); ?></p>
                                <h2 class="blog-card-title"><?php the_title(); ?></h2>
                                <p class="card-text"><?php the_excerpt(__('(more…)')); ?></p>
                                <p><strong><?php the_author(); ?></strong> | <?php the_date(); ?> </p>

                            <?php endif ?>





                    </div>
                </a>
        </div>

答案 1 :(得分:0)

您需要在变量中设置它并回显变量。例如,如果客户端显示:如果没有存储在$ style var中,那么它不会放置样式

<?php 
if(in_category('client')){$style = 'display:none;';} else {$style = '';}
?>  

<p style="<?php echo $style; ?>">This is not client</p>

答案 2 :(得分:-1)

如果您只想为客户更改样式,建议您将div .card添加为与使用<?php the_field('quote_name'); ?>的类别相同的类(您可能还需要{{1}和} str_replace规范化类名称),然后使用strtolower应用CSS更改。

如果您不想显示HTML部分,请不要渲染它们。例如:

.card.client

更好地使用类别的ID,因为稍后可以更改类别的名称。