WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Build a URL for social sharing based on post-slug (not permalink) & publish date (3 posts)

  1. demawo
    Member
    Posted 7 months ago #

    Hi. First, big thanks to people on this forum. I've learnt a lot over the years. Hoping someone can help me out here. I'm pretty terrible at PHP and am getting stuck trying to make this piece of code...

    I'm trying to put together some code that: if the post was published before Nov 1, 2013 takes my domain name + the post-slug and puts it into $sharing_url - else: (i.e. the post was published after Nov 1, 2013, then use the normal permalink in $sharing_url. I then plan to use <?php echo $sharing_url; ?> to populate the URL in social share buttons (e.g. Tweet this post). Reason behind this? I'm redesigning a site with 100+ posts, in the process changing the permalinks from /%postname%/ to /%category%/%postname% and I want to hold on to my social share counts on those older posts.

    Two problems: 1/ I can't get it to output http://japantravelmate.com/ + the post slug, all that's coming out is http://japantravelmate.com/. 2/ $url_change_date = strtotime doesn't seem to be working, $sharing_url is always putting out http://japantravelmate.com/ (but if the post was published after 1/11/2013, it should just show the permalink).

    <!-- START -->
    <?php
    // Changed URL structure from /%postname%/ to /%category%/%postname% on
    // 1 November 2013. This ensures older posts still present the old URLs
    // to sharing services so that share counts are maintained.
    $url_change_date = strtotime("1/11/2013");
    $post_date = strtotime(get_the_date());
    $sharing_url = get_permalink();
    global $post;
    echo $post->post_name;
    if ($post_date > $url_change_date) {
    // Output the URL
    $sharing_url = ( "http://japantravelmate.com/" . $post);
    }
    ?>
    <!-- END -->

    P.S... there could be a completely different way of doing this. I can easily output the domain name + the post-slug with the below code, but I need to make this happen only if the post was published before Nov 1, 2013.

    data-url="http://japantravelmate.com/<?php echo $post->post_name;?>"

  2. demawo
    Member
    Posted 7 months ago #

    OK, I stripped the code right back to the code below, and now $sharing_url is adhering to ($post_date > $url_change_date)... so if the post was published after Nov 1 2013, the permalink is shown.

    Can someone help me get the post name into $sharing_url = ( "http://japantravelmate.com/");? I can output the post name onto a page with <?php echo $post->post_name; ?>, but I'm having trouble getting the right syntax for declaring $post and then getting it into the $sharing_url.

    New code:

    <?php
    $url_change_date = strtotime("1.11.2013");
    $post_date = strtotime( get_the_date( ) );
    $sharing_url = get_permalink();
    if ($post_date > $url_change_date) {
    $sharing_url = ( "http://japantravelmate.com/");
    }
    ?>
    <?php echo $sharing_url; ?>
  3. demawo
    Member
    Posted 7 months ago #

    Wow, I actually did it myself. Updating this to share the knowledge if other people are looking to do a similar thing:

    <?php
    $url_change_date = strtotime("1.11.2013");
    $post_date = strtotime(get_the_date());
    $post_name = get_query_var('name');
    $sharing_url = get_permalink();
    if ($post_date < $url_change_date) {
    $sharing_url = ("http://japantravelmate.com/" . $post_name);
    }
    ?>
    SHARING URL:<?php echo $sharing_url; ?>

Reply

You must log in to post.

About this Topic