对多个分类法使用相同的页面

时间:2014-06-26 15:07:40

标签: php wordpress custom-taxonomy

我需要针对不同的分类和术语使用相同的页面。 如何从URL中检索分类法和术语,并为我想要的那些人运行相同的页面?

查询应该通过URL运行,因为我没有使用表单。 当用户点击链接时,应打开一个新页面,该页面是具有URL中指定的自定义帖子类型,分类和分类术语的帖子的存档。

这是我现在的代码:

<? /*
* Template Name: Activities template
* Description: Template for activties like restaurants, pubs, etc.
*/ 
?>

<?php $options = get_option('mh_options'); ?>
<?php get_header(); ?>

<?php 

if ( get_query_var('paged') ) {$paged = get_query_var('paged');}  
if ( get_query_var('page') )  {$paged = get_query_var('page');}

$args = array(
'post_type' => 'activties',
'tax_query' => array(
    array(
        'taxonomy' => 'restaurants',
        'field' => 'slug',
        'terms' => 'italian'
    ),
'paged' => $paged
)
);
query_posts( $args ); ?>

<div class="wrapper clearfix">
<div class="main">
    <div class="content <?php mh_content_class(); ?>">
        <?php mh_before_page_content(); ?>  

                <?php dynamic_sidebar('pages-1'); ?>

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


            <!--    <?php the_content(); ?> -->             
                <article <?php post_class(); ?>>    
                    <div class="loop-wrap loop-layout2">
                        <div class="clearfix">
                            <div class="loop-thumb">
            <a href="<?php the_permalink(); ?>">
                                                                                                          <?php if( get_field('business_logo') ): ?>

                                <?php $image = wp_get_attachment_image_src(get_field('business_logo'), 'loop'); ?>

                                        <img src="<?php echo $image[0]; ?>" alt="<?php  the_field('business_logo');?>" data-thumb="<?php echo $thumb[0]; ?>" />



<?php else: echo '<img src="' . get_template_directory_uri() . '/images/noimage_300x225.png' . '" alt="No Picture" />';  ?>

                                    <?php endif; ?>
                                </a>
                            </div>
<div class="loop-content">
                                <header>
<h3 class="loop-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
                                </header>   

<?php if (get_field('business_description')): ?>    
                                <?php $text_to_trim = get_field('business_description');
                                    echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
                                <?php endif; ?>
                                <?php if (get_field('business_paying_desc')): ?>    

<?php $text_to_trim = get_field('business_paying_desc');  
                                    echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
                                <?php endif; ?> 
                            </div>
                        </div>
                    </div>
                </article>  

        <?php dynamic_sidebar('pages-2'); ?>

        <?php endwhile; ?>
    <!-- <?php wp_reset_postdata(); ?> -->
        <?php wp_reset_query(); ?>

<?php if (isset($options['comments_pages']) ?     $options['comments_pages'] : false) :     ?>
            <section>
                <?php comments_template(); ?>
            </section>
            <?php endif; ?>
        <?php endif; ?>
    </div>
    <?php get_sidebar(); ?>
</div>
<?php mh_second_sb(); ?>        
</div>
<?php get_footer(); ?>

修改

几天之后,我找到了我要发布的解决方案,但它有一个问题:如果帖子有多个术语(基本上超过1个术语)被选中,我看不到帖子列表创建或编辑时的复选框。

这是我的taxonomy.php文件

<?php  
$post = $wp_query->post;  

/*$catMerc = array('restaurants','pubs', 'the-pizzerias', 'bars', 'cafes', 'nightlife-clubs', 'shopping', 'the-coffeeshops');*/


$catMerc = array('the-restaurants', 'the-coffeeshops', 'the-pizzerias', 'shopping', 'nightlife-clubs', 'cafes', 'bars', 'pubs');



$termsObjects = wp_get_object_terms($post->ID, $catMerc);

//Assuming your post only has one category, if theres more it will return multiple objects in it's return array:

$currentCustomCat = $termsObjects[0]->slug;

$currentCatMerc = get_query_var('taxonomy');


//you can have 'name' instead of 'slug' if that helps too 

$customcatarray = array('american-pubs-exclusive5', 'american-pubs-in-town', 'beer-houses-exclusive5', 'beer-houses-in-town', 'free-joints-exclusive5', 'free-joints-in-town', 'local-atmosphere-exclusive5', 'local-atmosphere-in-town', 'spanish-atmosphere-exclusive5', 'spanish-atmosphere-in-town', 'take-away-exclusive5', 'take-away-in-town', 'traditional-dutch-exclusive5', 'traditional-dutch-in-town', 'african-exclusive-5', 'african-in-town', 'argentinian-restaurants-exclusive5', 'argentinian-restaurants-in-town',' asian-restaurants-exclusive5', 'asian-restaurants-in-town', 'dutch-restaurants-exclusive5', 'dutch-restaurants-in-town', 'french-restaurants-exclusive5', 'french-restaurants-in-town', 'italian-restaurants-exclusive5', 'italian-restaurants-in-town', 'seafood-restaurants-exclusive5', 'seafood-restaurants-in-town', 'spanish-restaurants-exclusive5', 'spanish-restaurants-in-town', 'cocktail-bars-exclusive5', 'cocktail-bars-in-town', 'disco-bars-exclusive5', 'disco-bars-in-town', 'dutch-bars-exclusive5', 'dutch-bars-in-town', 'internet-cafes-exclusive5', 'internet-cafes-in-town', 'lounge-bars-exclusive5', 'lounge-bars-in-town', 'art-cafes-exclusive5', 'art-cafes-in-town', 'breakfast-lunch-exclusive5','breakfast-lunch-in-town', 'famous-cafes-exclusive5', 'famous-cafes-in-town', 'fashion-cafes-exclusive5', 'fashion-cafes-in-town', 'timeout-exclusive5', 'timeout-in-town', 'best-boutiques-exclusive5','best-boutiques-in-town', 'famous-brands-exclusive5', 'famous-brands-in-town', 'sportswear-exclusive5', 'sportswear-in-town', 'the-pizzerias-with-table-service-in-town', 'the-pizzerias-with-table-service-exclusive5', 'the-pizzerias-takeway-in-town', 'the-pizzerias-takeaway-exclusive5', 'the-coffeeshops-in-town', 'the-coffeeshops-exclusive5'); 


if (in_array($currentCatMerc, $catMerc)  && in_array($currentCustomCat, $customcatarray) ) {include(TEMPLATEPATH.'/page_activities.php'); } 



/*if( $currentCustomCat == "italian" || $currentCustomCat == "local-atmosphere"){
   //It matched, do stuff here
    {include(TEMPLATEPATH.'/single_activities.php'); } 
}*/

else  { /*include(TEMPLATEPATH.'/page.php'); */

    $pagelink=get_page_link (get_page_by_title( 'Homepage' ));
    header("Location: $pagelink",TRUE,301);
 } 

?>

这是我的page_activities.php文件

    
<?php   


if ( get_query_var('paged') ) {$paged = get_query_var('paged');}  
if ( get_query_var('page') )  {$paged = get_query_var('page');}


$args = array(
    'post_type' => 'activities',
    'tax_query' => array(

        array(
            'taxonomy' => $currentCatMerc,
            'field' => 'slug',
            'terms' => $currentCustomCat
        ),
    'paged' => $paged,
    'posts_per_page'=>'10',

    )
);

$args2 = array(
    'post_type' => 'activities',
    'paged' => $paged
);

query_posts( $args ); ?>

<div class="wrapper clearfix">
    <div class="main">
        <div class="content <?php mh_content_class(); ?>">
            <?php mh_before_page_content(); ?>  

                    <?php dynamic_sidebar('pages-1'); ?>

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


                <!--    <?php the_content(); ?> -->             
                    <article <?php post_class(); ?>>    
                        <div class="loop-wrap loop-layout2">
                            <div class="clearfix">
                                <div class="loop-thumb">
                                    <a href="<?php the_permalink(); ?>">


                                <?php if( get_field('business_logo') ): ?>

                                    <?php $image = wp_get_attachment_image_src(get_field('business_logo'), 'loop'); ?>

                                            <img src="<?php echo $image[0]; ?>" alt="<?php  the_field('business_logo');?>" data-thumb="<?php echo $thumb[0]; ?>" />



                                        <?php else: echo '<img src="' . get_template_directory_uri() . '/images/noimage_300x225.png' . '" alt="No Picture" />';  ?>

                                        <?php endif; ?>
                                    </a>
                                </div>
                                <div class="loop-content">
                                    <header>
                                        <h3 class="loop-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
                                    </header>   


                                    <?php if (get_field('business_description')): ?>    
                                    <?php $text_to_trim = get_field('business_description');
                                        echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
                                    <?php endif; ?>
                                    <?php if (get_field('business_paying_desc')): ?>    

                                    <?php $text_to_trim = get_field('business_paying_desc');  
                                        echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
                                    <?php endif; ?> 
                                </div>
                            </div>
                        </div>
                    </article>  

            <?php dynamic_sidebar('pages-2'); ?>

            <?php endwhile; ?>
        <!-- <?php wp_reset_postdata(); ?> -->
            <?php wp_reset_query(); ?>
            <?php endif; ?>


            <div class="sb-widget home-2 home-wide">
            <h4 class="widget-title">Latest Updates</h4>
            <ul class="cp-widget clearfix">

            <?php query_posts( $args2 ); ?>

            <?php if (have_posts()) : $i=1; while (have_posts() && ($i<=10)) : the_post(); ?>

              <li class="cp-wrap cp-small clearfix">
                <div class="cp-thumb">

                <a href="<?php the_permalink(); ?>">
                    <?php if( get_field('business_logo') ): ?>
                    <?php $image = wp_get_attachment_image_src(get_field('business_logo'), 'cp_small'); ?>
                    <img width="70" height="53" src="<?php echo $image[0]; ?>" alt="<?php  the_field('business_logo');?>" data-thumb="<?php echo $thumb[0]; ?>" class="attachment-cp_small wp-post-image" />
                    <?php endif; ?>
                </a></div>

                <div class="cp-data">
                    <p class="cp-widget-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
                </div>                  
                </li>   

            <?php $i++; ?>
                <?php endwhile; ?> 
                <?php wp_reset_query(); ?> 
                <?php endif;?>

            </ul>
            </div>
            <div class="sb-widget home-2 home-wide">
                <?php echo do_shortcode('[rev_slider 620_100]'); ?>
            </div> 




                <?php if (isset($options['comments_pages']) ? $options['comments_pages'] : false) : ?>
                <section>
                    <?php comments_template(); ?>
                </section>
                <?php endif; ?>
        </div>
        <?php get_sidebar(); ?>
    </div>
    <?php mh_second_sb(); ?>        
</div>
<?php get_footer(); ?>

请帮助我,因为我真的很无能为力。 一个好消息是,如果我用print_r($ termsObjects [0])检查数组;我可以正确看到输出。

我想我应该以某种方式使用foreach,但我不知道该把手放在哪里。

感谢任何可以帮助我的人。

此致

1 个答案:

答案 0 :(得分:0)

..你的问题有点广泛......但你需要做的是:

从网址

中提取变量
$query  = explode('&', $_SERVER['QUERY_STRING']);
$params = array();

foreach( $query as $param ){
list($name, $value) = explode('=', $param);
$params[urldecode($name)][] = urldecode($value);
}

现在你将有一个params阵列我不知道你的网址会是什么样子,但是说它的&#39; postterm1 =意大利语,postterm2 =中文等

$array['taxquery'] = array();
$array['taxquery'][relation]= 'OR';

foreach ($param as $key=>$value) {
   $array['taxquery'][]=array(

          'taxonomy' => 'restaurants',
          'field' => 'slug',
          'terms' => $value
   );


}

这将构建您在下面看到的查询,只需删除&#39; tax_query&#39;数组并插入var $数组。它应该可以工作,但我还没有对其进行测试,所以如果需要,可以使用该格式。

您的自定义查询(我想您想要多分类但不是全部?) 使用WP_query:

$args= array(
    'post_type' => 'activties',
    'tax_query' => array( 
        'relation'=>'OR',
          array(
          'taxonomy' => 'restaurants',
          'field' => 'slug',
          'terms' => 'italian'
          ),

          array(
          'taxonomy' => 'restaurants',
          'field' => 'slug',
          'terms' => 'chinese'
          )

      ), 

);

参考:http://codex.wordpress.org/Class_Reference/WP_Query