• Resolved Anonymous

    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(); ?>

    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?

Viewing 15 replies - 1 through 15 (of 18 total)
  • Thread Starter Anonymous

    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: https://www.counselorcompanion.com/index.php
    and the category breakdown page I’m trying to create is:
    https://www.counselorcompanion.com/index.php?year=2004
    Thanks.

    Thread Starter Anonymous

    Anyone?

    Thread Starter Anonymous

    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”

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

    Thread Starter Anonymous

    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:
    https://www.counselorcompanion.com/archives.php

    Anonymous,

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

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

    Thanks!

    – Bradley

    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?

    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?

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

    Thank you very much

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

    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.

    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

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

    Please, I’m too interested in this too

    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

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Need help with sorting posts by categories’ is closed to new replies.