WordPress.org

Ready to get started?Download WordPress

Forums

Get archive page's slug/name (3 posts)

  1. acornrevolution
    Member
    Posted 10 months ago #

    I use a lot of custom sidebars, which require looking at what category is being displayed and then returning certain content. Currently, I am using the following:

    <?php
    $category = get_the_category($post->ID);
    $incname = $category[0]->category_nicename;
    include(TEMPLATEPATH.'/zList'.ucfirst($incname).'.php');
    ?>

    However, this requires looking at the actual posts and not the category slug. This causes problems sometimes. I found the get_terms_by function and wrote the following, which does not seem to work:

    <?php
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    $incname = $term->slug;
    include(TEMPLATEPATH.'/zList'.ucfirst($incname).'.php');
    ?>

    How can I get this to work? For both categories, tags, and custom taxonomies?

    Thanks!

  2. parcodeisuoni
    Member
    Posted 10 months ago #

    Hi, I would like to try to help you, but I don't understand what you mean for

    I use a lot of custom sidebars, which require looking at what category is being displayed and then returning certain content.

    also, where are you using `<?php
    $category = get_the_category($post->ID);
    $incname = $category[0]->category_nicename;
    include(TEMPLATEPATH.'/zList'.ucfirst($incname).'.php');
    ?>` I mean is it a page template, or inside some other template?

    anyway, if you use it outside a loop, you should call first $post as global, so your code could be

    <?php
    global $post;
    $category = get_the_category($post->ID);
    $incname = $category[0]->category_nicename;
    include(TEMPLATEPATH.'/zList'.ucfirst($incname).'.php');
    ?>

    I don't know if I got well your question,
    hope this helps

  3. acornrevolution
    Member
    Posted 10 months ago #

    I'm doing all this in sidebar.php. My first code works. I want to modify it so that it doesn't need the $post->ID, and so that it works with tags too. Basically, I want this to work:

    <?php
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    $incname = $term->slug;
    include(TEMPLATEPATH.'/zList'.ucfirst($incname).'.php');
    ?>

    I don't need to call global $post

Reply

You must log in to post.

About this Topic