显示帖子基于称号,自定义文章类型

问题描述:

在我的网站我充满了公司的目录(自定义后型),各自有各自的清单页面。现在在页面的底部,我想显示所有贴有该公司标题的帖子,如其目录列表页面所示。显示帖子基于称号,自定义文章类型

目录页

示例公司
示例公司

为了测试相关的帖子,我手动把 'rennicks' 作为标记。然后在大约5个帖子中添加了'rennicks'作为标签,并且它们都显示在列表页面中。但显然我需要它动态检索标题并根据该变量中的数据搜索标签。

$original_query = $wp_query; 
$wp_query = null; 
$args=array('tag' => 'rennicks'); 
$wp_query = new WP_Query($args); 
if (have_posts()) : 
?> 
    <?php 
     while (have_posts()) : the_post(); ?> 
      <div class="small-12 medium-6 columns content-excerpt"> 
       <div class="thumbnail medium-6 columns nopm"> 
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> 
       </div> 
       <div class="content"> 
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        <p><?php //the_excerpt(); ?></p> 
        <div class="related-stats-info"> 
         <ul> 
          <!-- <li><?php //the_author(); ?></li> --> 
          <li><i class="fa fa-clock-o"></i> <?php the_date('Y-m-d') ?></li> 
          <li><i class="fa fa-comments"></i> <?php comments_number('0 comments', '1 comment', '% comments'); ?></li> 
         </ul> 
        </div> 
       </div> 
      </div> 
     <?php endwhile; ?> 
     <div class="clearboth"></div> 
<?php 
endif; 
$wp_query = null; 
$wp_query = $original_query; 
wp_reset_postdata(); 
+0

我不知道最终目标是什么。您是否想要动态更改正在引入的标记,而不是硬编码“rennicks”?所以,也许你正试图让自定义类型的帖子使用所有标签? – 2014-12-02 17:55:59

+0

知道了!那么,这不会是一个问题,但只有我们需要首先理清。标签(AFAIK)不能有空格,并且有些标题有空格。如果我们用slu as作为标签而不是标题,你会怎么想?或者,我们可以考虑使用像高级自定义字段或类型这样的wordpress扩展名来获取多个帖子之间的链接,而不使用tags字段。 – 2014-12-02 17:59:59

+0

Slug会做得很好。我也有ACF。如果可能的话,S would对我来说是最好的。 – JordanC26 2014-12-02 18:02:34

你需要得到后期slu so,所以首先我会阅读该评论,并确认你得到了slu。。尝试可能做一个var_dump($ slug);出口;紧接在slug变量之后,以验证您的值是否正确。一旦你这样做,试试这个镜头:

// Get the slug (This is assuming you 
// have the current post in a $post 
// variable. Otherwise, load the post 
// with the post ID like so: 
// $post = get_post($post_id); 
$slug = $post->post_name; 

// Build args array for query, replacing hyphens with nothing on the slug. 
$args = array('tag' => str_replace('-', '', $slug)); 

// Set wp_query with tag args 
$wp_query = new WP_Query($args); 

// If we get result posts from our query... 
if (have_posts()) :?> 
    <?php while (have_posts()) : the_post(); ?> 
     <div class="small-12 medium-6 columns content-excerpt"> 
      <div class="thumbnail medium-6 columns nopm"> 
       <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> 
      </div> 
      <div class="content"> 
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
       <p><?php //the_excerpt(); ?></p> 
       <div class="related-stats-info"> 
        <ul> 
         <!-- <li><?php //the_author(); ?></li> --> 
         <li><i class="fa fa-clock-o"></i> <?php the_date('Y-m-d') ?></li> 
         <li><i class="fa fa-comments"></i> <?php comments_number('0 comments', '1 comment', '% comments'); ?></li> 
        </ul> 
       </div> 
      </div> 
     </div> 
    <?php endwhile; ?> 
    <div class="clearboth"></div> 
<?php endif; ?> 
<?php 
// Reset wordpress... 
wp_reset_query(); 
wp_reset_postdata(); 
+0

是的,它肯定会得到正确的slu,,'rennicks-mts'就是这个例子。给标签'rennicks-mts'设置3条更多文章,但是没有任何内容返回到商品页面上。尝试在另一个列表页面上的标签也是一样的。嗯。 – JordanC26 2014-12-02 18:45:16

+0

这很奇怪,如果我迅速改变$塞一个静态的标签,只是一个字似乎工作。 rennicks的作品,但rennicks-mts即使不是静态的。 另外,如果我改变它使用POST_ID相反,它会忽略标签,并带来了最新的10个职位。 – JordanC26 2014-12-02 18:55:17

+0

我想知道在查询标签时连字符是否会引起问题?也许尝试用'rennicksmts'标记几个帖子并将其硬编码嵌入slu。中。如果这样的作品,我们可以尝试从蛞蝓删除连字符和走这条路线... – 2014-12-02 18:58:48