Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [Solved] Generate outline of a wordpress article (updated)

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #29117
    Zonglars
    Member

    I’m working on a blog right now and I want to write a function that automates something I plan on doing with my articles:

    – Locate all of the headers, add a unique ID to them
    – Generate a nested list based on the level of each header, and each list item links to it’s respective header

    I’ve already solved most of it:
    – scan the content for headings
    – get the level # of each heading
    – get the internal text of the headings
    – create a list of the links, each one linking to it’s respective heading *
    – insert the unique ID into each heading

    I’m not sure how to make a nested list based on the levels, that’s all that’s left.

    here’s my code so far:

    Code:
    $post;//already declared through get_post(ID) function

    //match all starting with , any character(s), then
    $headpattern=”|(.+)|”;

    //strip all tags from the code except the headers, since we don’t need the html tags inside the headers
    $newpost=strip_tags($post,’

    ‘);

    if(preg_match_all($headpattern,$newpost,$matches)){//find all matching patterns inside the stripped post content
    //matches will return 2 arrays: the first is the full HTML match, the second is the text inside the heading tags (found through “(.+)”)
    $i=0;
    // get the heading level pattern
    foreach($matches[0] as $html){
    //get whatever number comes after the n”;//start the list
    $i=1;
    foreach($headings as $title){
    //create a list item, with a class according to it’s level, with the title, linking to a unique ID for it’s header
    //example:

  • Lorem Ipsum
  • the class is simply a last resort if it can’t be nested
    echo “

  • “.$title[1].”
  • n”;
    $i++;
    }
    echo ““;//close the list

    $i=1;
    // add the new IDs to the headers
    foreach($headings as $title){
    //insert the id tag into the headers, between the , into the original content
    //should return something like this:

    $replace=”$1 id=”h”.$title[0].”-“.$i.””$2”;
    $post=preg_replace(‘|()|’,$replace,$post,1);
    $i++;
    }

    echo $post;//print the original content
    }else{
    echo “FAIL!”;
    }

    So, how can I generate a nested list, preferably using the $headings nested array.

    UPDATE: Nevermind, I found something that gave me an idea, here’s the code incase anyone else had trouble with this:

    Code:
      and write the link
      if($i==1){
      echo “
    • “.$title[1].”“;
      //if it’s h# is less than the previous one (higher level), then it closed the previous
    • &
        before opening a new

      • }elseif($title[0]<$previous){ $diff=$previous-$title[0];//how far apart the levels are (might jump from an h5 back to an h2) for($n=$diff;$n>0;$n–){//close
      • s and
          s as many times as the difference between levels
          echo “
      • n

      n”;
      }
      echo “

    • n

    • “.$title[1].”“;
      //if it’s h# is greater than the previuos one (lover level), then it opens a new

        before opening a new

      • }elseif($title[0]>$previous){//I’ll just assume nobody will jump from an h2 to an h5, so no diff check here.
        echo “n

          n

        • “.$title[1].”“;
          //finally, if it’s on the same level, it simply closes the previus
        • before opening a new
        • }else{
          echo “
        • n

        • “.$title[1].”“;
          }
          $previous=$title[0];
          $i++;
          }?>

    And I checked, so long as the structure is in a logical order (i.e. incrementing down the levels) it writes perfectly valid code.

    #76521
    TheDoc
    Member

    Thanks for coming back with an update!

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.