如何使用/创建插件模板显示自定义分类和术语

时间:2013-10-16 作者:gstricklind

我正在开发一个插件,它需要自定义分类法和3个术语(个人、组和宠物)。我已经用术语和一些示例帖子建立了分类法。我已经为我的自定义分类法创建了一个模板,该模板当前显示每个类别(术语)的相同输出。我需要输出只显示当前选定期限内的帖子。因此,如果从example.com/user_image_category/group/ - 它应该只显示“组”的帖子。

Please note 我正在尝试在不接触主题文件夹的情况下执行此操作。如果代码有点。。。至少可以说是恶心。我已经拼凑了一些我发现的东西,但已经修复了很多——我通常处理主题和小插件。

插件功能:

add_filter(\'template_include\', \'sui_user_image_cat_template\', 99);
             custom taxonomy / category
function sui_user_image_cat_template( $template ) {
    if ( is_tax(\'sui_image_category\') ) {
        $new_template = plugin_dir_path(__FILE__) . \'templates/taxonomy-user_image_category.php\';
        if ( \'\' != $new_template ) {
            return $new_template;
        }
    }
    return $template;
}
taxonomy-user_image_category.php:

<?php
/**
 * user_images_category taxonomy archive
 */

get_header(); 

?>

<div id="container">
  <div id="content" role="main">

<h2>Contest Photos: </h2><br />


<div id="category-navigation">
  <?php
    // put these in UL LI format if time allows

    $terms = get_terms(\'sui_image_category\');
    foreach ($terms as $term) {
      $term_link = get_term_link($term, \'user_images\');
      if( is_wp_error($term_link) )
        continue;
      echo \'<span class="category-link"><a href="\' . $term_link . \'">\' . $term->name . \'</a></span> \';
    }
  ?>
<br /><br />
</div>



<div id="gridContainer">
<?php
$c = 1; //init counter
$bpr = 3; //boxes per row


  $args = array(
    \'post_type\' => \'user_images\',
    \'post_status\' => \'publish\'    
  );

  $posts = new WP_Query($args);  

  if($posts->post_count){

      foreach($posts->posts as $user_image){
        $user_info = get_userdata($user_image->post_author);    
        $user_image_cats = get_the_terms($user_image->ID, \'sui_image_category\');

        foreach($user_image_cats as $cat){
          $user_image_cat = $cat->name;
        }

        $post_thumbnail_id = get_post_thumbnail_id($user_image->ID);

  ?>
      <div class="contest-entry" id="post-<?php echo $post_thumbnail_id ?>">
        <div class="contest-image">
          <?php echo wp_get_attachment_link($post_thumbnail_id, \'medium\'); ?>
        </div>
        <div class="row">
          <div class="votes half left-align">&hearts;</div>
          <div class="category half right-align">
            <a href="<?php echo get_term_link($cat) ?>">
              <?php echo $user_image_cat ?>
            </a>
          </div>
        </div>
        <p class="image-caption"><?php echo $user_image->post_title ?></p>
        <a class="author" href="#"><?php echo $user_info->user_login ?></a>
      </div>

      <?php if($c == $bpr) : ?>

        <div class="clr"></div>

      <?php $c = 0;
        endif;
      ?>
    <?php
      $c++;
    }  
  };

?>
<div class="clr"></div>
</div>

  </div><!-- #content -->

</div><!-- #container -->

<?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:Dero 整理而成

taxonomy-user_image_category.php 这个get_queried_object 方法将返回术语对象,您可以将其合并到WP_Query. 从我的头顶上看,在你的情况下,可能是这样的:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

$args = array(
  \'post_type\' => \'user_images\',
  \'post_status\' => \'publish\',
  \'tax_query\' => array(
      array(
          \'taxonomy\' => \'sui_image_category\',
          \'field\' => \'id\',
          \'terms\' => $term_id
      )
   )
);

$posts = new WP_Query($args);

结束

相关推荐

Show terms in archive page

我试图在存档页上显示特定帖子类型的术语。我不明白如何“呼应”术语名称。这是我的代码:<?php $terms = get_terms(\"event_cat\"); ?> <?php while (have_posts()) : the_post(); ?> print term->name here <?php endwhile; ?>