• Resolved demawo

    (@demawo)


    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 https://japantravelmate.com/ + the post slug, all that’s coming out is https://japantravelmate.com/. 2/ $url_change_date = strtotime doesn’t seem to be working, $sharing_url is always putting out https://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 = ( “https://japantravelmate.com/&#8221; . $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=”https://japantravelmate.com/&lt;?php echo $post->post_name;?>”

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter demawo

    (@demawo)

    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 = ( "https://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 = ( "https://japantravelmate.com/");
    }
    ?>
    <?php echo $sharing_url; ?>

    Thread Starter demawo

    (@demawo)

    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 = ("https://japantravelmate.com/" . $post_name);
    }
    ?>
    SHARING URL:<?php echo $sharing_url; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Build a URL for social sharing based on post-slug (not permalink) & publish date’ is closed to new replies.