[Wordpress] Wrong post_id when posting comments

Discussion in 'PHP' started by jaanFX, Oct 9, 2009.

  1. #1
    Hi guys,

    Hopefully someone here can help me with this problem I am having...
    I am using the below PHP code to grab all images associated with the post on the single.php to create a gallery and showing them my own slideshow using jQuery.. everything works fine except if you leave a comment, its being posted to a different post_id.
    
    <?php
    			$images = get_children(
    				array(
    				'post_parent' => $post->ID,
    				'post_status' => 'inherit',
    				'post_type' => 'attachment',
    				'post_mime_type' => 'image',
    				'order' => 'ASC',
    				'orderby' => 'menu_order'
    				)
    			);
    		?>
    				</div>
    				<div class='photoessay_nav'>
    					<div class="meta">
    					<span id="currentphoto">1</span>/<?= count($images); ?>
    					</div>
    				
    					<a href='#' onclick='pe_previous();return false;'>previous</a> | <a href='#' onclick='pe_next();return false;'>next</a>
    				</div>
    		
    		<?php
    
    			if ( $images ) 
    			{
    				$i = 0;
    				foreach ( $images as $id => $image ) 
    				{
    					$img = wp_get_attachment_url( $image->ID );
    					$link = get_permalink( $post->ID );
    					$caption = $image->post_excerpt;
    			
    					if ($i != 0)
    						$style = "style='display: none;'";
    					else 
    						$style = "";
    			
    					$caps .= "<li id='c_$i' $style class='photoessay_caption'>$caption</li>";
    					$essay .= "<a href='#' id='i_$i' $style class='image' ><div class='img' style=\"height:487px;width:730px;background: transparent url('$img') no-repeat 50% center;  \">photo</div></a>";
    					$i++;
    				}
    			}
    		
    		
    			echo "<div class='photoessay_photos'>
    			$essay
    			<div class='clearfix'></div>
    		
    			</div>
    			<div class='photoessay_captions'>
    				<ul>$caps</ul>
    		
    			</div>";
    		
    		?>
    
    PHP:
    Now say this was post_id: 605 and there are 4 images in the gallery, if you view the source of the post:
    <input type='hidden' name='comment_post_ID' value='610' id='comment_post_ID' />
    HTML:
    that post_id belongs to the last image of the gallery, I want the comments to get posted to the main post which post_id is 605 and not 610..

    Anyone know this is doing that?

    I have tried doing this:
    echo $post->ID; 
    PHP:
    on the single.php it displays the number 605, the problem is comments.php somehow posting the comments to number 610 instead..

    Thanks in advanced.
     
    jaanFX, Oct 9, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    shouldn't post_status be set to publish?
     
    JAY6390, Oct 10, 2009 IP
  3. jaanFX

    jaanFX Well-Known Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    they are all already published...
     
    jaanFX, Oct 10, 2009 IP
  4. jaanFX

    jaanFX Well-Known Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    this is rather a strange problem, i've fixed part of the problem by doing the following:
    I added this
    global $wp_query;
    $thePostID = $wp_query->post->ID;
    PHP:
    so now i've got the post_id, then on the comments area doing this manually:
    <input type='hidden' name='comment_post_ID' value='<?php echo $thePostID;?>' id='comment_post_ID' />
    PHP:
    which shows the id as 506, which is what i wanted, now if i post the comment it does get posted for post #506..

    another problem i've faced now is that viewing single post does not show the comment but on the front page it does show the comment where i am just displaying the first attached image along with its description... it shows the actual comment

    Here is the comments php part:
    
    <?php
    if ( have_comments() ) : ?>
    	<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3>
    
    	<div class="navigation">
    		<div class="alignleft"><?php previous_comments_link() ?></div>
    		<div class="alignright"><?php next_comments_link() ?></div>
    	</div>
    
    	<ol class="commentlist">
    		
    		<?php foreach ($comments as $comment) : ?>
    		<hr style="height:1px;width:100%;color:#999;" />
    		<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
    		<?php //echo get_avatar( $comment, 32 ); ?>
    		<cite><?php comment_author_link() ?>, <?= $comment->extra_country; ?></cite>
    		<br />		
    		<small class="commentmetadata">
    			<a href="#comment-<?php comment_ID() ?>" title="">
    			<?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a> 
    			
    			<?php edit_comment_link('edit',' ',''); ?>
    			
    			</small>
    		<?php if ($comment->comment_approved == '0') : ?>
    		<br/><em>Your comment is awaiting moderation.</em>
    		<?php endif; ?>
    		
    		<?php comment_text() ?>
    		
    		</li>
    		
    		<?php
    		/* Changes every other comment to a different class */
    		$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
    		?>
    		
    		<?php endforeach; /* end for each comment */ ?>
    
    	</ol>
    
    	<div class="navigation">
    		<div class="alignleft"><?php previous_comments_link() ?></div>
    		<div class="alignright"><?php next_comments_link() ?></div>
    	</div>
     <?php else : // this is displayed if there are no comments so far ?>
    
    	<?php if ('open' == $post->comment_status) : ?>
    		<!-- If comments are open, but there are no comments. -->
    
    	 <?php else : // comments are closed ?>
    		<!-- If comments are closed. -->
    		<p class="nocomments">Comments are closed.</p>
    
    	<?php endif; ?>
    <?php endif; ?>
    
    
    <?php if ('open' == $post->comment_status) : ?>
    
    <div id="respond">
    
    <h3 class="comment_form_title"><?php comment_form_title( 'Send Feedback', 'Send Feedback to %s' ); ?></h3>
    
    <div class="cancel-comment-reply">
    	<small><?php cancel_comment_reply_link(); ?></small>
    </div>
    
    <?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 echo urlencode(get_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 wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>
    
    <?php else : ?>
    
    <p><input type="text" class="required" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
    <label for="author">Name</label></p>
    
    <p><input type="text" class="required" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
    <label for="email">Email (will not be published)</label></p>
    
    <p><input type="text" name="country" id="country" value="" size="22" tabindex="3"  />
    <label for="country">Country</label></p>
    
    <?php endif; ?>
    
    <!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->
    
    <p><textarea name="comment" class="required" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
    
    <p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
    
    <script type="text/javascript">
    	$(function()
    	{
    		$(".required").change(function()
    		{
    			if ($(this).val().length == 0)
    			{
    				$(this).css("border","1px solid red");
    			}		
    			else
    				$(this).css("border","1px solid #CCC");
    		});
    		$("#submit").click(function()
    		{
    			var doSubmit = true;
    			$(".required").each(function()
    			{
    				if ($(this).val().length == 0)
    				{
    					$(this).css("border","1px solid red");
    					doSubmit = false;
    				}
    				else
    					$(this).css("border","1px solid #CCC");
    
    				
    			});
    			if (!doSubmit)
    				alert("Please fill all the required fields.");
    			return doSubmit;
    			
    		});
    	});
    </script>
    <input type='hidden' name='comment_post_ID' value='<?php echo $thePostID;?>' id='comment_post_ID' />
    </p>
    <?php do_action('comment_form', $post->ID); ?>
    
    </form>
    
    <?php endif; // If registration required and not logged in ?>
    </div>
    <?php endif; // if you delete this the sky will fall on your head ?>
    PHP:
    Do you know what I am doing wrong?
     
    jaanFX, Oct 10, 2009 IP
  5. jaanFX

    jaanFX Well-Known Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    What's happening is the comments are getting posted to the last attachment (images) of that post.. now how can I use the comments.php to pull all the comments (including the comments from attachment) into the main post?
     
    jaanFX, Oct 10, 2009 IP