【问题标题】:Wordpress display specific taxonomy term on custom taxonomy template pageWordpress 在自定义分类模板页面上显示特定分类术语
【发布时间】:2013-11-17 06:05:47
【问题描述】:

我无法解决这个问题。

我需要完成的工作:this page 上,我只需要在“新发展”的自定义分类下显示标有“Doral”一词的项目的结果,这是自定义帖子类型“列表”的一部分。

当前页面显示所有项目,无论自定义帖子类型“列表”的术语或分类如何。

我目前拥有的: 我的自定义分类创建代码

add_action('init', 'property_new_dev_taxonomies');
function property_new_dev_taxonomies() {
register_taxonomy('new-developments',
        'listing',
        array (
        'labels' => array (
                'name' => 'New Developments',
                'singluar_name' => 'New Developments',
                'search_items' => 'Search New Developments',
                'popular_items' => 'Popular New Developments',
                'all_items' => 'All New Developments',
                'parent_item' => 'Parent New Development',
                'parent_item_colon' => 'Parent New Development:',
                'edit_item' => 'Edit New Development',
                'update_item' => 'Update New Development',
                'add_new_item' => 'Add New Development',
                'new_item_name' => 'New Developments',
        ),
                'hierarchical' => true,
                'show_ui' => true,
                'show_tagcloud' => true,
                'rewrite' => array( 'slug' => 'new-developments'),
                'query_var' => 'new-developments',
                'public'=>true)
        );
}

我的自定义分类模板 (taxonomy-new-developments.php)

<?php 

/*
    Template Name: Taxonomy New Developments
*/ 

?>


<?php get_header() ?>


<div id="columnswrapper" class="columns-1">
<?php get_sidebar(); ?>
    <div id="content" class="norightsidebar">
        <div class="inner">

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <?php the_content(); ?>
        <?php endwhile; endif; ?>

    <?php $term = $wp_query->queried_object; echo '<h2>'.$term->name.'</h2>'; ?>

    <?php $term = new WP_Query('post_type=listing', 'taxonomy=new-developments', 'field=slug',  'terms=doral'); ?>

    <?php if ($term->have_posts()) : while ($term->have_posts()) : $term->the_post(); ?>
    <?php include 'includes/variables.php' ?>


    <div class="searchresult">

        <?php 
        $arr_sliderimages = get_gallery_images();   
        //$firstimage = $arr_sliderimages[0];
        //$arr_sliderimages = parse_url($firstimage);
        $resized = aq_resize($arr_sliderimages[0], 250, 185, true);
        ?>  

        <div class="resultimage">


            <?php include 'includes/bannerssmall.php'; ?>


            <?php if ($mlslisting == "Yes" && $mlsnumber) { ?>
                <a href="<?php bloginfo('url') ?>/idx/mls-<?php echo $mlsnumber ?>-"><img width="250" height="185" alt="Image for <?php the_title(); ?>" src="<?php echo $resized ?>" /></a>
            <?php } else { ?>
                <a href="<?php the_permalink(); ?>"><img width="250" height="185" alt="Image for <?php the_title(); ?>" src="<?php echo $resized ?>" /></a>
            <?php } ?>  


        </div>

        <div class="resultdetails">
            <div class="primary-data">
                <div class="address">

                <?php if ($mlslisting == "Yes" && $mlsnumber) { ?>
                    <a href="<?php bloginfo('url') ?>/idx/mls-<?php echo $mlsnumber ?>-"><?php the_title() ?>, <?php echo $citystatezip ?></a>
                 <?php } else { ?>
            <a href="<?php the_permalink() ?>"><?php the_title() ?>, <?php echo $citystatezip ?></a>
            <?php } ?>



                </div>
                <div class="price">
                    <?php include 'includes/price.php'; ?>
                </div>
            </div>


            <div class="secondary-data">
                <?php if ($beds) { ?>
                    <div><?php echo $beds ?> <?php echo get_option('wp_bedrooms_text'); ?>, <?php echo $baths; ?> <?php echo get_option('wp_bathrooms_text'); ?></div>
                <?php } ?>
                <?php if ($homesize) { ?>
                    <div><?php echo get_option('wp_homesize_text') ?>: <?php echo $homesize ?></div>
                <?php } ?>
                <?php if ($lotsize) { ?>
                    <div><?php echo get_option('wp_lotsize_text') ?>: <?php echo $lotsize ?></div>
                <?php } ?>
                <?php if ($year) { ?>
                    <div><?php echo get_option('wp_yearbuilt_text') ?>: <?php echo $year ?></div>
                <?php } ?>
            </div>  

            <a class="button" href="<?php the_permalink(); ?>"><?php echo get_option('wp_read_more_text') ?></a>


        </div><!-- end resultdetails -->
    </div><!-- end searchresult -->

        <?php endwhile; else: ?>
        <p><strong>There are no items to display.  Make sure you add 1 or more posts using the "Listings" category.  Also, in Theme Options, make sure the slideshow is using the "Listings" category.</strong></p>

        <?php endif; 
        wp_reset_query(); ?> 

    </div><!-- end inner -->
    </div><!-- end content -->
</div><!-- end columnswrapper -->


<?php get_footer(); ?>

我一直在努力让自己变得太久,并且迫切需要一些帮助来完成这项工作。

提前感谢您的帮助。

========================================= 编辑:基于 cmets 进行了尝试,但仍然无法正常工作 -

<?php 

/*
    Template Name: Taxonomy New Developments
*/ 

?>


<?php get_header() ?>


<div id="columnswrapper" class="columns-1">
<?php get_sidebar(); ?>
    <div id="content" class="norightsidebar">
        <div class="inner">

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <?php the_content(); ?>
        <?php endwhile; endif; ?>

    <?php $term = $wp_query->queried_object; echo '<h2>'.$term->name.'</h2>'; ?>

    <?php $term = new WP_Query('post_type=listing', 'taxonomy=new-developments', 'field=slug',  'terms=doral'); ?>

    <?php if ($term->have_posts()) :
    $args = array(
                'post_type' => 'Listing',
                'new-developments' => $term->slug
            );
     query_posts($args);
     while ($term->have_posts()) : $term->the_post(); ?>
    <?php include 'includes/variables.php' ?>


    <div class="searchresult">

        <?php 
        $arr_sliderimages = get_gallery_images();   
        //$firstimage = $arr_sliderimages[0];
        //$arr_sliderimages = parse_url($firstimage);
        $resized = aq_resize($arr_sliderimages[0], 250, 185, true);
        ?>  

        <div class="resultimage">


            <?php include 'includes/bannerssmall.php'; ?>


            <?php if ($mlslisting == "Yes" && $mlsnumber) { ?>
                <a href="<?php bloginfo('url') ?>/idx/mls-<?php echo $mlsnumber ?>-"><img width="250" height="185" alt="Image for <?php the_title(); ?>" src="<?php echo $resized ?>" /></a>
            <?php } else { ?>
                <a href="<?php the_permalink(); ?>"><img width="250" height="185" alt="Image for <?php the_title(); ?>" src="<?php echo $resized ?>" /></a>
            <?php } ?>  


        </div>

        <div class="resultdetails">
            <div class="primary-data">
                <div class="address">

                <?php if ($mlslisting == "Yes" && $mlsnumber) { ?>
                    <a href="<?php bloginfo('url') ?>/idx/mls-<?php echo $mlsnumber ?>-"><?php the_title() ?>, <?php echo $citystatezip ?></a>
                 <?php } else { ?>
            <a href="<?php the_permalink() ?>"><?php the_title() ?>, <?php echo $citystatezip ?></a>
            <?php } ?>



                </div>
                <div class="price">
                    <?php include 'includes/price.php'; ?>
                </div>
            </div>


            <div class="secondary-data">
                <?php if ($beds) { ?>
                    <div><?php echo $beds ?> <?php echo get_option('wp_bedrooms_text'); ?>, <?php echo $baths; ?> <?php echo get_option('wp_bathrooms_text'); ?></div>
                <?php } ?>
                <?php if ($homesize) { ?>
                    <div><?php echo get_option('wp_homesize_text') ?>: <?php echo $homesize ?></div>
                <?php } ?>
                <?php if ($lotsize) { ?>
                    <div><?php echo get_option('wp_lotsize_text') ?>: <?php echo $lotsize ?></div>
                <?php } ?>
                <?php if ($year) { ?>
                    <div><?php echo get_option('wp_yearbuilt_text') ?>: <?php echo $year ?></div>
                <?php } ?>
            </div>  

            <a class="button" href="<?php the_permalink(); ?>"><?php echo get_option('wp_read_more_text') ?></a>


        </div><!-- end resultdetails -->
    </div><!-- end searchresult -->

        <?php endwhile; else: ?>
        <p><strong>There are no items to display.  Make sure you add 1 or more posts using the "Listings" category.  Also, in Theme Options, make sure the slideshow is using the "Listings" category.</strong></p>

        <?php endif; 
        wp_reset_query(); ?> 

    </div><!-- end inner -->
    </div><!-- end content -->
</div><!-- end columnswrapper -->


<?php get_footer(); ?>

【问题讨论】:

  • 下面的答案应该适合你
  • @boom_Shiva 刚试了下还是不行,是不是我搞错了?请参阅上面的编辑以了解我的代码更新。
  • $term->slug 应替换为“Doral”
  • @boom_Shiva 并不能真正帮助保持这些动态,每次客户创建一个新术语时,我都必须为每个术语创建一个模板。长期维护不好解决,有没有办法根据页面的url动态填充term字段?

标签: php wordpress taxonomy


【解决方案1】:

需要在have_posts的while循环之前传递下面的代码

$args = array(
                'post_type' => 'Listing',
                'new-developments' => $term->slug
            );
     query_posts($args);
      if (have_posts()) : 
        while (have_posts()) : 
              the_post();
 endwhile;
endif;

【讨论】:

  • 我根据您的评论更新了我的代码如下但没有任何变化,是我做错了地方吗? &lt;?php if ($term-&gt;have_posts()) : $args = array( 'post_type' =&gt; 'Listing', 'new-developments' =&gt; $term-&gt;slug ); query_posts($args); while ($term-&gt;have_posts()) : $term-&gt;the_post(); ?&gt;
  • 是的,您需要编写上面更新的代码,并将 $term->slug 替换为您的术语的 slug?
  • 有没有办法根据页面的url来动态slug?这些术语将不断更新,为了保持这种方法,我必须为每个术语创建一个新模板才能显示。对我的客户来说不是一个好的解决方案,因为他们无法创建自己的模板。
  • 另外,我确实尝试了更改 $term->doral 的代码,但没有发生任何变化我仍然得到 post_type 'Listing' 的所有结果
  • 您只需要传递术语名称,例如 'new-developments' => 'Doral'
【解决方案2】:

您必须使用 get_query_var http://codex.wordpress.org/Function_Reference/get_query_var 来获取 new_development 下的当前类别,然后您可以查询该类别的帖子。

  $current_cat = get_query_var('new-developments');
  $args = array(
    'post_type' => 'Listing',
    'new-developments' => $current_cat
  );
 query_posts($args);
 if (have_posts()) : 
    while (have_posts()) : 
          the_post();
    endwhile;
 endif;

【讨论】:

  • 这并没有改变任何东西。我发现的是我拥有的这行代码:` ` 控制着一切。如果我删除它,无论我在它之后放置什么其他代码,我都不会在页面上得到任何结果,所以逻辑告诉我这是需要修改的,但我不确定如何修改。有什么想法吗?
  • 我刚刚更新了代码,$term->have_posts() 仅在我使用查询帖子时替换为 have_posts,您能否回显并检查您是否在 $current_cat 中得到任何东西
猜你喜欢
  • 1970-01-01
  • 2012-07-11
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多