douliang6563 2014-07-17 07:47
浏览 37
已采纳

如何在我的主页上显示有指定标签的帖子?

I am pretty new in WordPress development and I am trying to implement this custom theme that handle the so called featured posts: http://lnx.asper-eritrea.com/

As you can see in the posts area of the homepage I have the Articoli in evidenza sub area that contains my featured posts and under it the Ultimi Articoli subarea that contains the latest posts.

To implment this I use the posts tag and in the futured posts area I show the posts having the tag=featured condition.

So this is my code:

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

As you can see first I show the posts having a tag featured by the use of query-posts() function:

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

Now my problem is that if a post have the featured tag I don't want that it is shown in the latest post area (at this time it is shown). So I tried to use this code:

<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; ?>

But don't work and the featured post still shown in the homepage. As you can se I tried so specify that, to be shown, a post can't have the featured tag:

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

Why don't work? What am I missing? Can you help me to fix this issue?

Tnx

  • 写回答

2条回答 默认 最新

  • douxunchen3498 2014-07-17 07:57
    关注

    To return posts that do not contain a particular tag, you should use pass the ID of the term into the tag__not_in argument.

    // 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 ) );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作