2017-02-18 169 views
0

我有一个自定义的后类型的调用被称为'扇区'和另一种职位类型称为'挑战'挑战帖子类型有一个分类称为'sectortype' - 它具有与扇区相同的名称。WordPress添加变量到循环参数

我创建了一个名为'single-sector.php'的页面在该页面上显示一个循环,其中包含与该扇区相关的挑战。

当我编写显示挑战的循环时,如何使'sectortype'=>'advanced-education'变量成为变量,以便在其他单个扇区页面上工作?

下面是我对循环...

<?php $challenge_args = array(
    'post_type' => 'challenge', 
    'sectortype' => 'advanced-education', //Need Help Here 
    ); 
// create a new instance of WP_Query 
$challenge_query = new WP_Query($challenge_args); 
?> 
<?php if ($challenge_query->have_posts()) : while ($challenge_query->have_posts()) : $challenge_query->the_post(); // run the loop ?> 

回答

2

通过自定义分类方面获得自定义信息:

<?php 
    $terms = get_terms('sectortype'); 
    $challenge_args = array(
    'post_type' => 'challenge', 
    'publish_status' => 'published', 
    'posts_per_page' => -1, 
    'tax_query' => array(
     array(
     'taxonomy' => 'sectortype', 
     'field' => 'slug', 
     'terms' => $terms[0], //whichever term you want to select 
    ), 
    ), 
); 
// create a new instance of WP_Query 
$challenge_query = new WP_Query($challenge_args); 
?> 
<?php if ($challenge_query->have_posts()) : while ($challenge_query->have_posts()) : $challenge_query->the_post(); // run the loop ?> 

显示。在单独的页面 来显示不同的页面职位正如您在评论中提到的,您必须执行以下操作:

创建单独的页面链接::(页面导航项目)

<?php $categories = get_terms('sectortype');?> 
<ul> 
    <?php foreach($categories as $key => $c):?> 
     <?php $cat_link = get_term_link($c->term_id);?> 
     <?php $term_title= single_term_title('', false);?> 
     <li class="<?php echo ($c->name == $term_title)?'active':'';?>"><a href="<?php echo $cat_link;?>"><?php echo $c->name;?></a></li> 
     <?php endforeach;?> 
</ul> 

上使用创建的主题目录,文件名为“分类法sectortype.php”文件(实际上是分类术语归档模板)。

在该模板上,从常规循环中获取帖子而不使用任何查询,您将获得相应的帖子。

+0

对于分类学,我有商业,高等教育等类别。因此,通过您提供的编辑(谢谢),应该在正确的页面上显示来自给定分类的帖子,而无需为每个页面创建新页面一? – user3331701

+0

如果解决了您的问题,请参阅更新并将其标记为已接受。快乐编码! – Bayou