【问题标题】:Wordpress - Hide blog meta data if in specific categoryWordpress - 如果在特定类别中,则隐藏博客元数据
【发布时间】:2018-06-26 14:46:56
【问题描述】:

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

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

为了实现这一点,我尝试添加一个条件代码,如果此帖子区域的类别是“客户端”,则 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>

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

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

我猜它可能是如果容器的类别是客户端然后显示 META1 否则显示 META2。

任何帮助将不胜感激:)

【问题讨论】:

    标签: php wordpress loops if-statement


    【解决方案1】:

    通过两个 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>
    

    【讨论】:

      【解决方案2】:

      您需要在变量中设置它并回显该变量。例如,如果客户端 display:none 存储在 $style var 中,如果不是,则它不会放置样式

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-12
        • 2017-12-10
        • 2013-04-01
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 2018-04-20
        相关资源
        最近更新 更多