Cleanest Way to Select Every Second Element in a Loop?

I wouldn’t consider that strange :). WordPress uses a similar method to apply the ‘alternate’ class to every other row in the tables on the admin page, something like: <ul> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php $class = (empty($class) ? ‘class=”alternate”‘ : ”);?> <li <?php echo $class; ?> ><?php the_content(); ?></li> … Read more

List all posts commented by current user

If we want to avoid any filters and manual SQL queries, we could try (untested): $args = array( ‘post_type’ => ‘foods’, ‘posts_per_page’ => 5, ‘post__in’ => array_unique( wp_list_pluck( get_comments( array( ‘user_id’ => get_current_user_id() ) ), ‘comment_post_ID’ ) ), ); $my_query = new WP_Query( $args );

Display Authors Comments on Profile Page

What you need to use here is the WP_Comment_Query() function. So on the author.php page, you can easily get the author info and ID as followed: // get author info $curauth = (isset($_GET[‘author_name’])) ? get_user_by(‘slug’, $author_name) : get_userdata(intval($author)); // set ID $user_id = $curauth->ID; Then we add the user ID in the query arguments array: … Read more

Using the Loop to show all levels of subpages under a parent page? Halfway there

Sounds like you’re looking for a recursive function, ie a function that calls itself. Here’s a rough outline of how it can be done: function wpse13669_show_all_children( $post_id, $current_level ) { $children = get_posts( array( ‘post_type’ =>’page’, ‘posts_per_page’ =>-1, ‘post_parent’ => $post_id, ‘order_by’ => ‘title’, ‘order’ => ‘ASC’ ) ); if ( empty($children) ) return; echo … Read more

How to return loop contents

There are replacements that return pure strings for all parts, no need to print anything into an output buffer. I like sprintf() and would write your example like this: <?php if ( $cms_pl_pages->have_posts() ) { $content=”<section class=”cms-pl-gallery”>”; while ( $cms_pl_pages->have_posts() ) { $cms_pl_pages->the_post(); $content .= sprintf( ‘<article class=”cms-pl-item clearfix”> %1$s <h2> <a href=”https://wordpress.stackexchange.com/questions/57000/%2$s” title=”Read %3$s”>%4$s</a> … Read more

Display all posts from specific categories on a page

I’d Advise adding the arg of the category in an array. And don’t use query_posts. Also showposts is deprecated use posts_per_page instead. $args = array ( ‘cat’ => array(2,6,9,13), ‘posts_per_page’ => -1, //showposts is deprecated ‘orderby’ => ‘date’ //You can specify more filters to get the data ); $cat_posts = new WP_query($args); if ($cat_posts->have_posts()) : … Read more

Output before and after the loop

The function the_post() (source) triggers the action loop_start when it is first used in the loop. This is used in every WP_Query loop, so you may want to check if the current query is the ‘main’ one (assuming that’s the query you wish to target) add_action( ‘loop_start’, ‘wpse107113_loop_start’ ); function wpse107113_loop_start( $query ){ if( $query->is_main_query() … Read more

How do I get the category URL from get_the_category?

Use: get_category_link( $category_id ); See: https://codex.wordpress.org/Function_Reference/get_category_link In your specific case: <?php global $post; $categories = get_the_category(); foreach ($categories as $category) : $exclude = get_the_ID(); $posts = get_posts(‘posts_per_page=4&category=’. $category->term_id); foreach($posts as $post) : if( $exclude != get_the_ID() ) { ?> <a href=”https://wordpress.stackexchange.com/questions/219954/<?php the_permalink(); ?>” title=”<?php the_title(); ?>” class=”post c-1″> Link to actual post</a> <?php } endforeach; … Read more

Use WP_Query with have_posts()?

global $wp_query; $original_query = $wp_query; $wp_query = null; $wp_query = new WP_Query( $args ); if ( have_posts() ) : while ( have_posts() ) : the_post(); the_title(); the_excerpt(); endwhile; else: echo ‘no posts found’; endif; $wp_query = null; $wp_query = $original_query; wp_reset_postdata(); http://codex.wordpress.org/Function_Reference/wp_reset_postdata

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)