首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对多个分类使用相同的页面

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

Stack Overflow用户
提问于 2014-06-26 23:07:40
回答 1查看 710关注 0票数 0

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

查询应该通过URL运行,因为我没有使用表单。当用户单击该链接时,应该会打开一个新页面,其中包含在URL中指定的自定义帖子类型、分类法和分类法术语。

这是我现在拥有的代码:

代码语言:javascript
复制
<? /*
* 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文件

代码语言:javascript
复制
<?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文件

代码语言:javascript
复制
<?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)检查数组,我可以正确地看到输出。

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

感谢任何能帮助我的人。

问候

EN

回答 1

Stack Overflow用户

发布于 2014-06-27 09:05:20

这个问题有点像broad...but你需要做的事情的要点:

从url中提取变量

代码语言:javascript
复制
$query  = explode('&', $_SERVER['QUERY_STRING']);
$params = array();

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

现在你将有一个参数数组我不知道你的urls会是什么样子,但是说它是‘postterm1 =意大利语,postterm2 =中文,等等

代码语言:javascript
复制
$array['taxquery'] = array();
$array['taxquery'][relation]= 'OR';

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

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


}

这将构建您在下面看到的查询,只需删除'tax_query‘数组并插入var $array即可。它应该可以工作,但是我还没有测试它,所以如果需要的话,可以尝试一下它的格式。

您的自定义查询(我认为您需要多个分类法,但不是所有分类法?)使用WP_query:

代码语言:javascript
复制
$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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24434091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档