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; ?>

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

How to exclude or filter password protected posts when using next_post_link() previous_post_link

The function next_post_link() allow excludes, with the parameter excluded_terms. Add a list of the IDs of the password protected posts to this param. You get a list of all password protected posts with the follow example via DB select. $password_pages = $wpdb->get_col(“SELECT ID FROM $wpdb->posts WHERE post_status=”publish” AND post_type=”post” AND post_password !=””); Alternative can you … Read more

While loop with bootstrap [closed]

Try using the count variable to increment the loop. <div class=”panel-group” id=”accordion” role=”tablist” aria-multiselectable=”true”> <?php if(get_field(‘faq_general_ht’)): ?> <?php $count=0; ?> <?php while(has_sub_field(‘faq_general_ht’)): ?> <div class=”panel panel-default”> <div class=”panel-heading” role=”tab” id=”headingTwo”> <h4 class=”panel-title”> <?php echo ‘<a data-toggle=”collapse” data-parent=”#accordion” href=”#collapse’ .$count. ‘”aria-expanded=”true”>’ ?> <p><?php the_sub_field(‘faq_general_question_ht’) ?></p> </a> </h4> </div> <?php echo ‘<div id=”collapse’ .$count. ‘” class=”panel-collapse collapse” … Read more

How to check if a product is purchased within the last 365 days before displaying something

This is not that complicated, here is a code snippet that will get you started: if(is_user_logged_in()) { //We only need these products to check $products_to_check = array(81,82,83,84); $customer_bought = array(); //Get all orders made by the current user in the last 365 days $customer_orders = get_posts( apply_filters( ‘woocommerce_my_account_my_orders_query’, array( ‘numberposts’ => $order_count, ‘meta_key’ => ‘_customer_user’, … Read more

Make loop alphabetical

You have to alter the query and add the orderby argument specified as name. I would go for the pre_get_posts action (see documentation) with some conditional tags to select the query for the right page. For example: function change_posts_order($query) { if($query->is_home() && $query->is_main_query()) { // edit to match the desired page $query->set(‘orderby’, ‘name’); $query->set(‘order’, ‘ASC’); … Read more