Testimonials slider loop/query not working PHP Wordpress - php

So, I am trying to display Testimonials with Slick slider but it is not working. I guess I linked the css and js files wrong or something because testimonials are displaying but slider doesn't work. So please help with troubleshooting.
This is code:
HTML
<section class="reviews">
<div class="row d-flex flex-column">
<div class="testimonial-holder">
<?php
$args = array(
'post_type' => 'testimonials',
// 'post_status' => 'publish',
// 'posts_per_page' => 1,
// 'order' => 'ASC',
);
$query = new WP_Query( $args );
?>
<?php
while( $query->have_posts() ) :
$query->the_post();
?>
<div class="lead-text d-flex text-center justify-content-center flex-column">
<?php echo '<p class="lead">' . get_the_content() . '</p>' . '<small>' . '-' . get_the_title() . '</small>'; ?>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</section>
ENQUE
function load_css() {
wp_register_style( 'slick', get_template_directory_uri() . '/src/js/slick/slick.css', array(), false, 'all' );
wp_enqueue_style('slick');
}
add_action( 'wp_enqueue_scripts', 'load_css' );
function load_js() {
wp_register_script( 'slick', get_template_directory_uri() . '/src/js/slick/slick.min.js', false, true);
wp_enqueue_script('slick');
}
add_action( 'wp_enqueue_scripts', 'load_js' );
CALLING IT WITH JQUERY//This is only line of code and it should work
$('.testimonial-holder').slick();

Did you attach the .slick() callback on document.ready state?

At the end I pulled this off with Advanced Custom Fields with number field. I gave up on slider and changed the concept little bit. So, depends of the entered number in the filed, it will generate number of stars in the Front end.
<?php
$args = array(
'post_type' => 'testimonials',
'post_status' => 'publish',
'posts_per_page' => 1,
'order' => 'DESC',
'orderby'=> 'rand',
);
$query = new WP_Query( $args );
//if( $query->have_posts() ) :
?>
<!-- Displaying testimonials -->
<?php
while( $query->have_posts() ) : ?>
<!-- <div class="col-lg-4"> -->
<div class="content-holder">
<?php $query->the_post(); ?>
<div class="stars-holder">
<?php
$star = get_field('stars');
$count = 0;
$element = '<i class="fa fa-star"></i>';
while ($count < $star) : $count++;
echo $element;
endwhile;
?>
</div>
<div class="lead-text d-flex text-center justify-content-center flex-column">
<?php echo '<p class="lead">' . get_the_content() . '</p>' . '<small>' . '-' . get_the_title() . '</small>'; ?>
</div>
</div>
<!-- </div> -->
<?php endwhile;
wp_reset_postdata(); ?>

Related

Attempting something new in WP. Issue resetting WP loop

Im working in WP and basically what I've got set up is some logic to create a separate carousel for each category of my blog.
For each carousel im displaying the top viewed posts for each category. Each Item in the carousel has a post thumbnail, author name and author avatar.
My problem(if you scroll down to the bottom of the home screen pst"meet the community") the posts are showing the right post names and thumbnails but something funky is going on with the author names and avatars. The first post in the shelf looks correct but after that each post is showing the wrong author.I really dont know how to handle this. Ive been mashing they keys for a week already. Any thoughts?
<!-- A shelf for each category by most posts and posts by highest view count-->
<?php
$cat_args = array(
'number' => 5,
'orderby' => 'count',
'post_type' => 'post',
'order' => 'DESC',
'child_of' => 0
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<div class="shelf_holder">';
echo '<div class="shelf_title"> <h1> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" )) . '" ' . '> View All</h5></div>';
echo '<div class="shelf_prev"><div class="backwards_icon"></div></div>';
echo '<div class="shelf_next"><div class="forward_icon"></div></div>';
echo '<div class="display-posts-listing grid">';
$post_args = array(
'numberposts' => 8,
'category' => $category->term_id,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<div class="listing-item">
<?php
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium');
list($url, $width, $height, $is_intermediate) = $thumbnail;
?>
<div class="media-wrapper">
<?php $post_views = get_post_meta( get_the_ID(), 'post_views_count', true );?>
<div class="counter"><div class="views_icon"></div><?php echo $post_views; ?></div>
<div class="save_card"> <?php the_favorites_button($post_id);?></div>
<div class="media-gradient" style="height:196px;"></div>
<div class="media" style="height:196px;width:<?php echo $width; ?>px;background-image:url(<?php echo $url; ?>);background-size:cover;overflow: hidden;background-position: center;width: 100%;left:0px;"></div>
</div>
<div class="tiny_head"><?php echo get_avatar( get_the_author_meta( 'ID' ), 32 );?> </div>
<div class="card_content">
<h3><?php the_title(); ?></h3>
<div class="entry-meta">
<?php
global $post;
$author_id=$post->display_name;
?>
<?php
the_author_meta( 'display_name', $author_id );
?>
<?php wp_reset_query(); ?>
</div>
</div>
</div> <!-- listing item -->
<?php
}
echo '</div class="display-posts-listing grid">';
echo '</div class="shelf_holder">';
} ?>
Replace your foreach with this:
foreach($posts as $post) : ?>
<div class="listing-item">
<?php
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium');
list($url, $width, $height, $is_intermediate) = $thumbnail;
$author_id=$post->post_author;
?>
<div class="media-wrapper">
<?php $post_views = get_post_meta( $post->ID, 'post_views_count', true );?>
<div class="counter"><div class="views_icon"></div><?= $post_views; ?></div>
<div class="save_card"> <?php the_favorites_button($post_id);?></div>
<div class="media-gradient" style="height:196px;"></div>
<div class="media" style="height:196px;width:<?= $width; ?>px;background-image:url(<?= $url; ?>);background-size:cover;overflow: hidden;background-position: center;width: 100%;left:0px;"></div>
</div>
<div class="tiny_head"><?= get_avatar( get_the_author_meta( 'ID' ), $author_id );?> </div>
<div class="card_content">
<h3><?= get_the_title($post); ?></h3>
<div class="entry-meta">
<?= get_the_author_meta( 'display_name', $author_id ); ?>
</div>
</div>
</div> <!-- listing item -->
<?php
endforeach;
You had some errors in the way you were using get_the_author_meta. It needs the author ID and you tried getting it using display_name. The one you used for the avatar is hardcoded. (That's only going to return the meta that belongs to ID 32).
There are a lot of inconsistencies in your code. Try to understand what is happening. Look up the functions you're using and what they do exactly. The PHP documentation and WordPress documentation both are excellent resources.

I have problem when showing post in loop using WP_Query in Wordpress

i have a problem when trying to show a current 4 post using WP_Query.
But, the older post (The very first post) also shown in the first loop.
This is my code:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '3'
);
$query = new WP_Query( $args ); //show 4 post
if ( $query->have_posts() ){
/* Start the Loop */
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="col-md-3 d-flex align-items-stretch">
<div class="card ">
<?php if (has_post_thumbnail()) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
?>
<img class="miniimg" src="<?php echo $featured_img_url; ?>" width="auto" height="200px">
<?php } ?>
<div class="card-body">
<h4><a href="<?php the_permalink(); ?>">
<?php the_title();
$post_date = get_the_date( 'j F Y' );
?>
</a></h4>
<p class="card-text"><?php the_excerpt();?></p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-primary">Read More</button>
</div>
<small class="text-muted"><?= $post_date; ?></small>
</div>
</div>
</div>
</div>
<?php
$counter++; }
}
?>
This is the result =>
How to fix this problem? is there any problem with my code?
Thankyou.
If specifying 'posts_per_page' => 3 gives back 4 posts, it is almost 100% sure that the first post is a sticky post. Use the option ignore_sticky_posts to ignore them.
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'ignore_sticky_posts' => 1,
);
...
A Sticky Post is the post will be placed at the top of the front page of posts. This feature is only available for the built-in post type post and not for custom post types.
Source # https://developer.wordpress.org/themes/functionality/sticky-posts/
I'm pretty sure you applied is sticky to your post from 2012.
To verify you can just add the following to your loop inside the while statement:
<?php //...
while( have_posts() ): the_post();
if( is_sticky() ):
echo 1;
else: echo 0;
endif;
endwhile;
//... ?>
Or go to you post in the admin console, and verify that you didn't check the "is sticky" checkbox on the right side in the publish panel.
<?php
$args = array( 'post_type' => 'movies');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?><div class="movie-content" >
<?php
echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">' ?>
<h2><?php echo the_title(); ?></h2>
<div><?php the_post_thumbnail('thumbnail'); ?></div>
<div class="excerpt"><?php the_excerpt(); ?></div>
<?php
the_content();
echo '<div class="entry-content">';
echo '</div>';
echo "</div></a>";
endwhile;
?>

how to make sorting by meta_key in wordpress

in my search page wordpress sorting post by date.
But i want sorting to meta_key in my template - imic_date (date of event).
I need to modify the search query, but where?
code that should work. But... where I add it?
$args = array(
'post_type' => array('custom-post-type'),
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'imic_date'
);
$loop = new WP_Query($args);
(sorry for my english) :D
edit:
my search.php file:
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-md-9 posts-archive">
<?php
if (have_posts()) :
while (have_posts()):the_post();
echo'<article class="post">
<div class="row">';
if (has_post_thumbnail()):
echo '<div class="col-md-4 col-sm-4">
<a href="' . get_permalink() . '">';
the_post_thumbnail('600x400', array('class' => "img-thumbnail"));
echo'</a></div>';
endif;
echo '<div class="col-md-8 col-sm-8">';
echo '<h3>' . get_the_title() . '</h3>';
echo '<span class="post-meta meta-data">
<span><i class="fa fa-calendar"></i>' . get_the_time(get_option('date_format')) . '</span><br><span>Time:<br/>Date:<br/>Price:<br/>Venue:<br/>Category:</span><span></i><br/>'.imic_custom_taxonomies_terms_links().'</span> <span>';
comments_popup_link('<i class="fa fa-comment"></i>'.__('No comments yet','framework'), '<i class="fa fa-comment"></i>1', '<i class="fa fa-comment"></i>%', 'comments-link',__(' ','framework'));
echo'</span></span>';
//echo imic_excerpt(50);
echo '<p>' . __('More ', 'framework') . '<i class="fa fa-long-arrow-right"></i></p>';
echo '</div></div>';
echo '</article>';
endwhile;
else:
echo '<article class="post">';
if (current_user_can('edit_posts')) :
?>
<h3><?php _e('NOT', 'framework'); ?></h3>
<p><?php printf(__('NOT', 'framework'), admin_url('post-new.php')); ?></p>
<?php else : ?>
<h3><?php _e('NOT', 'framework'); ?></h3>
<p><?php printf(__('NOT', 'framework')); ?></p>
<?php
echo '</article>';
endif;
?>
<?php
endif; // end have_posts() check
pagination();
?>
</div>
<!-- Start Sidebar -->
<?php
echo '<div class="col-md-3 sidebar">';
dynamic_sidebar('sidebar');
echo '</div>';
?>
<!-- End Sidebar -->
</div>
</div>
<?php get_footer(); ?>
Try this code.
$args = array(
'post_type' => array('custom-post-type'),
'order_by' => array('imic_date'),
'order' => 'ASC',
);
$loop = new WP_Query($args);
edit,
modify your code like this,
$args = array(
'post_type' => array('custom-post-type'),
'order_by' => array('imic_date'),
'order' => 'ASC',
);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()):$loop->the_post();
/** and then your code as used in original file */
Edit,
use the code bellow, it works ( I tested )
<?php $args = array(
'post_type' => 'event',
'orderby' => 'meta_value',
'meta_query' => array(array('key' => 'imic_date')),
'order' => 'desc',
'paged'=>$paged,
);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()):$loop->the_post(); ?>
<?php the_title(); ?>
and your rest of the code here..
global $wpdb;
$tb_posts = $wpdb->prefix . 'posts';
$tb_postmeta = $wpdb->prefix . 'postmeta';
$query = $wpdb->get_results("SELECT $colums
FROM $tb_posts
INNER JOIN $tb_postmeta
ON $tb_posts.ID = $tb_postmeta.post_id
WHERE $tb_postmeta.meta_key IN ('your_meta_key')
AND $tb_posts.post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1
"
);
return $query;

How to add read more in wordpress

I have tried many things but i didn't get Read more link in paragraph
page.php
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="inner-box-post">';
echo '<h2><a href="get_permalink();">';
the_title();
echo '</a></h2>';
echo '<div class="thumbnail">';
the_post_thumbnail( 'thumbnail' );
get_comments( $args );
echo '</div>';
echo '<div class="post-containt">';
echo '<div class="post-date"><strong>';
echo get_the_date();
echo '</strong> </div>';
echo the_excerpt(); ;
echo '</div> ';
echo '</div>';
endwhile; ?>
</div>
function.php
function new_excerpt_more($output) {
return $output . '<p>' . 'Read more &raquo' . '</p>';
}
add_filter('get_the_excerpt', 'new_excerpt_more');
Filter is excerpt_more,
add_filter('excerpt_more', 'new_excerpt_more');
Reference.
THIS IS NOT AN ANSWER, JUST CORRECTIONS
You really don't need to overuse echo in your code. Just use opening and closing php tags correctly. Rather use this. It also helps with readibility
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="inner-box-post">
<h2><?php the_title(); ?></h2>
<div class="thumbnail">
<?php the_post_thumbnail( 'thumbnail' ); ?>
<?php get_comments( $args ); ?>
</div>
<div class="post-containt">
<div class="post-date"><strong>
<?php get_the_date(); ?>
</strong> </div>
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="inner-box-post">
<h2><?php the_title(); ?></h2>
<div class="thumbnail">
<?php the_post_thumbnail( 'thumbnail' ); ?>
<?php get_comments( $args ); ?>
</div>
<div class="post-containt">
<div class="post-date">
<strong><?php echo get_the_date(); ?></strong>
</div>
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
Remove [...] and add read more option using
function new_excerpt_more($output) {
$output = rtrim($output,'[...]');
return $output . '<p>' . 'Read more >>' . '</p>';
}
add_filter('excerpt_more', 'new_excerpt_more');

Display recent posts with thumbnails with masonry in wordpress

I am working on a wordpress cms. On a page-template, I need to display recent 10 posts. So I tried to use, wp_get_recent_posts, found here in the codex, which I think is an appropriate hook for the purpose.The rest of the code is from my archive.php ( which displays the post-thumbnails in a masonry grid just fine in archive.php). I simply would like to achieve the same thing with the recent posts-thumbnails being on this page-template. Currently, I have this code on a template.
<div id="masonry">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
$args = array('numberposts' => '10', 'meta_key' => '_thumbnail_id');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
if (has_post_thumbnail($recent["ID"])) {
echo '<a href="' . get_permalink($recent["ID"]) . '">';
echo get_the_post_thumbnail($recent["ID"], 'archive_grid');
echo '</a>';
}
}
?>
</div>
</div>
<?php endwhile;
endif; ?>
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
<div id="masonry-new"></div>
<div class="post-navigation clearfix"><!--BEGIN .post-navigation -->
<?php dt_pagination(); ?>
</div><!--END .post-navigation -->
ISSUE : This code returns just a single thumbnail from the most recent posts. I have no clue what is wrong with the loop. The other weird thing to notice is when I var_dump the $recent_posts it returns other text-contents of the posts just fine. If you need to know this, I have my setting as setting->reading->Blog pages show at most->20 posts.
First of all you don't need to have two loops if I understood correct what you want to do. Also 'meta_key' => '_thumbnail_id' isn't necessary. I suppose is just the thumbnail id and not a meta key created by you.
You could achieve what you want like that:
<div id="masonry">
<?php
$args = array(
'posts_per_page' => 10,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => false );
$recent_posts = get_posts( $args );
foreach ($recent_posts as $key=>$post): setup_postdata( $post ); {
?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
if (has_post_thumbnail(get_the_ID())) {
echo '<a href="' . get_permalink(get_the_ID()) . '">';
echo get_the_post_thumbnail(get_the_ID(), 'archive_grid');
echo '</a>';
}
?>
</div>
</div>
<?php } ?> //end of foreach loop
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
<div id="masonry-new"></div>
<div class="post-navigation clearfix"><!--BEGIN .post-navigation -->
<?php dt_pagination(); ?>
</div><!--END .post-navigation -->
But this way you have to fix the pagination your self.
UPDATE:
It seems that get_posts doesn't work with pagination.
<div id="masonry">
<?php
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => false );
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
if (has_post_thumbnail(get_the_ID())) {
echo '<a href="' . get_permalink(get_the_ID()) . '">';
echo get_the_post_thumbnail(get_the_ID(), 'archive_grid');
echo '</a>';
}
?>
</div>
</div>
<?php endwhile; ?> //end while loop
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
<div id="masonry-new"></div>
<div class="post-navigation clearfix"><!--BEGIN .post-navigation -->
<?php dt_pagination(); ?>
</div><!--END .post-navigation -->
<?php wp_reset_postdata(); ?>
If the second solution doens't work you have to provide the dt_pagination() function to see how is working.
Hope it helps.
First thing is you dont need two loops. Try new wp query instead of wp_get_recent_posts. here is an example for you.
<div id="masonry">
<?php $args = array('post_type' => 'post','posts_per_page' => '10' );
$loop = new WP_Query($args); if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post(); global $post; ?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
if (has_post_thumbnail(get_the_ID())) {
echo '<a href="' . get_permalink(get_the_ID()) . '">';
echo get_the_post_thumbnail(get_the_ID(), 'archive_grid');
echo '</a>';
}
?>
</div>
</div>
<?php endwhile; endif; wp_reset_Query(); ?>
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->

Categories