WordPress.org

Ready to get started?Download WordPress

Forums

Related Posts (24 posts)

  1. Anonymous
    Unregistered
    Posted 10 years ago #

    I downloaded and successfully installed the Related Posts hack. I did find one problem. When you validate the XHTML, the parser gives an empty tag warning (

      ). So... I added a few lines to the hack to make it validation-friendly. You can view the modified code on my website.

    • davidchait
      Member
      Posted 10 years ago #

      Weird. Hey mark, can you check out the version on my site:
      http://www.chait.net/index.php?p=87
      .. I added a case to test the output being null. But it really shouldn't be hitting that case unless something with the randomizer is going wrong. The count() case earlier detects <=0 counts and should generate proper xhtml.
      What case were you seeing the bad xhtml?
      -d

    • Anonymous
      Unregistered
      Posted 10 years ago #

      Something in the randomizer did appear to be going wrong, but it was intermittent. Adding that bit of code appeared to have solved the problem.

    • davidchait
      Member
      Posted 10 years ago #

      Cool. Glad to hear it.
      and if you mean MY site validation, it has gotten cleaned up a lot. It generally had to do with some of my internal link generation code not doing htmlentities calls on the title/alt text and body text before echoing. But the same_cat lib uses the wp link-builder fns, so if that's where it's failing, I'm not sure what I'm doing wrong. Again, might be that I need to be calling htmlentities, but I'd think the link-builder would do so.
      -d
      http://www.chait.net

    • OperaManiac
      Member
      Posted 10 years ago #

      Chait, need some help...
      the hack works fine. but there is no randomization... :(
      it picks the latest five post from the categories...

    • davidchait
      Member
      Posted 10 years ago #

      you calling it like:
      list_posts_of_cat($somecat, 5, 5)
      to get 5 random (of 5 max) posts? if you want all random, both numbers should be the same.
      my popular-posts code uses very similar code (I pulled it from there...), and I do something like ($somecat, 6, 3), giving me 6 total, but fix the first three as the actual first three, and the second three as random from the rest of the list..
      Let me know if that didn't help any, if it's simply busted. I looked at the code, fixed one minor thing, but you should still be seeing some randomness...
      -d

    • OperaManiac
      Member
      Posted 10 years ago #

      echo list_posts_of_cat(get_the_category(), 5, 5);
      using this and i get the latest 5 posts...

    • davidchait
      Member
      Posted 10 years ago #

      That's the same code I use on my single-post pages. Pick any article, look at the left sidebar 'similar posts' list. refresh the page. it changed! ;)
      I'm running older code, possible that something changed in the format of the array returned by get_the_category that is now failing weirdly?
      Where/when in your site are you using that call?
      -d

    • carthik
      Member
      Posted 10 years ago #

      @davidchait, I looked at the code in the WikiPage, and it seems like for the functions, some of the variables are fixed, in the function call.
      For example
      function list_posts_of_cat($cat_array, $max = 8, $randomize = 0.....
      shouldn't $max and $randomize not have have 8 and 0 assigned to them?
      Just something I noticed.

    • OperaManiac
      Member
      Posted 10 years ago #


      <?php include(ABSPATH . 'wp-comments.php'); ?>
      </div>
      <?php if ($name != '') { add_count($p);}?>
      <?php } } else { // end foreach, end if any posts ?>
      Sorry, no posts matched your criteria.
      <?php } ?>
      <?php if ($name != '')
      {
      echo "<div class='related'>Related Posts:";
      echo list_posts_of_cat(get_the_category(), 5, 5);
      echo "</div>";
      }
      ?>

      this is where it is posted...

    • davidchait
      Member
      Posted 10 years ago #

      2fargon -
      Those are default values. i.e., you can call:
      list_posts_of_cat(get_the_category)
      and it will output the last 8 in order by default.
      Any script I write typically has tons of default parameters, as does much of the WP code itself. Less useful in this particular script (5, 5 might be more useful), but you get the idea.
      Sushubh -
      I'll try that out on my test site at home, see if it's doing something weird, and if so I'll debug it.
      -d
      http://www.chait.net

    • OperaManiac
      Member
      Posted 10 years ago #

      still showing the last 5 latest posts.
      reapplied the hack and the code into index. still no use...
      using 21st nightly.

    • davidchait
      Member
      Posted 10 years ago #

      I'll go grab CVS and do a diff, see if anything jumps out.
      -d

    • davidchait
      Member
      Posted 10 years ago #

      I looked through the latest code, and while there are changes happening all over the place, most of the category code should be working well (otherwise, you wouldn't even get posts from that category!).
      Here's some suggestions to help you help me debug this :) :
      Try printing out values at various points...
      echo $c after it is set to count($catposts). It should probably be the lesser of total posts in cat or 10...
      echo $totalp, should be 10 (thus the 10 above).
      echo $max-$randomize, should be zero (for obvious reasons).
      echo $k after it is set to $i... I think it should be zero in this case.
      (WAIT)
      AHHHHH.... one thing DID just leap out at me. make sure you have a call to srand, needed for older PHP installations. like:
      srand((double) microtime() * 1000000);
      I have them sprinkled around, though there should just be one inside of wp-config. I don't know what other subsystems use rand calls, but in theory not having srand called would lead the system to not return a random result, which would then iterate through the list in order, which would get you the first n posts found...
      -d
      http://www.chait.net

    • soulcore
      Member
      Posted 10 years ago #

      heya david,
      i’m using your great hack on http://www.pixelblog.de and updated to wordpress 1.2 earlier this week (nightly builds).
      now i have the problem with sub-categories not be shown.
      can you workaround that or release a new version?
      sorry to bother but the hack is just great, i don’t want to dismiss ‘m ;)
      soulcore

    • davidchait
      Member
      Posted 10 years ago #

      Will look into it. (cool layout btw -- wish I could read it!)
      -d

    • OperaManiac
      Member
      Posted 10 years ago #

      i agree :)

    • davidchait
      Member
      Posted 10 years ago #

      I just grabbed from cvs, created a new scratch wp install.
      I created categories:
      - second
      -- subsecond
      - third
      -- subthird
      I created posts:
      Second, in second
      Second 2, in subsecond
      Second 3, in second+subsecond
      Third, in third
      Third2, in subthird
      Third3, in third+subthird
      I added include('./same_cat.php'); to top of index.php.
      I added
      <?php
      echo "<div class='related'>Related Posts:";
      echo list_posts_of_cat(get_the_category(), 5, 5);
      echo "</div>";
      ?>
      ... just prior to the include of wp-comments.php.
      Viewing the site outputs the expected lists of related/matching category posts.
      Then I added a duplicate, except using (..., 1, 1) to try the random. Got same thing every time.
      So I added to the top of same_cat.php on the line followin the opening <?php:
      srand((double) microtime() * 1000000);
      Now I get the random, expected result for the 1,1 case.
      Let me know what's not working for you folks. I can't find anything wrong with it here, unfortunately.
      -d
      http://www.chait.net

    • OperaManiac
      Member
      Posted 10 years ago #

      where is same_cat.php?
      and adding srand((double) microtime() * 1000000); to my-hacks.php did not help... :p

    • soulcore
      Member
      Posted 10 years ago #

      hmm, it works again. dunno what was wrong then.
      added the "srand((double) microtime() * 1000000);" to my-hacks.php (top)
      @sushubh: same_cat.php is the stuff in your my-hacks.php
      you don't include something when you got all that stuff in there.
      thanks david

    • OperaManiac
      Member
      Posted 10 years ago #

      this is my hack file:
      http://blog.sushubh.com/my-hacks.phps
      what to do?

    • soulcore
      Member
      Posted 10 years ago #

      put the following on top of it:
      srand((double) microtime() * 1000000);
      (if you don't start it somewhere else like your index.php...)
      that's for randomizing the stuff.
      to view the related articles paste:
      <?php
      echo "<div class='related'>Related Posts:";
      echo list_posts_of_cat(get_the_category(), 5, 5);
      echo "</div>";
      ?>
      before the comments in wp-comments.php (or where you want it)

    • OperaManiac
      Member
      Posted 10 years ago #

      ok the line was there...
      now its at the top.
      still:
      http://blog.sushubh.com/archives/2004/04/30/first-day-without-a-job-in-a-long-time-is-very-eventful/ has related posts in date wise orders with the last 5 articles coming in.

    • davidchait
      Member
      Posted 10 years ago #

      Sushubh -
      I just put up a new version on my site. Same location:
      http://www.chait.net/index.php?p=87
      to keep things clean, you might want to just include('same_cat.php') into your wp-hacks file. makes it easier to upgrade. ;)
      1. I added in the srand, to ensure everyone has it from now on.
      2. I added a new param in, after the MAX, RANDOM params, which is a postID to 'ignore'. So you can now do within the posts loop:
      <?php
      echo "<div class='related'>Related Posts:";
      echo list_posts_of_cat(get_the_category(), 1, 1, $post->ID);
      echo "</div>";
      ?>
      ... and it will filter OUT the current post from the possible responses.
      3. At the top of the file are two new globals:
      $sameCatDebug = 0;
      $sameCatDebugout = '';
      if you change the debug to 1, it will accumulate debugging information into sameCatDebugout. You can then do an echo, or write to a logfile, to capture the output. I tested it with
      <?php echo "<div id='samecaterrs' style='font: 10px Times, serif'>$sameCatDebugout</div>"; ?>
      if you capture the output for me for a few runs, I'll look at it and see if something seems off. The only thing that would cause it is if rand isn't working, or if your total posts in a cat is <= requested amount.
      I'll keep banging on it if I can get some debug output from you guys. BTW, watch for "randomize disabled" in the output, as that'll show up in cases where random did get turned off bc of not enough query results. The output should be somewhat self-explanatory if you've read the code at all.
      -d

    Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags

    No tags yet.