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(); ?>
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() ;?>
I am trying to loop out a repeater field in my wordpress front-page template
But for some reason the div is empty and doesn't seem to work.
I am 100% sure my code is correct so there must be an issue.
Anyone has any idea what it could be?
This is how my loop looks like, the field keys are correct! :)
<?php
/**
* Template Name: Front Page Template
*/
?>
<?php while(have_posts()) : the_post(); ?>
<?php if( have_rows('achtergrond_afbeeldingen') ): ?>
<div class="slider-circles">
<?php while ( have_rows('achtergrond_afbeeldingen') ) : the_row(); ?>
<p id="slide1" data-bgimage="<?php the_sub_field('image'); ?>" class="transparent-circle slick-active"></p>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
This is what my front-page.php looks like. The funny thing I've used this in an other project before and everything worked fine. Now my screen is just blank, I have no idea what's going on.
if have_rows('field_56e7d8bebb545') is really true, debug the_row();
var_dump(the_row());
You can view the_result, if is empty, maybe you must set a second parameter for have_row($field_name, $post_id)
field_56e7d8bebb545 is the field key, not the field name. The field name must be used in the have_rows() function. This can be found on the Advanced Custom Fields screen next to the label -
Try this one .. since you have not printed any such thing that can be displayed in browser .. you only provided the data attribute to div.
Write something inside inner HTML might see you repeater values
<?php
/**
* Template Name: Front Page Template
*/
?>
<?php while(have_posts()) : the_post(); ?>
<?php if( have_rows('achtergrond_afbeeldingen') ): ?>
<div class="slider-circles">
<?php while ( have_rows('achtergrond_afbeeldingen') ) : the_row(); ?>
<p id="slide1" data-bgimage="<?php the_sub_field('image'); ?>" class="transparent-circle slick-active"><?php the_sub_field('image'); ?></p>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
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();
The Problem:
My menu bar contains a set of categories. Posts automatically align. A new Plugin is based on Pages. The Page e.g. 'p1' is always on the main site.
The Idea:
Create a template that assigns a page to a specific category e.g. 'p1' -> 'c1'. I found a piece of code
<?php if (is_category('c1')) : ?>
The problem is that I don't know how to tell the program:
if (is_category('c1')) : show page / vice versa?>
How do I do that?
You will want a naming standard between pages and categories. Once that is done you can do the following (or something like it):
<?php if(is_category('c1)) : ?>
<?php query_posts('category_name=c1&order=asc');
if ( have_posts() ) : while( have_posts() ) : the_post();?>
<div class="pageWrapper">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; else: ?>
<p>No content was found</p>
<?php
endif;
wp_reset_query();//If writing your own queries, always a good idea to reset it. ?>
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!