• Resolved hbalagh

    (@hbalagh)


    I would like to display all post in my sidebar that have no comments that are in a certain category, ive searched but not finding anything

Viewing 14 replies - 1 through 14 (of 14 total)
  • You’ll need to use a new WP_Query for posts in that category. Then in the loop, use $post->comment_count to test the number of comments. Didn’t test this but it would be something like:

    if ($post->comment_count > 0 ) {
    //display your posts code here
    }
    Thread Starter hbalagh

    (@hbalagh)

    can u explain that a lil more im a lil lost lol

    Okay review The Loop and the The Loop in Action.

    Also here’s a recent example of a loop using get_posts

    https://www.ads-software.com/support/topic/334558

    Also see query_posts() for a list of arguments to restrict query to a particular category.

    Thread Starter hbalagh

    (@hbalagh)

    this is the code that im using to display the last five questions asked, but in another area i want to alter it to show questions unanswered (no comments) if someone can help me i would be very thankful

    <?php query_posts(array('orderby' => 'DESC', 'cat' => '37', 'showposts' => 5));
    
    					if (have_posts()) : while (have_posts()) : the_post(); ?>
    					<li style="font-weight:bold;	list-style:none; font-size:9pt;
    	cursor: pointer;
    	display:block; padding-top:10px;"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php if (3==$post->post_author) {
    ;
    echo ' by BPDF member/guest';
    } else {?>
    <?php } ?></li>
    					<?php endwhile; else : ?>
    					<?php endif; ?>

    After this line

    if (have_posts()) : while (have_posts()) : the_post(); ?>

    add

    <?php if ($post->comment_count > 0 ) { ?>

    and after this line

    <?php } ?></li>

    add

    <?php } ?>

    Thread Starter hbalagh

    (@hbalagh)

    still showing just the regular latest

    This worked in the WordPress Default theme’s sidebar.php

    <?php
        $args=array(
          'showposts'=>5,
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'Recent Posts with comments';
          while ($my_query->have_posts()) : $my_query->the_post();      ?>
    <?php if ($my_query->post->comment_count > 0 ) { ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
           }
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    Thread Starter hbalagh

    (@hbalagh)

    i want it to show post w/o comments

    Oops, this should be:

    <?php if ($my_query->post->comment_count == 0 ) { ?>
    Thread Starter hbalagh

    (@hbalagh)

    its only showing 2 post which are the last two posted and i need it from category 37

    <?php
        $args=array(
          'showposts'=>5,
    	      'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
    
        if( $my_query->have_posts() ) {
          echo 'Recent Posts with comments';
          while ($my_query->have_posts()) : $my_query->the_post();      ?>
    <?php if ($my_query->post->comment_count == 0 ) { ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
           }
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Ah. Have to get all posts for that category, then use a counter to display only 5.

    <?php
    $count=0;
        $args=array(
          'showposts'=> -1,
          'cat' => 37,
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '5 Recent Posts with no comments for category 37';
          while ($my_query->have_posts()) : $my_query->the_post();      ?>
            <?php if ($my_query->post->comment_count == 0 & $count < 5 ) {
            $count++;?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
            }
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    Thread Starter hbalagh

    (@hbalagh)

    thank u so very much ??

    Thread Starter hbalagh

    (@hbalagh)

    here is the page it was for, just if ur curious

    https://www.bpdfriends.com/section/qa/

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘list post in sidebar with no comments in a specific category’ is closed to new replies.