WordPress.org

Ready to get started?Download WordPress

Forums

List related posts in same category (11 posts)

  1. Kerim
    Member
    Posted 10 years ago #

    I've been struggling over this for a week, but I'm just not good enough at coding PHP to do it myself. Can anyone help me? I'd like to produce a list of all the posts in the same category as the currently displayed post. For example, if the post X is in category #1, I'd like a list of all the posts in category #1 (including post X) listed in alphabetical order by name. Just the title with a permalink.
    The code I came up with only uses the current "post" call, which doesn't work unless you are already listing everythingn in a category:

    $blog=1;
    require_once("wp-blog-header.php");
    foreach ($post as $posts) { start_wp(); //start of loop
    ?>

  2. " rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?>
    <?
    } //end of loop
    ?>

  3. I've tried writing a function which will do a new query, but I don't really know how to do that correctly, and I don't know how to integrate it into the loop written above:

    function post_by_cat() {
    global $tableposts, $posts, $id, $post, $wpdb;
    $current_category = $post->post_category;
    $catposts = @$wpdb->get_row("SELECT ID,post_title FROM $tableposts WHERE post_category='$current_category' ORDER BY post_date");
    }

    Thanks to anyone who can help!!!
    I'm doing this to create a menu on a site with only a small number of posts per category, since it is more of a static site than a blog. However, it would probably be useful for bloggers to have a "related posts" plug-in that worked in a similar way. My goal, however, is to make a dynamic "menu" that will show all posts in a category, allowing me to only create one "hard-link" to the main post in each category from the main page.

  4. Kerim
    Member
    Posted 10 years ago #

    Of course, the last tag is closed - but it won't let me edit something twice in a row. Some kind of spam protection which only seems to affect editing posts, not actually posting...

  5. Kerim
    Member
    Posted 10 years ago #

    Two problems. #1 is this:
    SQL/DB Error:
    [You have an error in your SQL syntax near 'ORDER BY post_status DESC, post_date DESC LIMIT 30' at line 1]
    SELECT DISTINCT * FROM wp_posts WHERE (post_status = 'publish' || post_status = 'sticky') AND post_category= ORDER BY post_status DESC, post_date DESC LIMIT 30
    The number 2 problem is I'm not sure how to write the loop. I know it should look something like this, but I'm not sure how to modify it:
    $cat=posts_of_cat();
    $blog=1;
    require_once("wp-blog-header.php");
    foreach ($post as $posts) { start_wp(); //start of loop
    ?>

  6. " rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?>
    <?
    } //end of loop

  • Kerim
    Member
    Posted 10 years ago #

    Damn, forgot to escape the HTML section - but I guess that doesn't matter, it is the loop I need to work on.

  • davidchait
    Member
    Posted 10 years ago #

    Let's take this offline until I can get it working for you (and update my Amazon wishlist! ;) ), and then I will post the working solution here or the Wiki. I already see the problem the SQL error is hitting, and I can put together a quick sample for your on the rest.
    Drop me a line at davebytes[AT]comcast.net, and I'll put something together you can just grab direct off my site. I assume you want the related stuff as a sidebar list (i.e., [ul]+[li-per-post], like list_cats or something...).
    -d
    http://www.chait.net

  • davidchait
    Member
    Posted 10 years ago #

    I should note you can use get_archives, passing in a category, to get a link-list, but I've been assuming you want something a little more, say, 'customizable'...
    I'm working on it now, should have something in a bit...
    -d

  • Kerim
    Member
    Posted 10 years ago #

    David,
    I sent you an e-mail, I hope you got it.
    The get_archives function might do the trick, I just need a link list of all the posts in the current category, not anything fancy. I don't even need it limited as there will not be many posts in the category. But perhaps other people would find a customizable solution helpful.
    Thanks.
    - Luhmann

  • Kerim
    Member
    Posted 10 years ago #

    where is the get_archives function? I don't see it in functions.php or in the category template functions document.

  • Kerim
    Member
    Posted 10 years ago #

    Works great! Thanks!

  • davidchait
    Member
    Posted 10 years ago #

    No problem.
    If someone wants to add this to the Wiki, feel free.
    -d
    http://www.chait.net

  • Kerim
    Member
    Posted 10 years ago #

    I copied your post to this new page, linked from the Wiki Hacks page:
    http://wiki.wordpress.org/index.php/Related%20Posts

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags

    No tags yet.