【问题标题】:How can I display projects that only use a tag of my choosing. Currently all projects are being fetched如何显示仅使用我选择的标签的项目。目前正在获取所有项目
【发布时间】:2018-03-10 15:18:38
【问题描述】:

我正在尝试修改一个 projects.php 文件,该文件使用一些循环和 if 语句来根据那里的标签获取项目。我需要修改下面的代码以仅获取带有标签 new 的项目。目前所有项目都在抓取和展示中。

任何帮助都会很棒。请参阅随附的代码。

谢谢

<div id="projects" class="clearfix">        

<?php $page_skills = get_post_meta($post->ID, "_ttrust_page_skills", true); ?>


    <?php $skill_slugs = ""; $skills = explode(",", $page_skills); ?>

    <?php if (sizeof($skills) >= 1) : // if there is more than one skill, show the filter nav?> 
        <?php $skill = $skills[0]; ?>
        <?php $s = get_term_by( 'name', trim(htmlentities($skill)), 'skill'); ?>
        <?php if($s) { $skill_slugs = $s->slug; } ?><?php endif;        

        $temp_post = $post;
        $args = array(
            'ignore_sticky_posts' => 1,
            'posts_per_page' => 200,
            'post_type' => 'project',
            'skill' => $skill_slugs
        );
        $projects = new WP_Query( $args );      

    endif; ?>




<div class="wrap">
    <div class="thumbs masonry">            
    <?php  while ($projects->have_posts()) : $projects->the_post(); ?>

        <?php
        global $p;              
        $p = "";
        $skills = get_the_terms( $post->ID, 'skill');
        if ($skills) {
           foreach ($skills as $skill) {                
              $p .= $skill->slug . " ";                     
           }
        }
        ?>      
        <?php get_template_part( 'part-project-thumb'); ?>      

    <?php endwhile; ?>
    <?php $post = $temp_post; ?>
    </div>
</div>

【问题讨论】:

    标签: php wordpress loops tags


    【解决方案1】:

    你应该只需要在你的 $args 数组中添加一个字段:

    $args = array(
        'ignore_sticky_posts' => 1,
        'posts_per_page' => 200,
        'post_type' => 'project',
        'skill' => $skill_slugs,
        'tag' => 'new'  // add this for the tag
        );
    $projects = new WP_Query( $args );
    

    让我知道它是否有效:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      相关资源
      最近更新 更多