• Resolved codecottage

    (@codecottage)


    I have a set of posts with titles like this:

    20071121 November 21, 2007

    I’d like to strip out the numeric date (used for sorting) and leave the date in words. The date shows up with the numbers but I don’t even think that it has even gotten to the point where I attempt to reformat it.

    Code

    $postslist = get_posts('numberposts=10&order=ASC&orderby=post_title');
    foreach ($postslist as $post) :
       setup_postdata($post);
       $mypost_title = the_title();
       $datewords = preg_split("/[\s]+/", $mypost_title);
       echo $datewords[1] . " " . $datewords[2] . " " . $datewords[3];
      the_excerpt();
      echo "<br />";
    endforeach;

    Output:

    http://test1.handcraftedsites.com/index.php

    Thank you for your help!

    Elizabeth

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

    (@codecottage)

    I found the answer using Google instead of the ww.wp.xz.cn search function. Here it is:

    I found the syntax for the_title and here’s the revised line
    OLD VERSION: $mypost_title = the_title();
    NEW VERSION: $mypost_title = the_title(”,”,false);

    I modified the first line as well to use query_posts.
    Here’s the new code in its entirety:

    $postslist = query_posts(‘cat=7&order=DESC’);
    foreach ($postslist as $post) :
    setup_postdata($post);
    $mypost_title = the_title(”,”,false);
    $datewords = preg_split(“/[\s]+/”, $mypost_title);
    echo $datewords[1] . ” ” . $datewords[2] . ” ” . $datewords[3];
    the_excerpt();
    echo “
    “;
    endforeach;

    The info on the syntax for the_title is at:
    http://codex.ww.wp.xz.cn/Template_Tags/the_title

    Thread Starter codecottage

    (@codecottage)

    This scheme has one serious fallacy I discovered later! The data in the title is string data and hence the numbers weren’t being sorted properly.

    Instead, I am making a calendar using pages (not posts) and placing the dates in the menu order box on the page development screen. (ex. 20070405) I sort on menu order and it works great. No need to write any extra code!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Parse Post Title’ is closed to new replies.