【问题标题】:Custom Taxonomy Archives templates自定义分类档案模板
【发布时间】:2015-04-13 13:51:51
【问题描述】:

我真的很想得到一些关于 Taxonoy 模板和 CPT 的帮助,我已经尝试解决这个问题一个星期了,我只是越来越困惑,我尝试的任何方法似乎都没有按我希望的方式工作?

任何人都可以解释或告诉我如何实现这一点,我考虑过花钱请人这样做,因为我不是编码员,我可以很高兴地编辑工作代码,但不能编写它。我虽然这会比这容易得多。

细节:

我有一个名为 - 'Machines'的自定义帖子类型

我有一个 分类法 附加到名为 - 'Machine-types'

下面是 3 种机器类型 - 'electrical' - 'mechanical' - 'solar'

我希望“machines”的存档显示“machine-types”下的术语列表以及指向该术语存档的链接。我不想显示帖子,只是将术语显示为列表。

我相信第一个文件是archive-machines.php

机器 (archive-machines.php) ?

  -electrical
  -mechanical
  -solar

当我关注其中一个术语链接时,我希望它显示该术语下的帖子列表,仅显示链接到它的“post-titlesingle.php(漂亮标准)

我无法解决的是,如何让 'machine-types' 下的所有术语使用相同的模板。我见过的大多数代码都需要您更改术语的名称,我需要它是动态的。

我的最终目标是使用一些自定义字段,但目前我只想让模板工作并从那里开始。

我觉得我的嘴已经写了我的大脑无法兑现的支票!任何帮助将不胜感激 ! :)

【问题讨论】:

    标签: php wordpress templates archive custom-taxonomy


    【解决方案1】:

    Wordpress 没有用于归档分类术语的标准模板,因此 archive-machines.php 模板对您没有任何好处。您最好创建一个自定义页面和模板,并在该模板中使用 get_terms() 函数来获取机器类型分类中的所有术语并输出您的列表。

    https://codex.wordpress.org/Function_Reference/get_terms

    此函数将返回一个术语对象数组。从中您可以获得 ID、slug、名称等。为了获得指向该术语的链接,您将使用 get_term_link() 函数。

    https://codex.wordpress.org/Function_Reference/get_term_link

    单击其中一个术语后,它将访问 taxonomy-machine-types.php 模板。这些分类模板设置为根据所选分类术语显示帖子列表。

    【讨论】:

    • 谢谢,几天前我涉足了那么多:)
    【解决方案2】:

    最后我使用了这两个模板,其中 machine-archive.php 使用了一些自定义字段。

    <?php
    
    /*
     * Custom WordPress Template: archive-machine
     */
    
    // Remove stuff
    
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    
    // Add our custom loop
    
    add_action( 'genesis_loop', 'machine_archive_loop' );
    
    function machine_archive_loop() {
    
            $libargs=array(  
                'hide_empty'    => 1,  
                'parent'        => 0,  
                'taxonomy'      => 'machine-type');  
    
                $libcats=get_categories($libargs);  
    
                foreach($libcats as $lc){ 
                    $termlink = get_term_link( $lc->slug, 'machine-type' ); 
    
            ?>
    
             <div class="row tax-list">
                <div class="small-12 medium-4 large-3 columns tax-img">
                   <a href="<?php echo $termlink; ?>"><img src="<?php the_field('tax_image' , 'machine-type_'.$lc->term_id); ?>" alt="view range"/></a>
                </div>
    
                <div class="small-12 medium-8 large-9 columns tax-description">
                   <h2><a  href="<?php echo $termlink; ?>"><?php echo $lc->name; ?> Range</a></h2>
                   <?php the_field('tax_description', 'machine-type_'.$lc->term_id); ?><a  href="<?php echo $termlink; ?>"> ....View the range</a>
                </div>
    
            </div>
    
            <?php } 
    
    
      // use reset postdata to restore orginal query
      wp_reset_postdata();
    
    } 
    
    
    
    genesis();
    

    这是taxonomy-machine-type.php

    <?php
    // Remove stuff
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    
    // Add our custom loop
    add_action( 'genesis_loop', 'tax_archive_loop' );
     function tax_archive_loop() {
    
      $taxonomy = 'machine-type';
      $queried_term = get_query_var($taxonomy);
      $terms = get_terms($taxonomy, 'slug='.$queried_term);
    
    
        // Define the query
       $args = array(
        'post_type' => 'machines',
        'machine-type' => $queried_term ,
        'posts_per_page' => -1,
     );
    
    // run the query 
        $query = new WP_Query( $args );
        if( $query->have_posts() ) { 
    
          // Start the Loop
            while ( $query->have_posts() ) : $query->the_post();  ?>
    
           <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
           <p><?php echo get_the_date(); ?></p>
             <div class="entry-content">   
               <?php echo the_content(); ?>
              <a href="<?php echo get_permalink(); ?>"> ... Find Out More</a>
            </div>
    
      <?php endwhile;?>
    
    <hr/>
    
    <?php 
    
    }        
        // use reset postdata to restore orginal query
        wp_reset_postdata();
    } 
    genesis();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多