duanbu1421 2012-07-24 12:24
浏览 28

尝试在侧边栏PHP中显示类别特定的注释

I'm trying to display comments in sidebar only from the current category. So far I've got this:

But it does not work, it either displays 0 comments everywhere or all everywhere. I don;t understand what I did wrong...

Problem appears to be this part: // category (can be a parent category) $cat = get_query_var('cat'); $category_parent = $cat;

<?php
    // Posts per page setting
    $ppp = 8; //get_option('posts_per_page'); // either use the WordPress global Posts per page setting or set a custom one like $ppp = 10;
    $custom_offset = 0; // If you are dealing with your custom pagination, then you can calculate the value of this offset using a formula

    // category (can be a parent category)
    $cat = get_query_var('cat');
    $category_parent = $cat;

    // lets fetch sub categories of this category and build an array
    $categories = get_terms( 'category', array( 'child_of' => $category_parent, 'hide_empty' => false ) );
    $category_list =  array( $category_parent );

    foreach( $categories as $term ) {
        $category_list[] = (int) $term->term_id;
    }

    // fetch posts in all those categories
    $posts = get_objects_in_term( $category_list, 'category' );

    $sql = "SELECT comment_ID, comment_date, comment_content, comment_post_ID, user_id, comment_author
     FROM {$wpdb->comments} WHERE
     comment_post_ID in (".implode(',', $posts).") AND comment_approved = 1
     ORDER by comment_date DESC LIMIT $ppp OFFSET $custom_offset";

    $comments_list = $wpdb->get_results( $sql );

    if ( count( $comments_list ) > 0 ) {

        $date_format = get_option( 'date_format' );

        foreach ( $comments_list as $comment ) {
?>

            <li>
                <a href="<?php echo ( get_permalink( $comment->comment_post_ID ) ); ?>#comment-<?php echo($comment->comment_ID); ?>"><?php echo get_avatar( $comment->user_id, 50 ); ?></a>
                <span><strong><?php echo($comment->comment_author); ?></strong> commented on</span>
                <h3><a href="<?php echo ( get_permalink( $comment->comment_post_ID ) ); ?>"><?php echo get_the_title ( $comment->comment_post_ID ); ?></a></h3>
                <span><?php echo($comment->comment_date); ?></span>
                <p>"<?php comment_excerpt(); ?>" <a href="<?php echo ( get_permalink( $comment->comment_post_ID ) ); ?>#comment-<?php echo($comment->comment_ID); ?>">Read More</a></p>
                <div class="clearfloat"></div><!-- Very Important -->
            </li>

<?php
        }

    } else {
        echo '<p>No comments</p>';
    }
?>
  • 写回答

1条回答 默认 最新

  • dqtok88424 2012-07-24 14:27
    关注

    WordPress has functions available that can query most of the things you need. For example, there is a function called get_the_category() which will include the current category and its children/ancestors.

    Reference:


    <?php
    
      $ppp = 8;
      $custom_offset = 0;
    
      // get the current category (includes children/parents)
      $categories = get_the_category();
    
      // create an array that only has each category_id
      $category_ids = array_map(function($category) {
        return $category->cat_ID;
      }, $categories);
    
      // find all posts by the category ids
      $posts = query_posts('cat=' . implode(',', $category_ids));
    
      // create an array that only has each post_id
      $post_ids = array_map(function($post) {
        return $post->ID;
      }, $posts);
    
      // find all comments by post_ids
      $sql = "SELECT comment_ID, comment_date, comment_content, comment_post_ID, user_id, comment_author
           FROM {$wpdb->comments} WHERE
           comment_post_ID in (".implode(',', $post_ids).") AND comment_approved = 1
           ORDER by comment_date DESC LIMIT $ppp OFFSET $custom_offset";
    
      $comments = $wpdb->get_results( $sql );
    
      if( $comments ) {
        // do stuff with $comments
      }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序