New answers tagged

1

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 ...


0

I figured it out: <?php $args = array( 'post_type' => 'faq'); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); ?> <div class="faq-wrap"> <div class="q-wrap"> <h3>&...


-1

I found out what was causing this problem. I had a plugin installed called Simple Posts Generator which I used to generate 10 sample posts for testing purposes. Since I generated the articles all at the same time, the post_date value in the database was the same for each post, which confused Wordpress as to what post should come first. I fixed it by going ...


0

You need to create a variable with the count and then check against it e.g. $num = 1; foreach ( $terms as $term ) { if($num%2) { echo '<div class="something">'; } // other stuff if($num %2) { echo '</div>'; } $num++ } The %2 is the part you need, it checks whether the number is dividable by 2


0

Unit testing plugin functions in Wordpress is extremely difficult. It's usually easier to do an acceptance or functional test. The difference is that in a unit test, you have to mock everything you interact with. In a functional test, you can run the whole system. For instance, I use Codeception to launch a browser and test against the whole system. Opinions ...


0

I also don't understand what rwmb_meta() does, but I can see a couple obvious problems in the code as written. Apparently, you want the foreach to deliver as many "posts" as it finds $image in $images, and echo a result each time. If so, you would remove the bracket from: $schedule_image_url = $image['url']; } and place it after each $image ...


0

I don't know what rwmb_meta() does. But if rwmb_meta('tbf_scheduledatetime') returns array, then try something like this: ... $images = rwmb_meta( 'tbf_scheduleimage', 'type=image&size=full'); $dates = rwmb_meta('tbf_scheduledatetime'); var_dump( $dates ); ... You'll probably want to loop thru $dates and repeat the post data for each element in $...


0

Just hit the same thing today, but I found a great solution: <?php if ( is_front_page() ) { echo "Home"; } else if (is_single()) { // POST, not needed for my site echo ""; } else if (is_page()) { // PAGE echo get_the_title(); } else if(is_category()) { $arr = get_the_category(); if ( ! empty( $arr ) ) { ...


2

This is purely PHP which is considered to be Off-topic but this is my take: <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); $i = $wp_query->current_post; echo ( 0 == $i % 3 ) ? '<div class="row">' : ''; ?> <div class="project"> </div&...


0

Something like this should work: <?php if ( have_posts() ) { $i = 1; // Always start the list with a div.row echo( '<div class="row">' ); /* Start the Loop */ while ( have_posts() ) { the_post(); if( $i%3 == 0 && $i != 1 ) { echo '</div>' . PHP_EOL . '<div class="row">'; ...


1

To use AJAX in a WordPress website you can the admin-ajax.php file. This file handles your requests and sends it to the correct function. A short step-by-step guide on how to achieve: First you need to localize your javascript file so that it knows the URL of the admin-ajax.php file on your server. add_action( 'wp_enqueue_scripts', 'wpse_229573_localize' )...


0

You don't need to alter the code for different formatting, you can simply use the :first-child pseudoclass in css. The foreach loop will, presumably, generate similar blocks of code, structured like this: <div class="all-my-posts"> <article class="my-slider-post"> ... </article> <article ..> </article> ....


1

WordPress runs a filter on the content to automatically add paragraphs: wpautop. You can remove the filter from all your post's content if you like: remove_filter('the_content','wpautop'); Alternatively, you can check out the Disabler plugin which can do this for you. If you want to remove the filter from particular post(s) you can do: add_filter('...


3

get_the_content and the_author have to be in a loop so you would need to use other functions to get the content you need global $wp_query; $post = $wp_query->post; $page_id = $post->ID; // page ID $page_object = get_page( $page_id ); // page stuff $author_id = $post->post_author; // author ID $page_content = $page_object->post_content; $...


1

The p parameter accepts a single integer value, not a string or array of integers. You would want to use the post__in parameter which allows for an array of post id's. Just a few notes here: You will need to check if you actually have a valid array of post ID's before passing it to post__in. If an empty array is passed, WP_Query fails epically in that it ...


0

I've finally solved this problem. I'm copying the code because it may help someone :) I've found the solution by populating an arrray and checking if a brand is or is not there, so I only get each brand one time. <?php //Query to match department $args = array( 'tax_query' => array( array( 'taxonomy' => '...


0

In your loop while(have_posts() ) : the_post(); you are unconditionally going through all posts, but since the loop stops after the_post() nothing actually happens. So that's where the condition has to go: while(have_posts() ) { the_post(); if ($daysleft >= 0) { ... other actions } } Your code looks a bit incomplete to me, ...


0

I got it resolved by adding 2 loops after each other. First loop display users with 'role__in' => 'sticky' while second loop display users with 'role__not_in' => 'sticky' It worked perfectly. Here is the code if it helps anyone. function Profiles() { echo "<div class='users-list'>" // First loop with sticky users $args = array( ...


0

If you index $related_posts by position along with $ad_posts, you can probably splice/insert them in at the same position... eg. function wpse_210493_apply_advertising_position(&$posts, $return = false) { $ad_posts = array(); $content_posts = array(); $related_post = array(); // Seperate $posts into "Ads" and "Content" arrays based on ...


0

If you read the code (in version 4.5.2 on line 164-173) you will see that orderby can be an array of values, coupled with order. So you could modify $args like this: $args = array( 'orderby' => array( array ('sticky' => 'DESC'), array ('registered' => 'DESC')), 'fields' => 'all_with_meta', ); ...



Top 50 recent answers are included