首页 文章

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

提问于
浏览
0

我在这里有一个标准的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>

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

我基本上想要显示一组带有“客户”类别的帖子缩略图的元组,然后是博客中所有其他类别的另一组元素 .

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

任何帮助将非常感谢:)

3 回答

  • 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>
    
  • 0

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

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

    如果你想仅为客户改变样式,我建议你使用 <?php the_field('quote_name'); ?> 将你的div .card 添加为与类别相同的类(你可能还需要 str_replacestrtolower 来规范化类名),然后使用 .card.client 应用CSS更改 .

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

    <?php if ( !in_category( 'ID' )){
        <p class="card-text"><?php the_excerpt(__('(more…)')); ?></p>
    }?>
    

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

相关问题