Sometimes, WordPress developers may need custom loops that cannot be generated using standard loops (such as category, author, index, date, archive, taxonomy, etc.). One of them is "Posts I commented on” >". In Q&A sites it means "questions I answered", so many developers may need it. Building on this tutorial, we can create another custom loop. Let's create this section.
Go to your theme folder and create a myanswers.php
file and copy and paste the following code into it:
<?php /* Template Name: myanswers */ ?> <?php get_header(); ?> <?php get_template_part( 'loop', 'myanswers' ); ?> <?php get_sidebar(); ?> <?php get_footer(); ?>
We just created a template file named myanswers. It will be used to display our custom loop.
Stay in your theme folder and create a second file called loop-myanswers.php. and paste the following code into the file:
<?php if($wp_query->query_vars['paged']==0){$wp_query->query_vars['paged']=1;} $querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts inner join $wpdb->comments on $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE $wpdb->posts.post_status='publish' and $wpdb->comments.user_id=".wp_get_current_user()->ID." GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.post_date DESC "; $lim_per_page=" limit ".($wp_query->query_vars["posts_per_page"]*($wp_query->query_vars['paged']-1)).",".$wp_query->query_vars["posts_per_page"]; $query_for_count = $wpdb->get_results($querystr, OBJECT); $wp_query->max_num_pages=ceil($wpdb->num_rows/$wp_query->query_vars["posts_per_page"]); $querystr=$querystr.$lim_per_page; $pageposts = $wpdb->get_results($querystr, OBJECT); ?> <?php if ($pageposts): ?> <?php global $post; ?> <?php foreach ($pageposts as $post): ?> <?php setup_postdata($post); ?> <?php /// THIS PART IS LOOP FROM TWENTYTEN, YOU CAN CHANGE IT HOWEVER YOU WANT BEGIN ?> <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2> <div class="entry-meta"> <?php twentyten_posted_on(); ?> </div><!-- .entry-meta --> <?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?> <div class="entry-summary"> <?php the_excerpt(); ?> </div><!-- .entry-summary --> <?php else : ?> <div class="entry-content"> <?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyten' ) ); ?> <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?> </div><!-- .entry-content --> <?php endif; ?> <div class="entry-utility"> <?php if ( count( get_the_category() ) ) : ?> <span class="cat-links"> <?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?> </span> <span class="meta-sep">|</span> <?php endif; ?> <?php $tags_list = get_the_tag_list( '', ', ' ); if ( $tags_list ): ?> <span class="tag-links"> <?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?> </span> <span class="meta-sep">|</span> <?php endif; ?> <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></span> <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?> </div><!-- .entry-utility --> </div><!-- #post-## --> <?php endforeach; /// THIS PART IS LOOP FROM TWENTYTEN, YOU CAN CHANGE IT HOWEVER YOU WANT END ?> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php include (TEMPLATEPATH . "/search.php"); ?> <?php endif; ?> <?php echo $wp_query->max_num_pages; if ( $wp_query->max_num_pages > 1 ) : ?> <div id="nav-below" class="navigation"> <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', 'twentyten' ) ); ?></div> <div class="nav-next"><?php previous_posts_link( __( 'Next<span class="meta-nav">→</span>', 'twentyten' ) ); ?></div> </div><!-- #nav-below --> <?php endif; ?>
This file is used to generate our custom loop in the template file.
Open your theme's theme functions file (functions.php) and add this function and filter to the file:
add_filter('query_vars', 'parameter_queryvars' ); function parameter_queryvars( $qvars ) { /* Plugin Name: Parameter Plugin URI: https://webopius.com/ Description: A plugin to allow parameters to be passed in the URL and recognized by WordPress Author: Adam Boyse Version: 1.0 Author URI: http://www.webopius.com/ */ $qvars[] = 'paged'; return $qvars; }
This pair of functions and filters is used to obtain the page ID required to build pagination in a permalink structured site.
Finally, go to your dashboard, create new page from Pages -> Add new page and name it "myanswers", by default its slug will be myanswers. Select a template for this page before publishing. In the template widget, you will see a combo box containing the myanswers option. Select it.
After selecting the myanswers option, click the Publish button.
You can now use yoursite.com/myanswers url as the page that displays the "Posts you commented on" loop. Of course, not just you, every logged in user can see theirs.
The above is the detailed content of Create a personalized loop for My Comment Posts. For more information, please follow other related articles on the PHP Chinese website!