WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need help with sorting posts by categories (19 posts)

  1. Anonymous
    Unregistered
    Posted 9 years ago #

    I've searched all over the net, and tried about 30 different ways, and so far nothing has worked. I know this is simple, but I'm not gettng what I want.
    I want to have a page that lists the categories and the posts associated with that category, like:
    Category 1
    post 1
    post 2
    Category 2
    post 1
    post 2
    And so on. I've set up a conditional in index.php to bring up my page when I want it, like so:
    <div id="content">
    <?php if ($posts) : foreach ($posts as $post) : start_wp();
    if ($year) {
    echo '

      ';
      require('includes/year.php');
      echo '

    ';
    }
    else {
    the_date('','<h2>','</h2>'); ?>
    And so on through the regular wp stuff.... that part works fine.
    in my year.php include, that's where I'm going to output the stuff I want, I currently have:
    <li class="storytitle"><?php the_category() ?>: " rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?>

  2. Which outputs the information I want, but I'm way off the format I want. I know I need some sort of if or foreach or while loop or something, but I'm just not getting it. Can anyone help?

  • Anonymous
    Unregistered
    Posted 9 years ago #

    GRRRRRR. Can't edit post, and blew it including code. My apologies.
    Here's the batch of code from index:
    <div id="content">
    <?php if ($posts) : foreach ($posts as $post) : start_wp();
    if ($year) {
    echo '

      ';
      require('includes/year.php');
      echo '

    ';
    }
    else {
    the_date('','<h2>','</h2>'); ?>

    etc. Like I said, that calls up the include as I need it, no problem.
    Here's the batch from year.php:
    <li class="storytitle"><?php the_category() ?>: " rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?>


  • Like I said, I know there's an easy way to get the formatting I want with a loop or something, but I'm not making it happen.
    Site is at: http://www.counselorcompanion.com/index.php
    and the category breakdown page I'm trying to create is:
    http://www.counselorcompanion.com/index.php?year=2004
    Thanks.

  • Anonymous
    Unregistered
    Posted 9 years ago #

    Anyone?

  • Anonymous
    Unregistered
    Posted 9 years ago #

    Allright, I'm beggin' here.... PLEASE? Pretty please? With sugar and gumdrops on top? Can ANYONE help with this, or at least tell me, "Sod off ya dirty bugger, it can't be done"

  • serendipity
    Member
    Posted 9 years ago #

    Search for "nicer archives". You can use Laughing Lizards code as-is or adapt it to your needs.

  • Anonymous
    Unregistered
    Posted 9 years ago #

    Thanks. Took me awhile to track it down, and I never could get it to sort by category as default, but pretty much worked a charm:
    http://www.counselorcompanion.com/archives.php

  • likoma
    Member
    Posted 9 years ago #

    Anonymous,

    I'd love to do what you did. Could you post the code? For this page:

    http://www.counselorcompanion.com/archives.php

    Thanks!

    - Bradley

  • Jaqueline
    Member
    Posted 9 years ago #

    I would love to see this code too. I was trying to use
    "nicer archives" but it never sorted by category as I wanted... :(
    Any help?

  • Lorelle
    Member
    Posted 9 years ago #

    Did anyone ever figure out how to 1) short your archives by category rather than by date, and 2) how to change the default for nicer archives (narchives) to sort by category?

  • iamarock
    Member
    Posted 9 years ago #

    Please, Please anonymous, could you share that code with the community??? is EXACTLY what i am looking for

    Thank you very much

  • iamarock
    Member
    Posted 9 years ago #

    I'm looking for that feature too, Anoymous seems missing, right?

  • tbaxter
    Member
    Posted 9 years ago #

    OK, I'm anonymous... It's been a while since I did this, and I can't remember exactly how or why I did stuff, but you all are welcome to it. The whole thing is based on nicer archives, with me hacking it to make it do what I wanted.

    <pre>
    '<div id="content">
    ' <?php
    ' $posts_per_page = '-1';
    ' $defaultorderby = 'category';
    ' $defaultorder = 'ASC';
    ' define('NL', "\n");
    ' function archive_header($before='', $after='') {
    ' global $post, $orderby, $month, $previous, $siteurl, $blogfilename, $archiveheadstart, $archiveheadend, $category_name;
    ' $orderby = explode(' ', $orderby);
    ' $orderby = $orderby[0];
    ' if ('category' == $orderby) {
    ' $thiscategory = $category_name;
    ' if ($thiscategory != $previous) {
    ' $output .= ''.get_catname($thiscategory).'';
    }
    ' $previous = $thiscategory;
    ' }
    ' if (!empty($output)) {
    ' $output = $before.$output.$after.NL;
    ' echo $output;
    ' }
    ' }
    ' function archive_date($format='Y-m-d H:i:s') {
    ' global $post;
    ' echo mysql2date($format, $post->post_date);
    ' }
    ' //Make sure categories get parsed out, they are deprecated in wp-blog-header.php
    ' global $author, $m;
    ' $orderby = 'category';
    ' if ($_POST["order"] == '') $order = "DESC";
    ' else $order = $_POST["order"];
    ' $year = '' . intval($_POST["m"]);
    ' $m = $year;
    ' if (!empty($year)) $where .= ' AND YEAR(post_date)=' . $year;
    ' if (!empty($whichauthor)) $where .= $whichauthor;
    ' $dogs = $wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1 ORDER BY cat_name $order");
    ' foreach ($dogs as $catt) {
    ' $categories = $wpdb->get_results("SELECT * FROM $tablepost2cat WHERE category_id = $catt->cat_ID");
    ' if ($categories) {
    ' echo '<div class="storycontent">';
    ' foreach ($categories as $post2category) {
    ' $posts = $wpdb->get_results("SELECT * FROM $tableposts WHERE $post2category->post_id = ID".$where);
    ' global $category_name;
    ' $category_name = $post2category->category_id;
    ' if ($posts) {
    ' foreach ($posts as $post) {
    ' start_wp();
    ' archive_header('<h4>', '</h4>

    ';
    ' echo '</div>';
    ' }
    ' }
    '?>
    ' <!--end content --></div>'</pre>

    Allright, well I can't get the <h4> header thing to output properly here, but I don't think it varies much, if any, from the loop.

  • iamarock
    Member
    Posted 9 years ago #

    Hello again and thank you Tbaxter for your help.... but it doesn't work...

    I've created a static page named "indices.php" with the same template as the index.php from my current theme and pasted your code on it, as this:

    <?php
    /*
    Template Name: Indice
    */
    ?>

    <?php get_header(); ?>
    <div id="outerColumnContainer">
    <div id="innerColumnContainer">

    <div id="leftColumn">
    <div class="inside">

    <?php get_sidebar(); ?>
    <div id="contentColumn">
    <div class="inside">

    <!-- middle column content begin -->

    <pre>
    '<div id="content">
    ' <?php
    ' $posts_per_page = '-1';
    ' $defaultorderby = 'category';
    ' $defaultorder = 'ASC';
    ' define('NL', "\n");
    ' function archive_header($before='', $after='') {
    ' global $post, $orderby, $month, $previous, $siteurl, $blogfilename, $archiveheadstart, $archiveheadend, $category_name;
    ' $orderby = explode(' ', $orderby);
    ' $orderby = $orderby[0];
    ' if ('category' == $orderby) {
    ' $thiscategory = $category_name;
    ' if ($thiscategory != $previous) {
    ' $output .= ''.get_catname($thiscategory).'';
    }
    ' $previous = $thiscategory;
    ' }
    ' if (!empty($output)) {
    ' $output = $before.$output.$after.NL;
    ' echo $output;
    ' }
    ' }
    ' function archive_date($format='Y-m-d H:i:s') {
    ' global $post;
    ' echo mysql2date($format, $post->post_date);
    ' }
    ' //Make sure categories get parsed out, they are deprecated in wp-blog-header.php
    ' global $author, $m;
    ' $orderby = 'category';
    ' if ($_POST["order"] == '') $order = "DESC";
    ' else $order = $_POST["order"];
    ' $year = '' . intval($_POST["m"]);
    ' $m = $year;
    ' if (!empty($year)) $where .= ' AND YEAR(post_date)=' . $year;
    ' if (!empty($whichauthor)) $where .= $whichauthor;
    ' $dogs = $wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1 ORDER BY cat_name $order");
    ' foreach ($dogs as $catt) {
    ' $categories = $wpdb->get_results("SELECT * FROM $tablepost2cat WHERE category_id = $catt->cat_ID");
    ' if ($categories) {
    ' echo '<div class="storycontent">';
    ' foreach ($categories as $post2category) {
    ' $posts = $wpdb->get_results("SELECT * FROM $tableposts WHERE $post2category->post_id = ID".$where);
    ' global $category_name;
    ' $category_name = $post2category->category_id;
    ' if ($posts) {
    ' foreach ($posts as $post) {
    ' start_wp();
    ' archive_header('<h4>', '</h4>

    ');
    ' ?>
    * ' ID) ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?> '<cite>(<?php archive_date('m-d-Y') ?>)</cite>

    ' <?php
    ' }
    ' }
    ' }
    ' echo '

    ';
    ' echo '</div>';
    ' }
    ' }
    '?>
    ' <!--end content --></div>'</pre>

    <!-- middle column content end -->

    </div>
    </div>
    <div class="clear mozclear"></div>
    </div>
    </div>
    <div id="footer" class="inside">

    <?php get_footer(); ?>

    But when I load the page all I get is the following error:

    "Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /var/www/wordpress/wp-content/themes/computer age/indices.php on line 27"

    Anyone could tell us what's going wrong?

    Thank you

  • Lori
    Member
    Posted 9 years ago #

    Wow this is a whole other language to me, wish I knew how to do this too!

  • iamarock
    Member
    Posted 9 years ago #

    Please, I'm too interested in this too

  • hellokelli
    Member
    Posted 8 years ago #

    thanks tbaxter i have been looking EVERYWHERE for this! and to iamarock- i tried to look at your code to see what the parse error was but i couldn't find it. When i was trying to get the code to work i got a few parse errors and they were all because i accidently deleted a single quote in the syntax. also- the

    ' * ' ID) ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?> '<cite>(<?php archive_date('m-d-Y') ?>)</cite> '

    line looks like only half of the line made it on the post... maybe everyone else already knows this, but it sure took me a while to figure out that the rest of the line should look something like this:

    ' <h3 id="post-<?php the_ID(); ?>"><a*href="<?php '
    ' the_permalink() ?>" rel="bookmark" title="Permanent Link to '
    ' <?php the_title(); ?>"><?php the_title(); ?> '<cite>(<?php '
    ' archive_date('m-d-Y') ?>)</cite> </h3> '

    *note i had to put a * in the link code "ahref" otherwise it would show up as a link

  • moshu
    Member
    Posted 8 years ago #

    *note i had to put a * in the link code "ahref" otherwise it would show up as a link

    Not if you (and the others) put the code in backticks - as it is indicated below the text area here :)
    e.g. <a href="http://example.com">link text</a>

  • Joni
    Member
    Posted 8 years ago #

    testing backticks
    testing
    another line of code

    This wasn't working earlier.

  • Joni
    Member
    Posted 8 years ago #

    Oh, for the love of heaven, what's wrong with the BACKTICKS! Ugh!

    <?php
    /*
    Template Name: Category Archives Template
    */
    ?>

    <?php get_header(); ?>

    <?php
    $posts_per_page = '-1';
    $defaultorderby = 'category';
    $defaultorder = 'ASC';
    define('NL', "n");
    function archive_header($before='', $after='') {
    global $post, $orderby, $month, $previous, $siteurl, $blogfilename, $archiveheadstart, $archiveheadend, $category_name;
    $orderby = explode(' ', $orderby);
    $orderby = $orderby[0];
    if ('category' == $orderby) {
    $thiscategory = $category_name;
    if ($thiscategory != $previous) {
    $output .= ''.get_catname($thiscategory).'';
    }
    $previous = $thiscategory;
    }
    if (!empty($output)) {
    $output = $before.$output.$after.NL;
    echo $output;
    }
    }
    function archive_date($format='Y-m-d H:i:s') {
    global $post;
    echo mysql2date($format, $post->post_date);
    }
    //Make sure categories get parsed out, they are deprecated in wp-blog-header.php
    global $author, $m;
    $orderby = 'category';
    if ($_POST["order"] == '') $order = "DESC";
    else $order = $_POST["order"];
    $year = '' . intval($_POST["m"]);
    $m = $year;
    if (!empty($year)) $where .= ' AND YEAR(post_date)=' . $year;
    if (!empty($whichauthor)) $where .= $whichauthor;
    $dogs = $wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1 ORDER BY cat_name $order");
    foreach ($dogs as $catt) {
    $categories = $wpdb->get_results("SELECT * FROM $tablepost2cat WHERE category_id = $catt->cat_ID");
    if ($categories) {
    echo '<div class="post">';
    foreach ($categories as $post2category) {
    $posts = $wpdb->get_results("SELECT * FROM $tableposts WHERE $post2category->post_id = ID".$where);
    global $category_name;
    $category_name = $post2category->category_id;
    if ($posts) {
    foreach ($posts as $post) {
    start_wp();
    archive_header('<h2 class="cathead">', '</h2>'

    );
    ?>

    <p class="catlist" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> :: <?php
    archive_date('m-d-Y') ?>

    <?php
    }
    }
    }
    echo
    ;
    echo '</div>'; //this closes POST div //
    }
    }
    ?>

    <?php get_footer(); ?>

    This is what I was able to piece together from the code soup. I created an empty page and put this code into it. You may have to tweak your stylesheet to get the effect you want. I added an H2 class and another p.class to adjust the formatting a bit. Now if it would just sort the cats in alpha order! I'll not be looking a gifthorse in the mouth tho.

    See it in action on my blog.

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags

    No tags yet.