Date Base Archive On WordPress Homepage - php

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; ?>

Related

Change page template to show Custom Post Type in Wordpress

I have this Page Template, developed by someone else a long long time ago, which shows all my Posts in a grid based on the category they're in.
Now, I want to have such a page for my Custom Post Type but I can't get it to work. I tried replacing the
<?php if (have_posts()) : while (have_posts()) : the_post();?>
with
<?php $args = array('post_type'=>array('shopitems')); query_posts($args); if (
have_posts() ) : while ( have_posts() ) : the_post();?>`
which seems to be the go-to answer everywhere I look. but for me, it turns out empty.
Below the original Page Template, I want to change this so it loads the 'shopitems' Custom Post Type.
Any help or nod in the right direction is greatly appreciated!
<?php get_header(); ?>
<div class="content-sidebar-wrap">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php $category_list= get_the_content(); ?>
<?php endwhile; endif; ?>
<div id="content" class="post-category page">
<?php $category_name=explode(',', $category_list);
$catIDs="";
foreach ($category_name as $cat_name) {
$catIDs.= get_cat_ID(trim($cat_name)).",";
}
$catIDs=trim($catIDs,",");
$i=0;
?>
<?php $thePosts= query_posts('cat='.$catIDs.'&post_status=publish&posts_per_page=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if (has_post_thumbnail( $post->ID ) ) { ?>
<?php // $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) ); ?>
<?php if($i%4==0) { ?>
<div class="clear"></div>
<?php } $i++; ?>
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<img alt="" src="<?php echo $image[0]; ?>" class="thumb-img">
<span class="thumb-title">
<?php
$second_name = get_field('second_name');
if( $second_name && !empty($second_name))
echo $second_name;
else
the_title();
?>
</span>
</a>
</div>
<?php } endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
Just you need to modify your query like below.
<?php
$args = array(
'post_type'=>'shopitems'
);
$query = new WP_Query($args);
if ($query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
//do your code here
endwhile;
else:
// No posts found
endif;
?>`

Alter Wordpress Loop Posts Display

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; ?>

Can't reset query to show ramdom post after firs loop Wordpress

I have a piece of code to show a random post under a post. But this code only show me the post's write from the author of that post. Whats wrong?
<!-- post -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>
<?php the_author(); ?>
<?php the_category(none); ?>
<?php the_date(); ?>
<?php the_content();?>
<?php $key="video"; echo get_post_meta($post->ID, $key, true); ?>
<?php $key="imagen"; echo get_post_meta($post->ID, $key, true); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<!-- ramdom post -->
<?php $posts = $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) { ?>
<?php the_post_thumbnail('photo-thumbnail') ?>
<?php the_author(); ?>
<?php the_category(none); ?>
<?php } ?>
Try adding <?php wp_reset_query(); ?> after the first query.
Replace the random post code with the code below. In your example you were querying 3 posts. If that's not your intention and you only want a single post replace 3 with 1 in posts_per_page.
$secondary_posts = new WP_Query( array( 'orderby' => 'rand', 'posts_per_page' => 3, 'no_found_rows' => 1, ) );
if ( $secondary_posts->have_posts() ) : while ( secondary_posts->have_posts() ) : $secondary_posts->the_post();
the_post_thumbnail( 'photo-thumbnail' );
the_author();
the_category();
endwhile; endif;
wp_reset_postdata();

Loop through sticky posts in wordpress?

I'm new to Wordpress and I'm trying to loop through sticky posts with this code:
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Query sticky posts */
$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>
<?php
foreach ($stickies as $sticky) {
the_title();
comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' );
}
?>
However in the out put if I have 2 stickies posts, the first one is displayed two times...
Any idea ?
Thanks a lot for your help !
EDIT :
It seems that
foreach ($stickies['WP_Post Object'] as $sticky) {
get the good two articles, but I still have this error message : Warning: Invalid argument supplied for foreach()...
Full code :
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>
<?php
foreach ($stickies['WP_Post Object'] as $sticky) {
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('thumbnail', array('class' => "media-object img-rounded"));
}
the_title();
comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' );
}
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="media">
<a class="pull-left" href="<?php the_permalink() ?>">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('thumbnail', array('class' => "media-object img-rounded"));
}
?>
</a>
<div class="media-body">
<h3 class="media-heading"><a class="permalink" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title(); ?></a></h3>
Par <?php the_author(); ?>, le <?php the_time('j F Y'); ?> - <?php comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' ); ?>
<?php the_excerpt(); ?>
</div>
</div> <!-- /media -->
<?php endwhile; ?>
<?php endif; ?>
Try this:
$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1, 'ignore_sticky_posts' => true) );
EDIT:
I think you should use standard wordpress posts loop:
while (have_posts()) : the_post();
the_title();
endwhile;
EDIT:
You can use rewind_posts() to start a new loop:
// main loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
// rewind
<?php rewind_posts(); ?>
// new loop
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

Post Objects in Advanced Custom Fields order by date

Hi I have a Post Object field in Advanced Custom Fields that I want to return multiple posts, ordered by date. I have the custom field data from those posts returning fine, but the Post Objects return in order of the Post ID. I want them to be ordered by the date that the post was published.
<?php $post_objects = get_field('exhibitions');
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>
</div>
</a>
<br><br>
<?php endforeach; ?>
<?php endif; ?>
This returns the text custom fields 'title' and 'dates' from each post thats selected in the Post Objects field on the post where this is called.
I want the posts to return here by order of their publish date.
Any ideas?
#Michael Ray-Von - your answer worked, but it involved getting the same data from the db twice. Instead you can just sort the post data returned in your initial ACF query rather than running the extra query. (The post_date is returned as a string so you can strcmp it):
<?php
// get the posts from ACF
$custom_posts = get_field('your_posts_field');
// sort the posts by post date, but you can also sort on ID or whatever
usort($custom_posts, function($a, $b) {
return strcmp($b->post_date,$a->post_date);
});
// write them out
foreach ($custom_posts as $post) : setup_postdata($post); ?>
<article>
<h1><?php the_title();?></h1>
<?php the_excerpt(); ?>
</article>
<?php
endforeach;
wp_reset_query();
?>
Hat-tip to this answer for the sorting: https://stackoverflow.com/a/10159521
Okay i've got it figured out!
Instead of calling get_field as the post_objects, you call it as a variable just to get the IDs of relevant posts, and then use that in an array for the $args of a get_posts. That way you have access to all the array options of get_posts before running the loop.
<?php
$ids = get_field('exhibitions', false, false);
$args = array(
'post__in' => $ids,
'orderby' => 'post_date',
);
$post_objects = get_posts( $args );
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>
</div>
</a>
<br><br>
<?php endforeach; ?>
<?php endif; ?>
Thanks for your help!
found my answer thanks to: http://support.advancedcustomfields.com/discussion/5846/adding-args-to-post_objects-get_field/p1
<?php $post_objects = get_field('exhibitions');
$args = array (
'orderby'=>'date',
);
$the_query = new WP_Query( $args );
if($the_query->have_posts()) {
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>` </div>
</a>
<br><br>
endwhile;
wp_reset_postdata();
}
I haven't tested, But it should work for you with little adaption !

Categories