New answers tagged

1

You could always try to create 2 arrays of titles, say $left and $right, $odd and $even, or $tom and $jerry, and fill them with the titles during your loop, and print them after the loop has ended like so: Create array $left = $right = array(); Then unleash your loop <?php // The loop if ( $query->have_posts() ) : $row_start = 1; while ( $...


0

Run through the loop with % for the left side first, then rewind the loop and start in on the right side. https://codex.wordpress.org/Function_Reference/rewind_posts


1

get_template_part() calls locate_template(), and both functions are running file_exists() checks for the same file over and over. There is no caching. Meh. But … PHP has an internal cache for file look-ups, so a direct file access will not happen on every call. There is an edge case, most developers aren't aware of: A file can be deleted after the first ...


1

That depends entirely on your code. Without seeing any, i will assume you are using a standard WP loop. If so, the template part is included every time the statement is executed. By default that is for every post in the loop.


-2

use wp_reset_query() after each while loop


0

Looks like you have multiple submit buttons inside a single form. When you submit the form you'll get a submit action for every submit button. Please note: RRikesh's comment is correct, you also seem to be declaring a php function for a JavaScript action. My answer addresses the code you've submitted assuming the other issues are solved.


0

Update: I'm about 90% complete on what I'm trying to accomplish. Everyone is more than welcome to copy this or make changes to the code. Keep in mind that this is a placeholder and it will have more items than the_title(). This is what I've done so far: // The query. Show only posts in a taxonomy called "post-status" that also belong to "post-status-...


1

Up-voting Andy's answer, you should be comparing integers to get this working. strtotime() can turn any string to the time integer, which will be compared later to tell the difference and how many seconds passed until now ( time() ). Something like follows: $expired = ( ( time() - strtotime(get_the_date('Y-m-d H:i:s')) ) / DAY_IN_SECONDS ) >= 5; if ( $...


2

You're comparing strings. For dates this will only ever work out if you use a yyyymmdd format or similar.


0

Consider using $queryObject->current_post to track the loop index. https://codex.wordpress.org/Class_Reference/WP_Query


3

The first use of wp_reset_postdata() is not redundant its unnecessary. Per the codex Use this function to restore the global $post variable of the main query loop after a secondary query loop using new WP_Query. It restores the $post variable to the current post in the main query. So only use it after your secondary query to reset things back for ...


1

You can just run through the loop and set a flag: if ( $query->have_posts() ) : $any_in_cat = false; while ( $query->have_posts() ) : $query->the_post(); if ( in_category( 'first-major' ) ) : $any_in_cat = true; endif; endwhile; $query->rewind_posts(); /* if $any_in_cat == true at this point then ...


1

You can create a separate function to check your array of posts contains a post with that category. It will basically do what you are now but it will allow you to call the function once instead of each time through the loop. You would put this code in your functions.php file: function does_array_contain_category( $categories, $posts ){ foreach( $posts ...


-2

is_category() tests to see if you are displaying a category archive, but you are displaying a single post, so this will always return false in_category() tests to see if a given post has a category in that specific category and must be used in The Loop, but you are trying to figure out what the category is, before you get to the loop. if ( is_single() ) {...


0

As our attempts to help don't seem to have worked, I've taken your code as the question currently shows: <?php /* Template Name: Trends */ get_header(); ?> <section id="top"> <?php the_post(); ?> <div class="intro"><?= get_the_title(); ?></div> <?php the_content();?> <?php $args = array( ...


0

What I see is , you are not following WP coding standard. <?= get_the_title(); ?> is not allowed in WP coding standard. Now you are getting only the post and not as per your args provided, this means you have not reset your main WP Query. We can write the above code as : <?php /* Template Name: Trends */ get_header(); ?> // Primary loop ...


0

You may need to add this to your query_posts() call 'order' => 'ASC' so it should look like this: query_posts( array( 'post__in' => $top_ideas, 'orderby' => 'post__in', 'order' => 'ASC' ) ); Hope this helps!


0

You have this code: elseif (17 < $count && $count <= 27 && $paged === 1) : if ($count === 10) echo 'DIV/CONTENT BEFORE THE NEXT 10 POST GOES HERE'; The first conditional fails when 10 === $count and so the second conditional can never succeed. As your while loop executes (in a structure that WordPress calls The Loop, calling ...


1

Here's a full working code for whomever may need it: $tags_id = wp_get_post_tags($post->ID); $first_tag = $tags_id[0]->term_id; $categories_id = wp_get_post_categories($post->ID); $first_category = $categories_id[0]; $by_tag = new WP_Query( array( 'post__not_in' => array($post->ID), 'tag__in' => $first_tag ) ); if ( $by_tag->...


-1

<?php $args = array( 'post_type' => 'product', /* your Custom post slag */ 'meta_key' => '_wla_homepage_slider_checkbox', 'meta_value' => 'on', 'posts_per_page' => 15, 'no_found_rows' => true ); $query= new WP_Query( $args ); ?>


1

It's a neat idea! The way I would tackle the problem is to create an associative array of post IDs mapped to their individual weights. Since weights are subjective - that is, the represent the value of other posts' relationships with the post currently being displayed - it doesn't make much sense to store these weights in the database each time you process ...


0

This is what i am using for Custom Post Type health and Custom Taxonomy health-category also fixes the pagination. <?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $q = new WP_Query([ 'post_type' => 'product', 'posts_per_page' => get_option('posts_per_page'), get_query_var( 'taxonomy' ) => get_query_var( 'term'...


0

But if the single post page is cached in Varnish, wouldn't the count stay the same? Yes, if the whole page is cached then the PHP will never get run to update the view count. You'd need an ESI block to call an uncached URL that will increment the view count. Or even an Ajax script would work. Just make sure your VCL doesn't cache the the ESI block request ...


0

Did this before for a website. Here's what i did: Checked if there is a category with the same name as the title: if( term_exists(get_the_title(), 'category' )) If there was one then I got the category by slug: $cat = get_term_by("slug", $post->post_name, "category"); Checked if category had parent or was a parent: if($cat->parent !== 0) { ...


0

Inside your parent post/category loop use this to generate a list of post titles/category names of the child posts/categories: $taxonomy_name = 'category'; $this_term = get_term_by( 'name', get_the_title(), $taxonomy_name ); $term_id = $this_term->term_id; $termchildren = get_term_children( $term_id, $taxonomy_name ); echo '<ul>'; foreach ( $...


0

It's a bad use of Query you may look at this post In this case use a secondary query with your own instance of WP Query. query_posts() is not recommanded here cause it overrides the main query. <?php $args = array( 'post_type' => 'instrument', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, ); $my_query = ...


0

Are you sure you want to do this in this manner? Normally a section like this might be written to the page template somehow and not called via the content editor. That being said, this should do what you need: add_shortcode( 'show_xyz_news', 'xyz_news_query' ); function xyz_news_query() { $args = array( 'posts_per_page' => 3, '...


1

REWORKED APPROACH Due to this becoming a bit of tameletjie due to styling issues, lets rework everything and look at a different approach I think what we should do here is to rather shuffle the $posts array through usort() to get the posts sorted in the order we want, and then there after we can run the loop normally Create our query: <?php $args = ...


0

There's some code missing from your question - the code where your slides are actually output. The foreach loop you've posted only sets the variables, and the actual output happens below the loop. So, all you're doing on the fourth slide is avoiding setting the variables... hence the slide is still output, but using the third slide's variables. Your if ...


0

if you take a look in the database in wp_postmeta table you will see meta_key will be _featured and meta_value will be yes or no so instead of value 0 or 1 write yes or no <?php $q = new WP_Query([ 'post_type' => 'product', 'stock' => 1, 'showposts' => 3, 'orderby' => 'date', 'order' ...


-1

Are all the if-statements that output html actually inside the checkbox if-statement? Can't really tell from the way you've posted the code. It looks like maybe on the last trip through the foreach loop, the values of the variables are being remembered from the previous trip and outputting them again??


0

As milo stated, I need to use the post_class function to use the post_class filter. By adding <?php post_class(); ?> to the div in my loop I was then able to successfully modify the post class. Below is my full code containing the post_class filter and the post_class function. Reference: https://codex.wordpress.org/Function_Reference/post_class ...


2

This little snippet could help you. $years = array(); if( have_posts() ) { while( have_posts() ) { the_post(); $year = get_the_date( 'Y' ); if ( ! isset( $years[ $year ] ) ) $years[ $year ] = array(); $years[ $year ][] = array( 'title' => get_the_title(), 'permalink' => get_the_permalink() ); } } This code gets ...


1

You can use get_term_by to get a category by name- $category = get_term_by( 'name', $post_slug, 'category' ); echo $category->term_id;


0

Definitely over thinking it <div class="section group"> <?php printf('<h1>%s</h1>', the_title()); foreach($users as $user) { ?> <div class="author"> <div class="col span_1_of_3"> <div class="authorAvatar"> <?php echo get_avatar( $user-&...


0

I managed to do it. I used the following query parameter: 'tax_query' => array( array('taxonomy' => 'wpccategories', 'field' => 'slug', 'terms' => $termz ) ) With $termz being: $termz = ...


5

You can just remove the filter before calling the_excerpt and then add it back afterwards... remove_filter('excerpt_more','new_excerpt_more'); the_excerpt(); add_filter('excerpt_more', 'new_excerpt_more');


2

If you need to skip the current post, you can make use of the offset parameter. The value passed will the amount of posts to skip, so in your case, you would do 'offset' => 1, to skip the latest post. Just a note on offset, when you add offset to any query, it breaks pagination. WP_Query internally uses offset to calculate pagination. If you need to ...


1

You need the WP_Query documentation. WP_Query is very powerful, and you can do a lot with it :) In your case, I don't think exclude is a valid parameter. What you're probably looking for is this: $query->set( 'post__not_in', array( 34 ) ); Here's the Post & Page Parameters section of the documentation, which covers this and the other parameters ...



Top 50 recent answers are included