commenting closed on an unusual wordpress site, desperate

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shatterjack
Forum Newbie
Posts: 3
Joined: Mon Dec 28, 2009 3:21 pm

commenting closed on an unusual wordpress site, desperate

Post by shatterjack »

In order to make this wordpress site do what I want, it seems I have gutted a lot of the common functions they use to make things happen and now I'm kind of alone with it. I have a multi-author site where each author gets one column div to post things he/she is working on and to encourage people to stay crafty and creative I sort the divs from left to right by most recent poster so they are the ones you first see when you load the page. To make this work, I used mysql queries to join some tables and then output the data.

I'm having trouble with making sure the comments display properly for each post as well as making the comment field (where you would type in your name and comment) appear below each post. Here is my index.php:

Code: Select all

<?php
get_header ();
?>
 
<div class="comments-template">
 
 
<div id="box">
 
<?php
    
$qry =  "SELECT u.id, user_nicename, post_date, post_content, post_title, post_type, post_excerpt, comment_content, comment_author, comment_approved, wp_posts.ID as post_id
         FROM wp_posts
         INNER JOIN wp_users AS u ON post_author = u.id
         LEFT JOIN wp_comments ON wp_posts.ID = comment_post_ID
         WHERE post_type='post'
         ORDER BY user_nicename ASC, post_date DESC";
 
$res = mysql_query($qry);
 
if ($res) {
   
    while ($row = mysql_fetch_assoc($res)) {
       $author[$row['user_nicename']][]=$row;       
    }
    
    foreach($author as $k => $v) {
        $orderbydate[$v[0]['post_date']][$k] = $v;
    }
    krsort($orderbydate);
    foreach ($orderbydate as $k => $v) {
        ksort($orderbydate[$k], SORT_STRING);
    }
}
 
 
 
$divIds = array('one', 'two', 'three', 'four', 'five', 'six');
$i = 0;
 
foreach ($orderbydate as $date => $authors) {
    foreach ($authors as $author=> $posts) {
        
          echo '<div id="'.$divIds[$i++].'">';
               if($author=="tony") {
                    echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/tonyname.jpg">';
               }
               elseif($author=="hippie") {
                    echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/hippiename.jpg">';
               }
               elseif($author=="eriku") {
                    echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/erikuname.jpg">';
               }
               elseif($author=="ben") {
                    echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/benname.jpg">';
               }
               elseif($author=="hingyi") {
                    echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/hingyiname.jpg">';
               }
               elseif($author=="chris") {
                    echo '<img src="http://www.hippievstony.com/wp-content/uploads/chrisname.jpg">';
               }
 
 
            $lastPost = null;
                 foreach($posts as $post) {                              
                      
                     if ($lastPost != $post['post_id']) {
                          
                     echo '<h2>' . $post['post_title'] . '</h2><small>' . $post['post_date'] . '</small>';
                     echo '<p>' . $post['post_content'] . '</p>';
                     echo '<p>' . $post['post_excerpt'] . '</p>';
                     echo '<p>' . $post['comment_content'] . '</p>';
                     echo '<p><small>-' . $post['comment_author'] . '</small></p>';                     
                     }
                 $lastPost = $post['post_id'];
                 }
 
               
                 if ($post['comment_approved']=1) {
                     if ($post['comment_author']){
                          
                          echo '<p>' . $post['comment_content'] . '</p>';
                          echo '<p><small>-' . $post['comment_author'] . '</small></p>';
                          }
                     else echo '<p>no comment author</p>';            
                }     
                else echo '<p>comment awaiting approval</p>';    
                               
                                                 
                   
        echo '</div>';
    }
}
 
?>
 

and here is the comments.php file in case you think it might help:

Code: Select all

<?php 
if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) die ('Please do not load this page directly. Thanks!');
if (!empty($post->post_password)) { // if there's a password
    if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
?>
 
<h2><?php _e('Password Protected'); ?></h2>
<p><?php _e('Enter the password to view comments.'); ?></p>
 
<?php return;
    }
}
 
    
 
$oddcomment = 'alt';
 
?>
 
 
 
<?php if ($comments) : ?>
    <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3>
 
<ol class="commentlist">
<?php foreach ($comments as $comment) : ?>
 
    <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
 
<div class="commentmetadata">
<strong><?php comment_author_link() ?></strong>, <?php _e('on'); ?> <a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> <?php _e('at');?> <?php comment_time() ?></a> <?php _e('Said&#58;'); ?> <?php edit_comment_link('Edit Comment','',''); ?>
        <?php if ($comment->comment_approved == '0') : ?>
        <em><?php _e('Your comment is awaiting moderation.'); ?></em>
        <?php endif; ?>
</div>
 
<?php comment_text() ?>
    </li>
 
<?php /* Changes every other comment to a different class */
    if ('alt' == $oddcomment) $oddcomment = '';
    else $oddcomment = 'alt';
?>
 
<?php endforeach; ?>
    </ol>
 
<?php else : ?>
 
<?php if ('open' == $post->comment_status) : ?>
    <!-- If comments are open, but there are no comments. -->
    <?php else : // comments are closed ?>
 
    
 
<?php endif; ?>
<?php endif; ?>
 
 
 
<?php if (comments_open()) : ?>
 
        <h3 id="respond">Leave a Reply</h3>
 
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>">logged in</a> to post a comment.</p>
 
<?php else : ?>
 
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<?php if ( $user_ID ) : ?>
 
<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Log out of this account">Logout &raquo;</a></p>
 
<?php else : ?>
 
<p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="40" tabindex="1" />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>
 
<p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="40" tabindex="2" />
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>
 
<p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="40" tabindex="3" />
<label for="url"><small>Website</small></label></p>
 
<?php endif; ?>
 
<!--<p><small><strong>XHTML:</strong> <?php _e('You can use these tags&#58;'); ?> <?php echo allowed_tags(); ?></small></p>-->
 
<p><textarea name="comment" id="comment" cols="60" rows="10" tabindex="4"></textarea></p>
 
<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
</p>
 
<?php do_action('comment_form', $post->ID); ?>
 
</form>
 
<?php endif; ?>
 
<?php endif; ?>
I have removed the two lines that are supposed to output the comment field:

$withcomments='1';
comments_template();

because they output all comments for all posts below each post. I have put those two lines in a lot of different places, but it's not working. I can post my comments.php file if you think it might help.

I got through a lot of this with help from others but now have come to a real dead end. Being able to post comments is a big part of the site so I would greatly appreciate any help. Thanks

tony
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: commenting closed on an unusual wordpress site, desperate

Post by manohoo »

In your comments.php file, line 22:

Code: Select all

<?php if ($comments) : ?>
    <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3>
I don't understand what you are trying to accomplish, but the IF statement above gets me lost. Are you jumping into HTML and leave the IF statement hanging? What is the colon doing there?

Try to make your IF statement easy to read:
if (condition) {
// do somehitng, or embed html
echo "<h3> This is HTML </h3>";
}

If you have to use HTML as part of the IF statement then "echo" your HTML code, instead of jumping in and out of PHP.
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: commenting closed on an unusual wordpress site, desperate

Post by MichaelR »

manohoo wrote:I don't understand what you are trying to accomplish, but the IF statement above gets me lost. Are you jumping into HTML and leave the IF statement hanging? What is the colon doing there?
It's the alternate syntax:

Code: Select all

 
  if ($foo == $bar):
    echo 'Foo equals bar';
  else:
    echo 'Foo does not equal bar';
  endif;
 
Is the same as

Code: Select all

 
  if ($foo == $bar) {
    echo 'Foo equals bar';
  }
 
  else {
    echo 'Foo does not equal bar'r;
  }
 
Think of switch statements and using case 1: echo $foo; break;. Similar format.
shatterjack
Forum Newbie
Posts: 3
Joined: Mon Dec 28, 2009 3:21 pm

Re: commenting closed on an unusual wordpress site, desperate

Post by shatterjack »

I got that comments.php file from this site about making your own wordpress template, so I thought it would work ok, and it did before I changed my index.php code from a more standard format to one based on all these joined tables. Right now I am trying to use my old code inside of the new stuff. Here is my old code that outputted the posts themselves correctly but didn't sort them the way I wanted:

Code: Select all

<?php
get_header ();
?>
<div id="box">
    
 
 
 
 
 
 
<?php
$last = wp_get_recent_posts( '1');
$last_id = $last['0']['ID'];
echo $last_id;
?>
 
<div id="one">
     <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/tonyname.jpg"
     <?php $my_query = new WP_Query ( 'author_name=tony' );
     while ( $my_query->have_posts () ) :
        $my_query->the_post () ?>
        
        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->    
        
                   <?php $withcomments = 1; ?>
                   <?php comments_template(); ?>
            </div><!-- end post div -->    
<?php
endwhile;/*end tony while have posts*/
 
?>
    </div><!-- End one Div -->
 
 
 
 
 
    <div id="two">
    <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/hippiename.jpg"
    <?php
    $my_query = new WP_Query ( 'author_name=hippie' );
    while ( $my_query->have_posts () ) :
        $my_query->the_post () ?>
 
        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->
        
            <?php $withcomments = 1; ?>
            <?php comments_template(); ?>
 
        </div><!-- end post div -->    
<?php
endwhile;/*end hippie while have posts*/
 
?>
    </div><!-- End two Div -->
 
 
 
<div id="three">
    <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/erikuname.jpg"
    <?php
    $my_query = new WP_Query ( 'author_name=eriku' );
    while ( $my_query->have_posts () ) :
        $my_query->the_post () ?>
 
        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->
        
            <?php $withcomments = 1; ?>
            <?php comments_template(); ?>
 
        </div><!-- end post div -->    
<?php
endwhile;/*end eriku while have posts*/
 
?>
    </div><!-- End three Div -->
 
 
<div id="four">
    <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/benname.jpg"
    <?php
    $my_query = new WP_Query ( 'author_name=ben' );
    while ( $my_query->have_posts () ) :
        $my_query->the_post () ?>
 
        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->
        
            <?php $withcomments = 1; ?>
            <?php comments_template(); ?>
 
        </div><!-- end post div -->    
<?php
endwhile;/*end ben while have posts*/
 
?>
    </div><!-- End four Div -->
 
 
 
<div id="five">
    <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/hingyiname.jpg"
    <?php
    $my_query = new WP_Query ( 'author_name=hingyi' );
    while ( $my_query->have_posts () ) :
        $my_query->the_post () ?>
 
        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->
        
            <?php $withcomments = 1; ?>
            <?php comments_template(); ?>
 
        </div><!-- end post div -->    
<?php
endwhile;/*end hingyi while have posts*/
 
?>
    </div><!-- End five Div -->
 
 
 
 
</div><!-- end box div -->
<?php
get_sidebar ();
get_footer ();
?>
The bit of code I used for each division works in terms of outputting the post, the comments and the comment box to type in new comments. I'm trying to figure out a way to use that bit of old code inside my new code in order to output the comment field, but I'm running into problems with hopping from php to html like you were mentioning in the comments.php file. Any ideas on how to get the index.php file to work with the old code?
Post Reply