treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Modifying WordPress PHP

  • Hello all,

    I'm trying to modify a WordPress file for the article pages of Mxdwn.com. Here is an example: MxDwn. After the article, there is Related ... Content links with subcategories such as News & Reviews. I am trying to prevent the recurrence of any link, such as Trent Reznor and Atticus Ross – The Girl with the Dragon Tattoo which is seen six times in the list.

    Here is the original code which prints the list:

        while (list($term_taxonomy_id, $post_id, $taxonomy, $term_id, $name) = mysql_fetch_row($query)) {
          $check = 0;
          if ($name) {
              if ($check == 0) {
                  echo "<br><b>Related $name Content:</b><br>";
              }
              $check = $check + 1;
              $result2 = "select wp_terms.term_id as term_id, wp_terms.name as name, wp_term_taxonomy.term_taxonomy_id as term_taxonomy_id1, wp_term_relationships.object_id as post_id, wtr2.term_taxonomy_id as term_taxonomy_id2, wtt2.term_id as category_id, wtt2.taxonomy as category, wt2.name as category_name from wp_terms left join wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id left join wp_term_relationships on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id left join wp_term_relationships as wtr2 on wp_term_relationships.object_id = wtr2.object_id left join wp_term_taxonomy as wtt2 on wtr2.term_taxonomy_id = wtt2.term_taxonomy_id left join wp_terms as wt2 on wtt2.term_id = wt2.term_id where wp_terms.term_id = $term_id and wtt2.taxonomy = 'category' order by category_name ASC";
              $query2 = mysql_db_query("mxdwn", $result2);
              $checkreviews = 0;
              $checknews = 0;
              $checkfeatures = 0;
              while (list($term_id, $name, $term_taxonomy_id1, $post_id, $term_taxonomy_id2, $category_id, $category, $category_name) = mysql_fetch_row($query2)) {
                  if ($category_name != 'Top Story') {
                      if ($category_name == 'Reviews' && $checkreviews == 0) {
                          echo "<br>Reviews<br>";
                          $checkreviews = $checkreviews + 1;
                      } 
                      elseif ($category_name == 'News' && $checknews == 0) {
                          echo "<br>News<br>";
                          $checknews = $checknews + 1;
                      } 
                      elseif ($category_name == 'Features' && $checkfeatures == 0) {
                          echo "<br>Features<br>";
                          $checkfeatures = $checkfeatures + 1;
                      }
                      $permalink = get_permalink($post_id);
                      $relatedtitle = get_the_title($post_id); 
                      echo "<a href=\"$permalink\">$relatedtitle</a><br>";
                  }
              }
          }
      }
    

    It appears the content echoed relies solely on $post_id, so I created a check to see if the loop puts through an identical $post_id. It can be seen in the code with some other slight modifications:

      $already_posted = array(); //AMS; create array that will hold $post_id for each title/permalink already echoed
      while (list($term_taxonomy_id, $post_id, $taxonomy, $term_id, $name) = mysql_fetch_row($query)) {
    
          $post_check = true;
          foreach($already_posted as $already_posted_each) {
              if( $post_id == $already_posted_each) {
                  $post_check = false;
          break;
              }
          }
          if( $post_check == true ) {
        $check = 0;
              if ($name) {
                  if ($check == 0) {
                      echo "<br><b>Related $name Content:</b><br>";
                      // $name of the tag, such as "The Flaming Lips" or "B.B. King"
                  }
                  $check = $check + 1;
          $result2 = "select wp_terms.term_id as term_id, wp_terms.name as name, wp_term_taxonomy.term_taxonomy_id as term_taxonomy_id1, wp_term_relationships.object_id as post_id, wtr2.term_taxonomy_id as term_taxonomy_id2, wtt2.term_id as category_id, wtt2.taxonomy as category, wt2.name as category_name from wp_terms left join wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id left join wp_term_relationships on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id left join wp_term_relationships as wtr2 on wp_term_relationships.object_id = wtr2.object_id left join wp_term_taxonomy as wtt2 on wtr2.term_taxonomy_id = wtt2.term_taxonomy_id left join wp_terms as wt2 on wtt2.term_id = wt2.term_id where wp_terms.term_id = $term_id and wtt2.taxonomy = 'category' order by category_name ASC";
                  $query2 = mysql_db_query("mxdwn", $result2);
                  $checktopstory = 0;
                  $checkhighfidelity = 0;
                  $checkreviews = 0;
                  $checknews = 0;
                  $checkfeatures = 0;
                  while (list($term_id, $name, $term_taxonomy_id1, $post_id, $term_taxonomy_id2, $category_id, $category, $category_name) = mysql_fetch_row($query2)) {
                      $post_check = true;
                      foreach($already_posted as $already_posted_each) {
                          if( $post_id == $already_posted_each) {
                              $post_check = false;
                break;
                          }
                      }
    
                      if( $post_check == true ) {
    
                          if ($category_name == 'Top Story' || $category_name == 'High Fidelity' || $category_name == 'Reviews' || $category_name == 'News' || $category_name == 'Features') {
                          // any category we do not want to appear on page must go in this if statement
    
    
                              if ($category_name == 'Top Story' && $checktopstory == 0) {
                                  echo "<br>News<br>";
                                  $checktopstory = $checktopstory + 1;
                              } 
                              elseif ($category_name == 'High Fidelity' && $checkhighfidelity == 0) {
                                  echo "<br>Features<br>";
                                  $checkhighfidelity = $checkhighfidelity + 1;
                              }
    
                              elseif ($category_name == 'Reviews' && $checkreviews == 0) {
                                  echo "<br>Reviews<br>";
                                  $checkreviews = $checkreviews + 1;
                              } 
                              elseif ($category_name == 'News' && $checknews == 0) {
                                  echo "<br>News<br>";
                                  $checknews = $checknews + 1;
                              } 
                              elseif ($category_name == 'Features' && $checkfeatures == 0) {
                                  echo "<br>Features<br>";
                                  $checkfeatures = $checkfeatures + 1;
                              }
    
                              $permalink = get_permalink($post_id);                 
                              // get_permalink returns a permalink (string) for a given post id
    
                              $relatedtitle = get_the_title($post_id);          
                              // get_the_title returns the title of a post for a given post id
    
                              echo "<a href=\"$permalink\">$relatedtitle</a><br>";
                              // displays title post anchored to corresponding permalink
    
                              $already_posted[] = $post_id;
    
                          }
                      }
                  }
              }
          }
      }
    

    Now, when a $post_id is repeated, the echoed list is abruptly stopped. Any ideas?