I am trying to implement a recent posts carousel using Slick Slider and ACF.
I have found a great tutorial and followed the instructions here:
https://imranhsayed.medium.com/slick-slider-wordpress-without-plugin-slick-carousel-ef2394c737ef
and after MUCH trial and error I can get it to work as expected using some basic test content like this:
<div class="posts-carousel">
<div>test</div>
<div>test</div>
<div>test</div>
</div>
But when I try and implement the code for the recent posts loop, I just get a white screen. I'm not great at php but I have tried to work out the issue and just I can't see it.
Another code example that is given works perfectly when copy and pasted:
<div class="posts-carousel px-5">
<!--Slide One-->
<div class="card">
<img width="350" height="233" src="https://via.placeholder.com/150" class="w-100" alt="alt-text">
<div class="card-body">
<h3 class="card-title">Your Post heading</h3>
<p>Your Post Excerpt</p>
View More
</div>
</div>
<!--Slide Two-->
<div class="card">
<img width="350" height="233" src="https://via.placeholder.com/150" class="w-100" alt="alt-text">
<div class="card-body">
<h3 class="card-title">Your Post heading</h3>
<p>Your Post Excerpt</p>
View More
</div>
</div>
</div>
But the code example for including a post loop does not
<?php
/**
* Posts Carousel
*
* #package aquila
*/
$args = [
'posts_per_page' => 5,
'post_type' => 'post',
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
];
$post_query = new \WP_Query( $args );
?>
<div class="posts-carousel px-5">
<?php
if ( $post_query->have_posts() ) :
while ( $post_query->have_posts() ) :
$post_query->the_post();
?>
<div class="card">
<?php
if ( has_post_thumbnail() ) {
the_post_custom_thumbnail(
get_the_ID(),
'featured-thumbnail',
[
'sizes' => '(max-width: 350px) 350px, 233px',
'class' => 'w-100',
]
);
} else {
?>
<img src="https://via.placeholder.com/510x340" class="w-100" alt="Card image cap">
<?php
}
?>
<div class="card-body">
<?php the_title( '<h3 class="card-title">', '</h3>' ); ?>
<?php aquila_the_excerpt(); ?>
<a href="<?php echo esc_url( get_the_permalink() ); ?>" class="btn btn-primary">
<?php esc_html_e( 'View More', 'aquila' ); ?>
</a>
</div>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
I tried editing or removing the references to 'aquila' which I think is the theme the post writer it using but it did not work
I currently have this:
<?php
$args = [
'posts_per_page' => 2,
'post_type' => 'post',
];
$post_query = new WP_Query( $args );
?>
<div class="posts-carousel px-5">
<?php
if ( $post_query->have_posts() ) :
while ( $post_query->have_posts() ) :
$post_query->the_post();
?>
<div class="card">
<?php
if ( has_post_thumbnail() ) {
the_post_custom_thumbnail(
get_the_ID(),
'featured-thumbnail',
[
'sizes' => '(max-width: 350px) 350px, 233px',
'class' => 'w-100',
]
);
} else {
?>
<img src="https://via.placeholder.com/510x340" class="w-100" alt="Card image cap">
<?php
}
?>
<div class="card-body">
<?php the_title( '<h3 class="card-title">', '</h3>' ); ?>
</div>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
I have actually found a plugin that can do it for me, but I feel like I am so close to getting it working, i just can't work it out.
Any help would be great
I have the same problem. You can try change the name of thumbnail function :
<?php
if ( $post_query->have_posts() ) :
while ( $post_query->have_posts() ) :
$post_query->the_post();
?>
<div>
<div class="card">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(
get_the_ID(),
'featured-thumbnail',
[
'sizes' => '(max-width: 350px) 350px, 233px',
'class' => 'w-100',
]
);
} else {
?>
<img src="https://via.placeholder.com/510x340" class="w-100" alt="Card image cap">
<?php
}
?>
<div class="card-body">
<?php the_title( '<h3 class="card-title">', '</h3>' ); ?>
<?php the_excerpt(); ?>
<a href="<?php echo esc_url( get_the_permalink() ); ?>" class="btn btn-primary">
<?php esc_html_e( 'View More', 'aquila' ); ?>
</a>
</div>
</div>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
Related
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>
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 -->
I'm fairly new with using WP shortcodes, and I've run into a problem. I have tried to make a shortcode, that shows 6 of my blog posts through a loop, but it doesn't work. When it loads, it just smashes the page. The loop code works in practice, just not with the shortcode.
The code
function myshort() { ?>
<?php
$args = array( 'post_type' => 'cases', 'posts_per_page' => 6 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-sm-6 wow fadeInUp" data-wow-delay="0.1s">
<a class="content" href="<?php echo get_permalink( $post->ID ); ?>">
<div class="image">
<?php the_post_thumbnail(); ?>
</div>
<div class="text">
<span class="date"><?php echo rwmb_meta( 'rw_stitle' ); ?></span>
<h3><?php the_title(); ?></h3>
<p><?php echo rwmb_meta( 'rw_sdesc' ); ?></p>
</div>
</a>
</div>
<?php endwhile;
}
add_shortcode('doitman', 'myshort');
So, my question is, how do I write this the right way?
The result should be returned as a value,
you could try something like this :
function myshort() {
ob_start(); ?>
<?php
$args = array( 'post_type' => 'cases', 'posts_per_page' => 6 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-sm-6 wow fadeInUp" data-wow-delay="0.1s">
<a class="content" href="<?php echo get_permalink( $post->ID ); ?>">
<div class="image">
<?php the_post_thumbnail(); ?>
</div>
<div class="text">
<span class="date"><?php echo rwmb_meta( 'rw_stitle' ); ?></span>
<h3><?php the_title(); ?></h3>
<p><?php echo rwmb_meta( 'rw_sdesc' ); ?></p>
</div>
</a>
</div>
<?php endwhile;
return ob_get_clean();
}
add_shortcode('doitman', 'myshort');
I'm trying to get taxonomy from post and link. But, link has to be from other custom post "directors" and taxonomy is has to be from other custom post type... I can get taxonomy normally using another code but, i cant get links...
When i trying to use this code i get nothing on page...
What is the problem of this code?
<h6 class="box-title"><?php _e( 'Directed by:', 'listingpress' ); ?></h6>
<div class="director-box-inner">
<?php
$taxonomy ='mydirectors';
$term_lists = get_the_terms($post->ID, $taxonomy);
foreach ($term_lists as $termlist):
$name_term= $termlist->slug;
$argsdirectors = array(
'post_type' => 'directors',
'name' => $name_term
);
$loop = new WP_Query($argsdirectors);
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post();
?>
<div class="agent-card">
<a href="<?php the_permalink() ?>" class="agent-avatar-container">
<?php if(has_post_thumbnail()){
the_post_thumbnail();
}else{?>
<img src="<?php echo get_stylesheet_directory_uri().'/images/profil.png';?>"/>
<?php }
?>
</a>
<div class="agent-card-info">
<div class="agent-card-info-inner">
<h6 class="agent-card-name">
<?php the_title(); ?>
</h6>
</div>
</div>
</div>
<?php _e( 'View Profile', 'listingpress' ); ?>
<div class="clearfix"></div>
<?php
endwhile;
endif;
endforeach;
wp_reset_query();
?>
</div>
When I try to get the caption or description from a image in wordpress it's not working. I can't find a real answer. I tried so many things but I don't know where i have to put some of the code. I read that I had to put this in functions.php:
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
$attachment_meta = wp_get_attachment(your_attachment_id);
and this in my loop:
<?php echo $attachment_meta['caption']; ?>
Like this:
<section>
<div class="container">
<div class="row">
<?php
if( class_exists('Dynamic_Featured_Image') ):
global $dynamic_featured_image;
global $post;
$featured_images = $dynamic_featured_image->get_featured_images( $post->ID );
if ( $featured_images ):
?>
<?php foreach( $featured_images as $images ): ?>
<div class="col-md-4 col-sm-4 col-xs-12">
<img src="<?php echo $images['full'] ?>" alt="" class="img-responsive">
<?php echo $attachment_meta['caption']; ?>
</div>
<?php endforeach; ?>
<?php
endif;
endif;
?>
</div>
</div>
</section>
What am I doing wrong?
You must call wp_get_attachment() function inside your loop like this:
<?php foreach( $featured_images as $images ): ?>
<div class="col-md-4 col-sm-4 col-xs-12">
<img src="<?php echo $images['full'] ?>" alt="" class="img-responsive">
$attachment_meta = wp_get_attachment($images['attachment_id']);
<?php echo $attachment_meta['caption']; ?>
</div>
<?php endforeach; ?>