I am trying to alter the number of posts that will display on an events archive page using the Events Organiser plugin. Right now the following code displays only 8 events because that is what the Wordpress query is set to.
If I create a new query and set it to post type="event" it will grab all events and not just from the event category.
How can I alter the code below to be able to make sure it pulls all posts in that event category?
This is a template file so all event categories use this template. I am trying to find an easy solution to alter the number of posts since everything else is populating correctly.
<!-- Start Loop -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="col-md-4 clearfix">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
?>
</a>
<div class="event__details">
<a href="<?php the_permalink(); ?>" class="event__title">
<?php the_title(); ?>
</a>
<?php
if( eo_is_all_day() ){
$format = 'd F Y';
$microformat = 'Y-m-d';
}else{
$format = 'd F Y '.get_option('time_format');
$microformat = 'c';
}?>
<time itemprop="startDate" datetime="<?php eo_the_start($microformat); ?>"> <?php eo_the_start($format); ?></time>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<!-- End Loop -->
create a custom WP_Query check this
<?php
$args = array(
'post_type' => 'event',
'posts_per_page' => 8
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Related
Every time I post a new article I want only the newest post to be filtered and shown on my page.
However, using the code I have got is where I would like to achieve this ideally. So, essentially I will use the code I have got below twice - once to show the newest artcile as a top blog post (that will be styled differently) and then reuse this code to filter all the other posts.
Below is the code for it;
<article>
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<h1 style="text-transform:uppercase;"><?php the_title(); ?></h1>
<h4>Posted on: <?php the_time('F jS, Y') ?></h4>
<p><?php the_content(__('Read More')); ?></p>
<?php endwhile; else: ?>
<p><?php _e('Sorry, there are no posts available at the time.'); ?></p>
<?php endif; ?>
</article>
How can I achieve this, please?
that should be what you're looking for.
<!-- Start most recent post loop here -->
<?php
$query = new wp_query( array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => '1', 'orderby' => 'post_date', 'order' => 'ASC' ) ); if ( $query->have_posts() ): ?>
<?php while ( $query->have_posts() ): $query->the_post(); ?>
<!-- Start post template here -->
<div style="padding:15px;border:1px solid red;margin-bottom:15px;"><a aria-label="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<!-- End post template here -->
<?php endwhile; else: ?>
<?php endif; ?>
<!-- End most recent post loop here -->
<!-- Start all post exept first loop here -->
<?php
$query = new wp_query( array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => '-1', 'orderby' => 'post_date', 'order' => 'ASC', 'offset' => '1' ) ); if ( $query->have_posts() ): ?>
<?php while ( $query->have_posts() ): $query->the_post(); ?>
<!-- Start post template here -->
<div style="padding:15px;border:1px solid grey;margin-bottom:15px;"><a aria-label="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<!-- End post template here -->
<?php endwhile; else: ?>
<?php endif; ?>
<!-- End all post exept first loop here -->
As you want to separately show the latest post on top styled differently and all others below it to be styled differently you can use this.
<article>
<?php $counter = 1; ?>
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<?php if($counter == 1) { ?>
<!-- Use this block to style your top most latest post -->
<h1 style="text-transform:uppercase;"><?php the_title(); ?></h1>
<h4>Posted on: <?php the_time('F jS, Y') ?></h4>
<p><?php the_content(__('Read More')); ?></p>
<?php $counter = $counter + 1; ?>
<!-- Top Block Ends -->
<?php } else { ?>
<!-- Use this block to style other remaining posts -->
<h1 style="text-transform:uppercase;"><?php the_title(); ?></h1>
<h4>Posted on: <?php the_time('F jS, Y') ?></h4>
<p><?php the_content(__('Read More')); ?></p>
<!-- Bottom Block Ends -->
<?php } ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, there are no posts available at the time.'); ?></p>
<?php endif; ?>
</article>
Change the h1 tags or style or color whatever you want for each block defined in the code and you will be able to get the desired effect.
I have two wp_query loops.
The first loop looks for a featured post and formats it differently than the rest of the posts.
Then the second loop displays the rest of the posts.
I want to exclude this first, featured post from the rest of the loop.
Right now, the post ID of the first post is saved as a variable.
I want to use that variable in the second loop, with the wp_query exclude argument.
But as far as I understand, that variable dies when my first loop ends. It's then a null variable in the second loop and so nothing is excluded.
<!-- Here's the query for the featured post -->
<?php
// the arguments
$args = array(
'posts_per_page' => '1',
'orderby' => '',
'meta_key' => 'featured_post',
'meta_value' => '1'
); ?>
<?php $the_query = new WP_Query($args); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
<?php $postid = get_the_ID(); ?>
<div class="small-12 columns entry" >
<div class="text-center" id="featured-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured'); ?>
</a>
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php
$content = get_the_content();
echo wp_trim_words($content, '75');
?>
</p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- If there is no featured post, pull in the most recent post and make it big -->
<?php else: ?>
<?php
// the arguments
$args = array(
'posts_per_page' => '1',
'orderby' => '',
); ?>
<?php $the_query = new WP_Query($args); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
<?php $postid = get_the_ID(); ?>
<div class="small-12 columns entry" >
<div class="text-center" id="featured-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured'); ?>
</a>
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php
$content = get_the_content();
echo wp_trim_words($content, '75');
?>
</p>
</div>
</div>
<!-- End of the nested loop (most recent posts) -->
<?php endwhile; ?>
<?php endif; ?>
<!-- End of the featred post query loop-->
<?php endif; ?>
And here's the main loop:
<?php get_template_part('parts/content', 'featured-post'); ?>
<!-- This ends the logic for the featured post. Now we show the rest of the posts, excluding the first one we showed -->
<?php get_template_part('parts/content', 'category-filter'); ?>
<?php
// the arguments
$args=array(
'paged' => $paged,
'posts_per_page' => 9,
'post__not_in' => array($postid)
); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- Start row that holds blocks -->
<div class="row small-up-1 medium-up-2 large-up-3">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="column entry" >
<?php if( get_the_post_thumbnail() ): ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('blog'); ?>
</a>
<?php else : ?>
<?php endif; ?>
<h5><?php the_title(); ?></h5>
<?php
$excerpt = get_the_excerpt();
echo wp_trim_words( $excerpt , '10', '');
?>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>Sorry, no more posts.</p>
<?php endif; ?>
How exactly do I set up a variable to use in the second loop?
I want to exclude category from shoowing my blog posts. My category id is 62. category name is perfect_work
Here is my wordpress blog template code:
<div id="left" class="eleven columns">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="title">
<h2><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>" ><?php the_title(); ?></a></h2>
<div class="postmeta"> <span>by <?php the_author_posts_link(); ?></span> | <span><?php the_time('l, F jS, Y') ?></span> | <span><?php the_category(', '); ?></span> </div>
</div>
<div class="entry">
<?php $image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'top_feature'); ?>
<img src="<?php echo $image_attr[0]; ?>" class="postim scale-with-grid" id="blog-thumb" >
<?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
<div class="clear"></div>
</div>
</div>
<?php endwhile; ?>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>
I already tried using
$wp_query = new WP_Query('cat=-62');
its not work. I also put
<?php query_posts('cat=-62'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Its work but page navigation not work, and also not showing others post. only 1st 5 post show.
Any Solution?
Get the page number
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
Then you may use
$wp_query = new WP_Query('cat=-62&paged=' . $paged);
Or use
$cat_id = get_cat_ID('perfect_work');
$wp_query = new WP_Query('cat=-' . $cat_id . '&paged=' . $paged);
Then loop
if($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
// ...
endwhile;
endif;
Try this one you have to specify the showposts to limit the posts
<?php $wp_query->set( 'cat', '-62' ); ?>
<?php query_posts( 'showposts=10' ); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
.
.
.
<?php endwhile; ?>
<?php endif; ?>
Note : The minus sign indicates the exclusion of all Posts which
belong to that category from being retrieved from the database. In
turn, the Loop will never have Posts of that category id and only
process the specified number of Posts of other category ids.
Please read the codex on WP_Query, it is imo very detailed, look at the category params part
Just add a minus sign - in front of the categories you dont want, so the below code would mean show posts with category 10 and 11, but exclude category 62
$recent = new WP_Query("showposts=3&cat=10,11,-62")
You don't need to use the $temp variable before or after the query. You should use something like this:
//This should do the trick
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => -62,
'paged' => $paged
);
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
//The real trick!
<?php wp_reset_postdata(); ?>
Two things to note:
The paged query parameter
To reset the query use wp_reset_postdata()
Sorry, I am pretty new to WordPress coding and learning it from scratch. Please pardon me for my lack of experience.
I have a custom post type called 'Products' and want to display only them as a date based archive in homepage. But, I Don't want to display the post titles or any other content from the posts except the featured from first three or four posts in that date. Something like this:
I am trying the following code, but it returns the posts as the normal loop.
<?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
<?php if ( $query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
$archive_year = get_the_time('Y');
$archive_month = get_the_time('m');
$archive_day = get_the_time('d');
?>
<div class="idpostdate">
<?php the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?>
</div>
<div class="thumbnail">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('homet'); } ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
Any guidance?
Functions like the_ID() and the_post_thumbnail() run on the main query. If you want to use their equivalents in your code, you'll need to prepend $query-> to them.
Untested, but I think it'll do what you want it to:
<?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
<?php if ( $query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php $query->the_ID(); ?>" <?php $query->post_class(); ?>>
<?php
$archive_year = $query->get_the_time('Y');
$archive_month = $query->get_the_time('m');
$archive_day = $query->get_the_time('d');
?>
<div class="idpostdate">
<?php $query->the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?>
</div>
<div class="thumbnail">
<?php if ( $query->has_post_thumbnail() ) { $query->the_post_thumbnail('homet'); } ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
I've been stuck on this since last night and cant' figure out why the "Work" section of my wordpress doesn't show the the_title. Also, my "work" page is set to the "Work Page" template. Also, how would I go about having multiple posts on this page? Thanks!
http://www.listentotheway.com
<?php
/*
Template Name: Work Page
*/
get_header(); ?>
<p> This is the work.php file </p>
<?php
$args = array( 'post_type'=>'work' );
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><a href="<?php the_permalink() ;?>"<?php the_title(); ?></h3>
<?php the_field( 'description' ); ?>
<hr>
<?php endwhile; else: ?>
<p> customize later </p>
<?php endif; ?>
<?php get_footer(); ?>
To summarise my suggestion so far:
<?php if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<h3><a href="<?php the_permalink() ;?>"<?php the_title(); ?></h3>
<?php // the_field( 'description' ); ?>
<hr>
<?php endwhile; else: ?>
<p> customize later </p>
<?php endif; ?>
I have coded out the words the_field, there is no such native Wordpress function. Try my code an see what happens!