Show Posts from Single Category Not Working

Take a look the section called Preserving Existing Query Parameters on http://codex.wordpress.org/Function_Reference/query_posts Try merging in the existing query parameters; they might be getting overridden. Instead of: <?php query_posts(‘cat=4’); ?> try global $query_string; query_posts( $query_string . ‘&cat=4’ );

Sort posts by first letter in the title, not by first number

This is a bit messy to do in MySQL, given the lack of a preg_replace – the following uses a nested REPLACE to strip all numbers from the title: function wpse167989_posts_orderby( $orderby, $query ) { global $wpdb; // Strip all numbers from title, and trim any leading spaces. return $wpdb->prepare( ‘LTRIM(‘ . str_repeat( ‘REPLACE(‘, 10 … Read more

Scrolling Posts with Ajax

Well first of all, that tutorial is “doing it wrong” in the AJAX department. WordPress has an AJAX API, which should be used in this case. That said, the basis of your problem is this: $args = array( ‘posts_per_page’ => $numPosts, ‘paged’ => $page ); Those are your query arguments for the posts you’re requesting. … Read more

Need to add class to first element in a foreach loop

Use below code <?php $isFirst = true; $i=1; foreach ( $attachments as $attachment ): $description = $attachment->post_content; if($i==1) { echo ‘<div class=”item’.$isFirst.'” data-description=”‘ . $description . ‘”>’; echo wp_get_attachment_image($attachment->ID, ‘large’); echo ‘</div>’; } else { echo ‘<div class=”item” data-description=”‘ . $description . ‘”>’; echo wp_get_attachment_image($attachment->ID, ‘large’); echo ‘</div>’; } $i++; endforeach; ?>

Trying to retrieve random post, getting a page

You need to specify the post type to be a post – try this for your $args instead: $args = array( ‘post_type’ => ‘post’, // Specifying you want posts only ‘posts_per_page’ => 1, ‘orderby’ => ‘rand’, ‘post_status’ => ‘publish’, ); And most importantly, you need to apply the $args to your query, so do this … Read more

Give each posts in a loop a number in sequence

As this is a presentational thing, I’d be inclined to use CSS (there are of course ways of doing it in PHP, and others may disagree.) You’d make use of the counter-reset and counter-increment properties. CSS counters can be applied to any element, not just ordered-lists (<ol></ol>) and added using pseudo-elements and you can control … Read more