I've been trying to make a single sticky post display on the second (or third etc) position instead of the default which is the first.
I have been playing with the loop for hours but I always run into problems.
Here is what I have done so far:
<!-- Wordpress Main Loop Start -->
<?php query_posts(array("post__not_in" =>get_option("sticky_posts"))); ?>
<?php if ( have_posts() ) : $loop_count = 0; while ( have_posts() ) : the_post(); $loop_count++; ?>
<div <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<div class="byline postinfo">by <?php the_author_posts_link(); ?></div>
<div class="postdate postinfo"><?php the_time('l F d, Y'); ?></div>
<div class="commentscounter postinfo"><?php comments_number('0', '1', '%'); ?></div>
<?php
if ($loop_count == 2) :
$sticky = get_option( 'sticky_posts' );
query_posts( 'p=' . $sticky[0] );
wp_reset_query();
else:
the_excerpt('Read More...');
endif;
?>
</div>
<?php endwhile; else: ?>
<p><?php _e('No posts were found. Sorry!'); ?></p>
<?php endif; ?>
<div class="navi">
<div class="right">
<?php previous_posts_link('Previous'); ?> / <?php next_posts_link('Next'); ?>
</div>
</div>
<!-- Wordpress Main Loop End -->
What I have done is this:
I used query_posts() to filter out all sticky posts from the loop.
Used the loop to get the posts and a counter which increments everytime a post is fetched.
On the div which displays the fetched post, I added an if statement which checks if the counter is a specific number and if it is, it rewrites the query_posts and gets the sticky one. If not, it displays the fetched post.
I then reset the query and the loop continues as it should (almost).
The problem is, after the count reaches the value I want, the loop instead of continuing from the last post, it start from the beginning.
If I remove the wp_reset_query() function, the loop just stops when the sticky post is found.
If I try to rewrite the query to include all posts, I get infinite looping because I'm already inside the loop.
I don't want to break the loop in two or three so if anyone can help fix this using one loop, that would be great!
Related
I'm using a basic wordpress loop to show 3 blog posts + pagination, using query_posts to loop these - code attached. The loop and pagination work well, but it's showing a strange glitch: the full page content (for all 3 posts) is also looped and displayed. The full content appears after the pagination and after the endwhile closing tag, at the end of all code.
My suspicion is that the loop is somehow not closed, but any closing tags I know to check are already present. My php is still early beginner level and mostly focused on standard wordpress functions, so any suggestions how I can close this up would be welcome.
Note: the loop is using query_posts instead of new WP_Query because of the pagination, this did not work using any new WP_Query loops. I want to get rid of the additional content but keep the base loop the same so the pagination is still functional.
Final note: I have verified the extra content is caused by this loop as removing the loop also removed the content.
<article class="row">
<?php query_posts(array('posts_per_page' => 3, 'post_type' => 'post', 'paged' => get_query_var('page')));
if (have_posts()) : while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<span class="inline-button">Read more <i class="fas fa-chevron-right"></i></span>
</a>
<?php endwhile; ?>
<?php endif;?>
<?php wp_reset_postdata(); ?>
</article>
<article class="row">
<?php the_posts_pagination(); ?>
</article>
<!------ in html, all extra generated content will appear here below the above article --->
Resolved!
I found the answer and simply closed it adding this to the end:
<?php wp_reset_query() ;?>
Im trying to display data via a loop using ACF[advance custom fields, Option Page] in my Footer.
This works on my homepage without any problems. But if i navigate to sub-pages it breaks , meaning it would show maybe only the page_links row or on another page only the images_footer row.
It confuses me because home page works perfectly normal and shows all footer data from below.
Also i looked if there is any conflict in the custom-fields names towards the sub-pages custom-fields names and the footer but i dont find any.
Another thing if i would remove the first loop the second one works this is only on some pages, so weird.
If i keep the first while loop ,delete the second while loop, and navigate to other pages,the first while loop show on some pages and some not, how is this even possible.
*I have done some var_dumps(); after both if statements, but obviously the dump does not. So it doesnt even pick up the row -> ('page_links', 'option') or in some cases ('images_footer', 'option').
Any helpful tips would be much appreciated.
<div class="col-lg-8">
<div class="links">
<?php
if( have_rows('page_links', 'option') ):
while( have_rows('page_links', 'option') ): the_row();
?>
<p><?php the_sub_field('footer_link_name'); ?></p>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="foot-images">
<?php
if( have_rows('images_footer', 'option') ):
while( have_rows('images_footer', 'option') ): the_row();
?>
<div><img class="img-fluid" src="<?php the_sub_field('logo_image'); ?>" alt=""></div>
<?php endwhile;endif; ?>
</div>
</div>
Following this documentation: ACF - Get values from an options page
I am writing blog for Wordpress using Bootstrap grid. My first post is sticky, 100% width of column. Next posts should be placed undearneath it in two columns.
Every two posts are wrapped in div with .row class to keep them in line. I've done this and it worked perfectly.
<div class="col-md-8">
<div class="top-news">
// sticky post
</div>
<div class="next-news-grid">
<?php while ( have_posts() ) : the_post(); ?>
<?php if(is_sticky()) continue; ?>
<?php if( $wp_query->current_post%2 != 0 ) echo "\n".'<div class="row">'."\n"; ?>
<div class="col-md-6 next-news">
<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('next- news-thumbnail'); ?>
<?php } ?>
<h4><?php the_title(); ?></h4>
<hr>
<?php the_content( "read more" ); ?>
</div>
<?php if( $wp_query->current_post%2 != 1 || $wp_query->current_post == $wp_query->post_count-1 ) echo '</div> <!--/.row-->'."\n"; ?>
<?php endwhile; ?>
</div>
</div>
But I want to use ajax-load-more plugin to automatically load posts on the same page after button is clicked. Unfortunately it won't wrap my posts in .row class anymore.
I copied my while loop into repeater template in plugin settings but it seems like it's ignoring the if statement parts with div appending. Therefore it looks ugly when one post is longer and is pushing another to the right AND another down making huge gaps between them.
So how can I wrap every two posts in div.row container with this plugin? Or can I use something else to achieve infinite loading with button?
I'm using a WP_Query in a WordPress loop and for some reason I can not get the "else" portion to work. Basically I'm looking for posts in the category of 59. If there are none I want to display some text. The loop works fine when any posts in the category are present, but if there are none, nothing shows up. Can't seem to figure out why this isn't working. Here's my code. Any help is much appreciated!
<?php
//The Query
$custom_posts = new WP_Query();
$custom_posts->query('cat=59');
//The Loop
if ( have_posts() ) : while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
<article>
<div class="thenews">
<h1><?php the_title(); ?></h1>
<h2>Posted on: <?php the_time('F jS, Y') ?></h2>
<?php the_excerpt(); ?>
</div><!-- thenews div ENDS here -->
<div class="clearfloats"><!-- clears the elements above --></div>
</article>
<?php endwhile; else: ?>
<article>
<div class="thenews">
<p>Nothing here to see.</p>
</div><!-- thenews div ENDS here -->
<div class="clearfloats"><!-- clears the elements above --></div>
</article>
<? endif;
//Reset Query
wp_reset_query();
?>
You're not using the have_posts method of your custom loop which is why else isn't firing.
Change:
//The Loop
if ( have_posts() ) : while ($custom_posts->have_posts()) : $custom_posts->the_post();
To:
//The Loop
if ( $custom_posts->have_posts() ) : while ($custom_posts->have_posts()) : $custom_posts->the_post();
I'm trying to figure out how to display a custom loop with only certain elements in a wordpress loop.
My loop currently contains the title, creation date, author & content.
However I am trying to remove the content for each post in the loop as I do not want it displayed in this list with no luck. I have even removed the_content() out of the loop & it still displays a listing of the post content under the looped section.
Any help would be greatly appreciated,
here is my code:
<?php query_posts('category_name=halloween &posts_per_page=6'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- IF articles then display them -->
<h6><?php the_title(); ?></h6>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<?php endwhile; else: ?>
<!-- IF no articles were created then show -->
NO Posts Present
<?php endif; ?>
I seemed to have found what i was missing in my code as there needed to be the following snippet of code required at the end of my loop which fixed the issue:
here is the code which I added after my endif
<?php wp_reset_query(); ?>