【问题标题】:How to don't show post having a specified tag in my homepage?如何不在我的主页中显示具有指定标签的帖子?
【发布时间】:2014-07-17 07:47:09
【问题描述】:

我是 WordPress 开发的新手,我正在尝试实现这个自定义主题来处理所谓的特色帖子http://lnx.asper-eritrea.com/

正如您在主页的帖子区域中看到的那样,我有 Articoli in evidenza 子区域,其中包含我的精选帖子,其下方是 Ultimi Articoli 包含最新帖子的子区域。

为了实现这一点,我使用了帖子标签,并在未来帖子区域中显示具有 tag=featured 条件的帖子。

这是我的代码:

<section id="blog-posts">

<header class="header-sezione">
        <h2>Articoli in evidenza</h2>
</header>

<?php query_posts('tag=featured');?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <div id="featured-posts">

    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
      <div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> &nbsp;//&nbsp;  <?php the_category(', ') ?>  &nbsp;//&nbsp;  <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?> 
      </div>
      <div class="featured-details"><?php the_excerpt()?>
      <?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
      <a href="<?php the_permalink(); ?>"><img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" /></a>
      </div>
    </div>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>



    <header class="header-sezione">
        <h2>Ultimi Articoli</h2>
    </header>

    <?php
    if (have_posts()) :
        // Start the Loop.
        while (have_posts()) : the_post();

            /*
             * Include the post format-specific template for the content. If you want to
             * use this in a child theme, then include a file called called content-___.php
             * (where ___ is the post format) and that will be used instead.
             */
            get_template_part('content', get_post_format());

        endwhile;
    else :
        // If no content, include the "No posts found" template.
        get_template_part('content', 'none');

    endif;
    ?>

</section>

正如您首先看到的,我通过使用 query-posts() 函数显示具有标签 featured 的帖子:

<?php query_posts('tag=featured');?>

现在我的问题是,如果帖子有 featured 标签,我不希望它显示在最新的帖子区域(此时显示)。所以我尝试使用这段代码:

<header class="header-sezione">
    <h2>Ultimi Articoli NOT FEATURED</h2>
</header>

<?php query_posts('tag != featured');?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <div id="featured-posts">

    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
      <div class="meta">
Scritto da <span class="author"><?php the_author_link(); ?></span> &nbsp;//&nbsp;  <?php the_category(', ') ?>  &nbsp;//&nbsp;  <?php comments_popup_link('Nessun Commento', '1 Commento ', '% Commenti'); ?> 
      </div>
      <div class="featured-details"><?php the_excerpt()?>
      <?php $featured_img = get_post_meta($post->ID, 'featured_img', $single = true); ?>
      <a href="<?php the_permalink(); ?>"><img src="<?php echo $featured_img ?>" alt="<?php the_title(); ?>" /></a>
      </div>
    </div>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

但不工作,特色帖子仍显示在主页上。如您所见,我尝试过指定要显示的帖子不能具有 featured 标签:

<?php query_posts('tag != featured');?>

为什么不工作?我错过了什么?你能帮我解决这个问题吗?

Tnx

【问题讨论】:

    标签: php content-management-system wordpress-theming wordpress


    【解决方案1】:

    要返回不包含特定标签的帖子,您应该使用pass the ID of the term into the tag__not_in 参数。

    // get the term using the slug and the tag taxonomy
    $term = get_term_by( 'slug', 'featured', 'post_tag' );
    // pass the term_id to tag__not_in
    query_posts( array( 'tag__not_in' => array ( $term->term_id ) );
    

    【讨论】:

    • mmm 我已经尝试使用您的代码,但它仍然显示具有 featured 标记的帖子...我错过了什么?我需要更改一些参数吗?
    • 好的,解决了阅读 get_term_by() 文档的问题...它是“post_tag”而不是“tag”:$term = get_term_by('slug', 'featured', 'post_tag');
    【解决方案2】:

    is_front_page()

    您应该尝试&lt;?php is_front_page(); ?&gt;,反之亦然。

    http://codex.wordpress.org/Function_Reference/is_front_page

    <?php if (is_front_page()) { ?>
        <!-- Do something -->
    <?php } else { ?>
        <!-- Add else only if you need it! -->
    <?php } ?>
    

    【讨论】:

    • 我认为这不是我的问题的解决方案:如果帖子有特定标签,我不想显示它!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2018-03-05
    • 2016-05-31
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    相关资源
    最近更新 更多