• Hi,

    I’ve got two blogs, one in digital ink & the other in text with the same content. Each post in each blog links to its counterpart in the other blog. E.g. text version of post 1 links to ink version of post 1 and vice versa.

    I want to ‘automate’ the linking process by adding a line of code into the template itself. So far I’ve been doing it manually in the post editor.

    I need to get the post_name and only the post_name because the posts have exactly the same slug, only the main URL is different.

    Ink version: https://domain.com/y/m/d/slug
    Txt version: https://domain.com/txt/y/m/d/slug

    I’ve tried cobbling a hack but it outputs all the slugs of all the pages I have. I need it to just output the slug for the post it is in. Something to do with the post ID but I just can’t figure out how to add that in.

    Here’s the cobbled bit of hack:

    <?php
    
    function get_post_name() {
        global $wpdb, $tableposts;
        $request = "SELECT ID, post_name FROM $tableposts WHERE post_status = 'publish' ";
        $posts = $wpdb->get_results($request);
        $output = '';
        foreach ($posts as $post) {
            $post_name = ($post->post_name);
            $output .= $post_name;
        }
        echo $output;
    }
    
    ?>

    Would appreciate any help on this thanks!

    Regards,
    Silver

  • The topic ‘Output post_name ONLY in WP Loop’ is closed to new replies.