SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Output custom post type name and post id in a string

    Hey everyone,

    I would like to have a unique reference for my posts. For this I decided to output 2 letters from the custom post type name and the unique post ID which is generated automatically by wordpress.

    So for example if my post type is called "os_fashion" and the post ID is 23456 - I would like to output in a string fa23456 which is the 3rd and 4th character of the custom post type name combined with the post ID.

    I require some help with coding this as my php skills are quite limited.

    thanks in advance for your help guys
    Andy

  2. #2
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello, I would use something like this.
    I will explain a little but of what is going on within the function.
    The first line in the function which returns $string simply uses str_replace to remove the underscore, if you want to leave the possibility of including the underscore this line would then simply look like this:
    PHP Code:
    $string str_shuffle($post_type); 
    The second line just grabs the characters of the shuffled string, the length of which is determined by the size of the limit parameter. The next line appends the digits to the create the desired string. Hope this helps.
    PHP Code:
    <?

    $post_type 
    'os_fashion';
    $post_id 23456;

    function 
    generate_random_output($post_type$post_id$limit 2)
    {
        
    $string str_shuffle(str_replace('_'''$post_type));
        
    $output substr($string1$limit);
        
    $output .= $post_id;
        return 
    $output;
    }
    echo 
    generate_random_output($post_type$post_id);

    ?>

  3. #3
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keeping things simple - I'm assuming os_ prefix is always 3 characters (including the underline) how about:

    PHP Code:
    $post_ID 'os_fashion';
    $post_type 23456;    
    $prefix substr $post_ID 3);
    echo 
    $post_ID.$post_type
    Output:

    fa23456
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys this works!

  5. #5
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just noticed, I put the wrong variable in the echo - should be:

    PHP Code:
    $post_ID 'os_fashion';
    $post_type 23456;    
    $prefix substr $post_ID 3);
    echo 
    $prefix.$post_type
    That what happens when you don't test it first. Guess you noticed this anyway.
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •