2

I recently needed to change my permalink structure in Wordpress. However the change caused all my social sharing counters to reset. I found this great script which I modified below, http://encosia.com/preserving-social-sharing-counters-through-a-url-change/. I rewrote it so it would remove the %poste-id% at the end of the url on older posts.

The problem is that for the most part this works, however I need it to call the permalink category not the first category in the get_the_category(); array. Is there anyway I can pull this info, or modify this script to work that way? This is also being used in the loop. Thanks!!

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

     }
    ?>

    <?php echo $sharing_url; ?>
4

0 に答える 0