0
votes

I need to use the same page for different taxonomies and terms. How can I retrieve the taxonomies and the terms from the URL and run the same page for those I that I want?

The query should be run through a URL since I am not using forms. When the user clicks on the link, a new page should be opened that is an archive of posts having custom post type, taxonomy and taxonomy terms specified in the URL.

This is the code that I have right now:

<? /*
* 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(); ?>

EDIT

After a few days, I found the solution that I am going to post, however it has got an issue: I cannot see the list of the posts if a post has got multiple terms (more than 1 term basically) selected by the checkboxes when it is created or edited.

This is my taxonomy.php file

<?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);
 } 

?>

And this is my page_activities.php file

<?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(); ?>

Pls help me with this because I am really clueless. One good news is that if I check the array with print_r($termsObjects[0]); I can see properly the output.

I guess I should use a foreach somehow, but I don't know where to put my hands on.

Thank you to anyone who can help me with this.

Regards

1

1 Answers

0
votes

..your question is a bit broad...but the gist of what you need to do:

to pull the variables from the url

$query  = explode('&', $_SERVER['QUERY_STRING']);
$params = array();

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

now you will have a params array i've no idea what your urls will look like, but say its ' postterm1 = italian, postterm2 = chinese, etc

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

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

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


}

this will build the query you see below, just delete 'tax_query' array and insert the var $array instead. It should work but i haven't tested it so play around with the format if needed.

your custom query (i think you want multi taxonomies but not all?) Use 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'
          )

      ), 

);

ref: http://codex.wordpress.org/Class_Reference/WP_Query