Wordpress Timeline Show post Monthly - php

I am fetching data through custom post type and category. I have added 12 category Jan to Dec and Jan has 1 posts Feb have 2 post
What I am struggling to do on January post the 2 circle is showing on the left I want only one January Circle rest of the January post under the circle.
Can you please how can i put check on category?
here is the site http://novartis.portlandvault.com/timeline/
Thanks
<div id="timeline">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'timeline', 'order' => 'asc' );
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
$thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'large') );
$category = get_the_terms($id, 'timeline_categories');
?>
<div class="timeline-item">
<div class="timeline-icon">
<div class="timeline-month">
<?php echo $category[0]->name;?>
</div>
</div>
<div class="timeline-content right">
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php echo the_content(); ?>
</p>
<div class="timeline_img">
<img src="<?php echo $thumb; ?>" class="img-responsive">
</div>
</div>
</div>
<?php endwhile;?>
</div>
<!-- Timeline ends here -->

I am not sure using categories is the best way to do this. You are limiting yourself to one years worth of data. I would suggest actually using the post date to separate the posts, then your code could look something like this.
<div id="timeline">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'timeline', 'order' => 'asc' );
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
$thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'large') );
$category = get_the_terms($post->ID, 'timeline_categories');
?>
<section id="<?php echo $post->ID; ?>">
<?php
if( $loop->current_post === 0 ) {
$current_quarter = $category[0]->name; ?>
<div class="quarterlyheading">
<?php echo $current_quarter; ?>
<div class="quarterlinebreak"><hr></div>
</div>
<?php } else {
$post_quarter = $category[0]->name;
if($current_quarter != $post_quarter) { ?>
<div class="quarterlyheading">
<?php echo $post_quarter; ?>
<div class="quarterlinebreak"><hr></div>
</div>
<?php }
}
$current_quarter = $post_quarter;
?>
<div class="timeline-item">
<?php
if( $loop->current_post === 0 ) {
$current_month = get_the_time('M'); ?>
<div class="timeline-icon">
<div class="timeline-month">
<?php echo $current_month; ?>
</div>
</div>
<?php } else {
$post_month = get_the_time('M');
if($current_month != $post_month) { ?>
<div class="timeline-icon">
<div class="timeline-month">
<?php echo $post_month; ?>
</div>
</div>
<?php }
}
$current_month = $post_month;
?>
<div class="timeline-content right">
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php echo the_content(); ?>
</p>
<div class="timeline_img">
<img src="<?php echo $thumb; ?>" class="img-responsive">
</div>
</div>
</div>
</section>
<?php endwhile;?>
</div>

Related

How to output text from single custom post type on archive page

I have a custom post type. There are single and archive pages. The archive page displays cards with the team. Each card has a city. How can I, when creating a single team member, specify his city and display it on the archive page. This information is not on the single page, only archived.
<?php
$args = array(
'post_type' => 'speakers',
);
$loop = new WP_Query( $args );
while ($loop->have_posts()) :
$loop->the_post(); ?>
<li>
<?php if ( is_object_in_term( $loop->post->ID, 'countries', 'germany' ) ) {
echo '<div class="speaker-flag"><img src="/wp-content/uploads/2022/08/flag-for-germany.svg"></div>';
} else if ( is_object_in_term( $loop->post->ID, 'countries', 'switzerland' ) ) {
echo '<div class="speaker-flag"><img src="/wp-content/uploads/2022/08/flag-for-switzerland.svg"></div>';
} ?>
<a href="<?php echo esc_attr(the_permalink())?>">
<div class="speaker-img">
<?php $img_url = get_the_post_thumbnail_url( $loop->post->ID ); ?>
<img src="<?php echo $img_url ?>">
</div>
<div class="speaker-name">
<p>
<?php the_title(); ?>
</p>
</div>
<div class="speaker-city">
<p>
Fribourg
</p>
</div>
</a>
</li>
<?php endwhile;
wp_reset_query(); ?>
//single
<div class="speaker_info">
<div class="speaker_text">
<h4>
<?php the_title(); ?>
</h4>
<?php the_content(); ?>
</div>
<div class="speaker_img">
<img src="<?php the_field('speaker_img'); ?>">
</div>
</div>

Wrapping a div around the results of a word press loop but excluding the first 2

I have a loop for a custom post type. I'm bringing back a block of title, image and content for each post. I want to apply slick slider to the results to create a slick carousel, but I don;t want to include the first two results of the loop - so I'd need to create a parent div to the results but only start that div after the first two results.
I've trialed ways of querying the results on a loop count to apply a class to only the first two results, but this doesn't really achieve what I'm after.
<div class="wrapper_for_news_items">
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'news',
'order' => 'DESC'
));
if( $posts ): ?>
<?php $post = $posts[0]; $c=0; ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<div class="treatment_block news_block <?php $c++; if($c == 1) { echo ' featured'; } elseif($c == 2) { echo ' featured'; } ?>">
<h2 class="block_title above"> <?php the_title( '' ); ?></h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below"> <?php the_title( '' ); ?></h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
No News Found!
<?php endif; ?>
<!-- end of news loop -->
</div> <!-- treatment news block wrapper -->
You could just create 2 loops.
Use the first for the featured output and the second for the carousel.
<div class="wrapper_for_news_items">
<?php
$args_with_two_posts = array(
'posts_per_page' => 2,
'post_type' => 'news',
'order' => 'DESC'
);
$query_with_two_posts = new WP_Query( $args_with_two_posts );
if( $query_with_two_posts->have_posts ) :
while ( $query_with_two_posts->have_posts ) : $query_with_two_posts->the_posts; ?>
<div class="treatment_block news_block featured">
<h2 class="block_title above">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?> No News Found!
<?php endif; ?>
<!-- end of 2 post initial news loop -->
</div>
<!-- treatment news block wrapper -->
<?php
// Start your second loop containing the slickslider content
?>
<div class="wrapper_for_news_carousel_items">
<?php
$args_with_all_posts = array(
'posts_per_page' => -1,
'offset' => 2 // Offset the 2 initial posts
'post_type' => 'news',
'order' => 'DESC'
);
$query_with_two_posts = new WP_Query( $args_with_all_posts );
if( $args_with_all_posts->have_posts ) :
while ( $args_with_all_posts->have_posts ) : $args_with_all_posts->the_posts; ?>
<div class="treatment_block news_block">
<h2 class="block_title above">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?> No News Found!
<?php endif; ?>
<!-- end of news loop -->
</div>
<!-- treatment news carousel items -->
Or you could count the posts in the loop and asign a wrapper before the third post and after the last post to create the carousel.
<div class="wrapper_for_news_items">
<?php
$args_with_two_posts = array(
'posts_per_page' => 2,
'post_type' => 'news',
'order' => 'DESC'
);
$query = new WP_Query( $args_with_two_posts );
$counter = 1; // Set the counter
if( $query->have_posts ) :
while ( $query->have_posts ) : $query->the_posts;
if ( $count == 3 ) { echo '<div class="slick-slider">'; };
?>
<div class="treatment_block news_block">
<h2 class="block_title above">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php
$counter++; // Add +1 every loop
if (($query->current_post +1) == ($query->post_count)) {
echo '</div>'; // This is the last post
}
endwhile;
?>
<?php wp_reset_postdata(); ?>
<?php else : ?> No News Found!
<?php endif; ?>
<!-- end of news loop -->
</div>
<!-- treatment news block wrapper -->

WP_Query and variables in php (WordPress)

I have a set_gallery function and I pass the set_args array to her. The set_args array always changes. I'm looking for a solution for deleting posts posted and so that the loop outputs posts with new parameters. How can this be realized? Thanks...
function set_gallery($set_args = '') { ?>
<?php if ($set_args == '') {
$args = array (
'post_type' => 'gallerys',
'posts_per_page' => '-1'
);
} else {
$args = $set_args;
} ?>
<?php $gallerys_posts = new WP_Query($args);
wp_reset_query();
if( $gallerys_posts->have_posts() ) :
while ( $gallerys_posts->have_posts() ) :
$gallerys_posts->the_post(); ?>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 shuffle-item filtered">
<div class="portfolio-item">
<a href="<?php echo get_permalink(get_the_id());?>">
<?php if ( get_the_post_thumbnail(get_the_id()) ) { ?>
<?php echo get_the_post_thumbnail( get_the_id(), array(620, 423 )); ?>
<?php } ?>
<div class="portfolio-overlay">
<div class="caption">
<?php the_title(); ?>
<span>
<?php echo get_the_content(); ?>
</span>
</div>
</div>
</a>
</div>
</div>
<?php endwhile; endif; wp_reset_query();?>
<?php }

php template code - all properties page display issue

My page: http://kevinkang.ca/en_US/all-properties/ was broken yesterday.
But I can't find any problem on these. Can anyone help me to find out the problem?
<div class="grid-x all-properties-page">
<?php
$args = array( 'category__not_in' => array( 4, 8, 9, 10 ) , 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $i=0; ?>
<div class="medium-12 cell padding-top-page">
<div class="home-data description-title-price">
<div class="grid-container">
<div class="grid-x">
<div class="medium-6 cell">
<?php the_title(); ?>
</div>
<div class="medium-6 cell text-right">
<?php the_field('price'); ?>
</div>
</div>
</div>
</div>
<a href="<?php the_permalink(); ?>"><div class="grid-x">
<ul class="images3">
<?php
//Get the images ids from the post_metadata
$images = acf_photo_gallery('3-images', $post->ID);
$img3=0;
if( count($images) ):
//Cool, we got some data so now let's loop over it
foreach($images as $image):
$id = $image['id']; // The attachment id of the media
$title = $image['title']; //The title
$caption= $image['caption']; //The caption
$full_image_url= $image['full_image_url']; //Full size image url
$full_image_url = acf_photo_gallery_resize_image($full_image_url, 1562, 760); //Resized size to 262px width by 160px height image url
$thumbnail_image_url= $image['thumbnail_image_url']; //Get the thumbnail size image url 150px by 150px
$url= $image['url']; //Goto any link when clicked
$target= $image['target']; //Open normal or new tab
$alt = get_field('photo_gallery_alt', $id); //Get the alt which is a extra field (See below how to add extra fields)
$class = get_field('photo_gallery_class', $id);
$img3++;
?>
<?php if( !empty($url) ){ ?><a href="<?php echo $url; ?>" <?php echo ($target == 'true' )? 'target="_blank"': ''; ?>><?php } ?>
<li style="background-image: url(<?php echo $full_image_url; ?>);" class="img-list<?php echo $img3; ?>">
<?php if( !empty($url) ){ ?></a><?php } ?></li>
<?php endforeach; endif; ?>
</ul>
<?php foreach ( get_the_category() as $value) {
$i++; ?> <h3 class="cat<?php echo $i; ?> <?php echo $value->name; ?>"><?php echo $value->name;} ?></h3>
</div></a>
<div class="grid-container pr-address-bg">
<div class="address-cont price-address">
<?php the_field('price'); ?>
<?php the_field('address_first_line'); ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
I see you have a lot of problems in your HTML ( inside , multiple tags. First check your HTML with https://validator.w3.org/. It looks the main parts of the problems come from header.php. I would have a look.

CPT output on page breaks other fields

Not sure what i have done wrong here but i noticed that everything dissapears below my code.
If i take fields and put them in top of template then everything works fine.
Code
<?php
$blacklabelpost = array( 'post_type' => 'black-label', );
$loop = new WP_Query( $blacklabelpost );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$images = get_field('photos_of_product'); // get gallery
$image = $images[0]; // get first image in the gallery [1] for second, [2] for third, and so on.
if( $image ) : // only show the image if it exists ?>
<div class="col-sm-3 col-md-4 col-lg-3 product-col">
<a href="<?php the_permalink() ?>">
<img src="<?php echo $image['url']; ?>" class="img-responsive"/>
</a>
<div class="row text-center">
<div class="col-xs-12">
<h3 class="UC sTitle"><?php the_title()?></h3>
<div class="border-line center"></div>
<div class="subtitle about_product_short UC"><?php the_field('about_product_short'); ?></div>
</div>
<div class="col-xs-12">
Läs mer
</div>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
Below i just have couple of if get field.
Example
<?php if( get_field('full_width_photo_footer') ): ?>
<div class="F-W sida archive">
<?php
$image = get_field('full_width_photo_footer');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" class="img-responsive footer-fullwidth-img"/>
<?php endif; ?>
</div>
<?php endif; ?>
As soon as i delete this 'full_width_photo_footer' starts to work again:
<?php
$mypost = array( 'post_type' => 'white-label', );
$loop = new WP_Query( $mypost );
?>
What am i doing wrong ?

Categories