【问题标题】:Hide a Wordpress blog post from main list when it appears as featured当它显示为特色时,从主列表中隐藏 Wordpress 博客文章
【发布时间】:2020-05-02 07:33:20
【问题描述】:

如下图所示 - 我有一个博客存档页面,该页面将精选博客文章显示为大文章,然后显示其下方的所有博客文章。

我想在主博客循环中添加一些内容:

如果博客文章被推荐,请不要在此处显示

这样我就可以停止显示重复的博客文章(一篇精选,然后在主博客循环中显示相同的一篇)。

我有两段代码,一段用于拉出精选帖子,另一段用于循环播放所有帖子。这里

<!-- Featured Blog Item -->

<?php
$loop = new WP_Query( array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'meta_key'      => 'featured',
      'meta_value'  => 'yes'
  )
);
?>

<div class="container">
<div class="row no-gutters">

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <div class="col-12">

        <a href="<?php the_permalink(); ?>">
        <div class="hero__overlay featured-grad-blog">
        </div>
        </a>

        <div class="featured-blog-container">
            <h3><?php the_title(); ?></h3>
            <p><?php the_date(); ?></p>
        </div>
        <?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
        <div class="featured-blog-grid-image-container" style="background-image:url(<?php echo $feat_image; ?>);"></div>


    </div>


<?php endwhile; wp_reset_query(); ?>

</div>
</div>



<!-- All Blog Items -->

<?php
$loop = new WP_Query( array(
    'post_type' => 'post',
    'posts_per_page' => -1
  )
);
?>

<div class="container blog-page-container">
  <div class="row blog-page-row">

  <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>


      <div class="col-lg-4 col-sm-6 col-xs-12 blog-page-col">



        <div class="blog-image" style="position: relative;">
          <a href="<?php the_permalink(); ?>">
            <div class="hero__overlay grad-blog-hover"></div>
          </a>
          <?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
          <div class="blog-grid-image-container blog-page-image blog-post-image-div" style="background-image:url(<?php echo $feat_image; ?>);"></div>
        </div>

        <div class="blog-title">
          <a href="<?php the_permalink(); ?>">
            <h4><?php the_title(); ?></h4>
          </a>
        </div>

        <div class="blog-bars-outside-container">
          <div class="blog-bars-inside-container">
            <div class="blog-all-bar blog-bar1"></div>
            <div class="blog-all-bar blog-bar2"></div>
            <div class="blog-all-bar blog-bar3"></div>
            <div class="blog-all-bar blog-bar4"></div>
            <div class="blog-all-bar blog-bar5"></div>
          </div>
        </div>

        <div class="blog-excerpt">
         <?php the_excerpt(); ?>
        </div>

        <div class="meta-details">
          <p><?php the_author_meta( 'display_name', $author_id ); ?> - <?php echo get_the_date(); ?></p>
        </div>

      </div>


  <?php endwhile; wp_reset_query(); ?>

  </div>
</div>

我想我可能需要将与此相反的内容添加到主要博客文章的数组中:

'meta_key'      => 'featured',
'meta_value'    => 'yes'

但是我什么都做不了......

任何帮助将不胜感激!

【问题讨论】:

  • 您如何设置“特色”元数据?我假设这是一个自定义字段。你为什么不直接使用内置的置顶帖呢?
  • 是的,它实际上是一个自定义字段 - 粘性帖子是 Wordpress 的内置功能吗?还是我需要一个单独的插件?
  • 是的,置顶帖是内置的 WP 功能。看起来你得到了你正在寻找的答案,但这里有一些关于使用置顶帖子的文档:developer.wordpress.org/themes/functionality/sticky-posts
  • 非常感谢,它值得我为我建立的下一个网站看看 :)

标签: php wordpress conditional-statements


【解决方案1】:

对于非特色部分,请尝试:


<!-- All Blog Items -->

<?php
        $loop = new WP_Query( array(
                'post_type' => 'post',
                'posts_per_page' => -1,
                'meta_query' => array(
                    array(
                        'key' => 'featured',
                        'value' => 'yes',
                        'compare' => '!='
                    )
                )
            )
        );
?>

这应该选择所有 'featured' 不等于 '!=' 'yes' 的帖子。

【讨论】:

  • 谢谢你——这正是我想要的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
相关资源
最近更新 更多