3

I have a custom post type and custom taxonomy. The taxonomy works like a category system. Users can add the custom posts to the categories. There is one page per each category, displaying the posts in that category.

I need to get the page slug of the current category page. How can I do this please?

For example, my url is: ?event-categories=day-courses.

event-categories is the custom taxonomy.

This code would work if I only had 'day-courses' in this custom taxonomy, which I do not:

<?php
 global $post;
    $post_slug=$post->post_name;
    echo $post_slug; 

$my_query = new WP_Query( array( 'post_type' => 'event',  'event-categories' => 'day-courses' ) );
while ($my_query->have_posts()) : $my_query->the_post();
?>
<div>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="excerpt"><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
  • Take a look at get_query_var($custom_taxonomy_slug); codex.wordpress.org/Function_Reference/get_query_var – gdaniel Jan 16 '15 at 17:31
  • What do you mean by the page slug? If the page is for a category, it'd have multiple posts on there. Are you looking for the taxonomy term slug? – Manny Fleurmond Jan 16 '15 at 17:40
  • @MannyFleurmond Yes, that's it! – Leona grey Jan 16 '15 at 17:41
  • Is there any reason you are creating a custom query? – Manny Fleurmond Jan 16 '15 at 17:44
  • @MannyFleurmond Yes, my custom taxonomy acts like a category system, with posts from my custom post type associated with it. I want to display all the posts associated with each 'category' on separate pages – Leona grey Jan 16 '15 at 17:46
3

Using your current code:

$tax_slug = get_query_var( 'event-categories' );
  • Thank-you, that's what I think I need!I have put $tax_slug in $my_query = new WP_Query( array( 'post_type' => 'event', 'event-categories' => '$tax_slug' ) ); but it doesn't seem to work? Any ideas please? – Leona grey Jan 16 '15 at 17:56
  • See my new answer and use that instead – Manny Fleurmond Jan 16 '15 at 17:58
  • Thank-you Manny, you are my hero right now. Really up against time, and this is the 1st website I have worked on. Have a good day/evening! – Leona grey Jan 16 '15 at 18:00
  • I recommend that you dome some research into template file hierarchy: codex.wordpress.org/… . Although my answer worked, how you are doing your template would be easier if you let WordPress handle the taxonomy stuff and use the main loop. – Manny Fleurmond Jan 16 '15 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.