0

This is driving me crazy, so please help me guys with this.

I'm trying to add a metatag to custom taxonomy ter archive page header which contains the url of the term archive page of a custom taxonomy.

How to get the URL of the archive page when there's neither specified term nor the taxonomy itself.

  • 1
    If you want the URL of the archive page of a taxonomy term when there is no term nor taxonomy specified ...... you want the URL to nowhere. It is like if you want the URL of a post when no post is specified. Can you clarify what are you trying to do? – cybmeta Apr 20 '14 at 20:19
  • Yea, it sounds weird. I'm trying to add Open Graph meta tags to the header of Custom Taxonomy. I know that SEO by Yoast plugin can do this task & it adds URL to the Custom Tax. – Hardeep Asrani Apr 21 '14 at 8:58
1

If you are in term archive page, the taxonomy and term is actually set. You could hook wp_head to add the open graph in your functions.php:

 add_action( 'wp_head', 'wpse_wp_head' );
 function wpse_wp_head () {

      //First, we check if we are in our custom taxonomy
      if( is_tax('my-custom-taxonomy') ) {

           $taxonomy_slug = get_query_var( 'taxonomy' );
           $term_slug = get_query_var( 'term' );
           $term_url = get_term_link( $term_slug, $taxonomy_slug );

           //Add Open Graph property
           echo '<meta property="og:url" content="'. esc_url( $term_url ) .'">';

           //If you need the full object of current term
           //$term = get_term_by( 'slug', $term_slug, $taxonomy_slug );
           //Now you can get all the term data. For example:
           //$term->name, $term->term_id, $term->slug,.....

      }

 }
|improve this answer|||||
  • Sorry for troubling you, but I found my answer & posted it into this thread. – Hardeep Asrani Apr 21 '14 at 9:19
  • 1
    Sorry I think my answer is better. Your answer doesn't add any meta tag as you requested and doesn't display where to hook the code. Aslo you don't check if you actually is in the custom taxonomy term archive page and so on. – cybmeta Apr 21 '14 at 9:29
  • Maybe your code is better than the one I posted. However, that code works perfectly in my plugin. – Hardeep Asrani Apr 21 '14 at 10:35
  • And it checks if it's tax and stuff, but I just posted the main part of the code that I was looking for & rest is already in my plugin. – Hardeep Asrani Apr 21 '14 at 10:36
-3

I finally found the right answer for my question:

First add this code:

function get_tax_data($data){
    if(!$data)
        return;
    global $wp_query;
    $term = $wp_query->get_queried_object();

    if($data == 'title')
        return $term->name;

    if($data == 'description')
        return strip_tags($term->description);

    if($data == 'link') {
        $link = get_term_link($term);
        return $link;
    }
}

Now just add following to call the slug:

$the_tax = get_tax_data('link');

|improve this answer|||||

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.