WordPress.org

Ready to get started?Download WordPress

Forums

Permalink Category (1 post)

  1. orbital318
    Member
    Posted 1 year ago #

    I am using get_the_category() within the loop. I am looking to get the category that is called in the permalink. For some reason this is not always the first category in the array and it's different all the time. The code below is a reworking of a script to compensate for SSO loss when migrating permalinks.

    Thanks!

    <?php
      // Changed URL structure from /postname.html to /postname-id.html after
      //  post 68135. This ensures older posts still present the old URLs
      //  to sharing services so that share counts are maintained.
      $url_change_id = 68135;
      $postid = $post->ID;
      $category = get_the_category();
      $slug = $post->post_name;
      $permalink_url = get_permalink();
    
     if (intval($postid) < $url_change_id) {
      	$url_date_prefix = "/" . $category[0]->category_nicename .
                           "/" . $slug .
                           ".html";
    
        $sharing_url = str_replace($permalink_url,
                                   "http://website.com" . $url_date_prefix,
                                   $permalink_url);						   
    
      } else {
    	  $sharing_url = get_permalink();
    
     }
    
      // At this point, $sharing_url is the correct URL to present
      //  to the social sharing APIs in order to maintain counters.
    ?>
    
    <?php echo $sharing_url; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic