按分类循环访问定制帖子,并将标题显示在列表中,重复8次

时间:2015-12-30 作者:jfoutch

在互联网上搜索了数百篇帖子和抄本之后,我一直在处理这个问题,时间太长了。我觉得我几乎拥有了它,但我错过了一些东西。我对PHP的知识有限,这让我左右为难。任何帮助都将不胜感激。此外,如果有一种简单的方法可以将“active”类添加到锚中,如果显示相应的帖子,那将非常棒。谢谢

    <?php
/**
 * The template for displaying all single posts.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * 
 */


/* this page is a single custom post page -- single-peterbilt.php */

get_header(); ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <div class="row">
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                <div class="large-10 columns large-offset-1">
                <h2><?php the_title(); ?></h2>
                <div class="large-8 columns slider">
                    <div class="models text-center">
                        <?php if( has_term( \'peterbilt-389\', \'model\' ) ) :  ?>

                            <?php // terms for the model taxonomy 
                                $terms = get_terms( \'model\', array(
                                    \'orderby\'    => \'count\',
                                    \'hide_empty\' => 0
                                ) );
                                ?>

                                <?php

                                    foreach( $terms as $term ) {

                                        // Define the query
                                        $args = array(
                                            \'post_type\' => \'peterbilt\',
                                            \'tax_query\' => array(
                                                array(
                                                    \'taxonomy\' => \'model\',
                                                    \'field\' => \'slug\',
                                                    \'terms\' => \'peterbilt-389\'
                                                )
                                            )
                                        );
                                        $query = new WP_Query( $args );

                                        // output the post titles in a list
                                        echo \'<ul class="inline-list">\';

                                            // Start the Loop
                                            while ( $query->have_posts() ) : $query->the_post(); ?>

                                                <li>
                                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                </li>

                                            <?php endwhile;

                                        echo \'</ul>\';

                                        // use reset postdata to restore orginal query
                                        wp_reset_postdata();

                                    } ?>

                            <?php  else:  ?> 
                            <?php // terms for the model taxonomy
                                $terms = get_terms( \'model\', array(
                                    \'orderby\'    => \'count\',
                                    \'hide_empty\' => 0
                                ) );
                                ?>

                                <?php

                                    foreach( $terms as $term ) {

                                        // Define the query
                                        $args = array(
                                            \'post_type\' => \'peterbilt\',
                                            \'tax_query\' => array(
                                                array(
                                                    \'taxonomy\' => \'model\',
                                                    \'field\' => \'slug\',
                                                    \'terms\' => \'peterbilt-386\'
                                                )
                                            )
                                        );
                                        $query = new WP_Query( $args );

                                        // output the post titles in a list
                                        echo \'<ul class="inline-list">\';

                                            // Start the Loop
                                            while ( $query->have_posts() ) : $query->the_post(); ?>

                                                <li>
                                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                </li>

                                            <?php endwhile;

                                        echo \'</ul>\';

                                        // use reset postdata to restore orginal query
                                        wp_reset_postdata();

                                    } ?>
                        <?php   endif; ?>       
                    </div>
                    <div class="thumb-slider"><?php the_field(\'thumb_slider\'); ?></div>
                    <div class="single-description"><?php the_field(\'single_description\'); ?></div>
                </div>
                <div class="large-4 columns">
                    <div class="content">
                        <div class="spec-wrapper">
                            <h4>Specs</h4>
                            <?php the_field(\'specs\'); ?>
                        </div>
                    </div>          
                </div>
                </div>
                <?php endwhile; else : ?>

                    <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
                <?php endif; ?>
            </div>
        </main><!-- #main -->
    </div><!-- #primary -->

<?php
get_footer();

1 个回复
SO网友:s_ha_dum

您已将该术语硬编码到第二个查询中。当然,你每次都会看到同样的事情:

$terms = get_terms( 
  \'model\',
  array(
    \'orderby\'    => \'count\',
    \'hide_empty\' => 0
  ) 
);

if (!is_wp_error($terms)) {
  foreach( $terms as $term ) {
    // Define the query
    $args = array(
      \'post_type\' => \'peterbilt\',
      \'tax_query\' => array(
        array(
          \'taxonomy\' => \'model\',
          \'field\' => \'id\',
          \'terms\' => $term->term_id
        )
      )
    );
    $query = new WP_Query( $args );
    // more code
  }
}

相关推荐

Templates for Mobile Site

是否有任何内置方法可以根据浏览器大小显示不同的模板(即移动设备检测)?我做了一些研究,我能找到的只是大量插件,它们的功能远远超出了我的需要。我基本上只需要一种方法,将移动目录添加到我的主题中,并为移动用户显示该主题。