I'm using the carousel that comes with bootstrap. This I'll be using in WordPress. I'm querying two recent posts with a foreach loop but for the carousel to work correctly I need the newest post to have an extra 'active' class. I found some solutions here on stackoverflow but it were all whileloops, I really need it for this foreach loop. This is my code:
<div id="NewsCarousel" class="carousel slide">
<div class="carousel-inner">
<?php
$args = array( 'numberposts' => '2', 'category' => 5 );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<div class="item"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], array(200,200)) .$recent["post_title"].'</a> </div> ';
}
?>
</div>
</div>
You can add a counter like so $count = 0; outside of the foreach loop. And then inside the foreach loop you tell it to increment like so $count++;
You then check if the count is equal to 1 like this: if($count == 1){//do this}
So in your case lets do it like this:
<div id="NewsCarousel" class="carousel slide">
<div class="carousel-inner">
<?php
$args = array( 'numberposts' => '2', 'category' => 5 );
$recent_posts = wp_get_recent_posts( $args );
$count = 0;
foreach( $recent_posts as $recent ){
$count++;
echo '<div class="item'; if($count == 1){echo ' active';}"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], array(200,200)) .$recent["post_title"].'</a> </div> ';
}
?>
</div>
</div>
Try that, it should do the trick. I just used this method on a project of which I'm dealing with currently.
You can use a boolean variable to determine if it is a first loop or not. Initial value is true, once it loops, the value is set to false.
<div id="NewsCarousel" class="carousel slide">
<div class="carousel-inner">
<?php
$args = array( 'numberposts' => '2', 'category' => 5 );
$recent_posts = wp_get_recent_posts( $args );
$isFirst = true;
foreach( $recent_posts as $recent ){
echo '<div class="item' . $isFirst ? ' active' : '' . '"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], array(200,200)) .$recent["post_title"].'</a> </div> ';
$isFirst = false;
}
?>
</div>
</div>
Related
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(); ?>
I've created a loop which is getting the correct child pages and their content, however it's staggering/repeating the page content on the foreach loop.
The boxes (in order) should appear with the following content:
Box 1: PRESS
Box 2: TV SHOWS
Box 3: Radio Shows
Box 4: Gallery
However as you can see it's doing the following:
Box 1: Press
Box 2: Press, TV Shows
Box 3: Press, TV Shows, Radio Shows
Box 4: Press, TV Shows, Radio Shows, Gallery
<?php
//get current page ID
$the_id = get_the_ID();
$args = array(
'child_of' => $the_id,
'title_li' => '',
'parent' => $the_id,
'sort_order' => 'DESC',
'sort_column' => 'menu_order'
);
$pages = get_pages( $args );
$output = '';
foreach($pages as $value){ ?>
<div class="col-xs-12 col-sm-6">
<div class="row">
<div class="col-xs-12">
<?php
$thumb = get_the_post_thumbnail( $value->ID, $attr = ' img-responsive' );
$output .= "<div class='thumbnailcontainer'><a href='" . $value->post_name . "' >" . $thumb . "<span>" . $value->post_title . "</span></a></div>";
echo $output;
?>
</div>
</div>
</div>
<?php } ?>
I tried limiting the posts_per_page to 1 but no difference.
Replace the $output .= with $output = on line 24. :) I think that should solve it.
or what you could try is:
<?php
//get current page ID
$the_id = get_the_ID();
$args = array(
'child_of' => $the_id,
'title_li' => '',
'parent' => $the_id,
'sort_order' => 'DESC',
'sort_column' => 'menu_order'
);
$pages = get_pages( $args );
$output = '';
foreach($pages as $value) {
$thumb = get_the_post_thumbnail( $value->ID, $attr = ' img-responsive' );
$output .= "<div class='thumbnailcontainer'><a href='" . $value->post_name . "' >" . $thumb . "<span>" . $value->post_title . "</span></a></div>";
}
?>
<div class="col-xs-12 col-sm-6">
<div class="row">
<div class="col-xs-12">
<?=$output?>
</div>
</div>
</div>
Btw, try to avoid mixing PHP and HTML as much as possible! ;)
I'm building a custom WordPress widget/plugin to return the title and excerpt of a post within a permalink to the article. I am almost there but am struggling with getting the excerpt to show. Here is my current code:
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$categories = get_the_category($recent["ID"]);
$excerpt = apply_filters('get_the_excerpt', $recent->post_excerpt);
echo '<a class="m-item diet-and-nutrition" href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" > <div class="pix"></div><div class="eyebrow"> <b>' . $categories[0]->name . '</b> / '. $recent["post_date"].'</div> <figure>' . get_the_post_thumbnail($recent["ID"], 'full') . '<figcaption class="center"> <span> <h4>'. $recent["post_title"].'</h4> </span> <span> <p>'. $excerpt .'</p> </span> </figcaption> </figure> </a>';
}
?>
It's an array so:
$recent->post_excerpt
should be:
$recent['post_excerpt'];
I am trying to create news paper web site and need help to show the content of recent post and title of old posts only... here is my code
<?php
$args = array(
'orderby' => 'id',
'hide_empty' => 1,
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) { ?>
<div class="newsdiv">
<?php
echo '<center><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></center> <br/> ';
$post_args = array(
'numberposts' => 3,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {?>
<?php echo"*";the_title(); ?>
<div class="entry">
<?php the_content(); ?>
</div>
<?php
}
echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>Read more </a></dd>';
echo '</dl>';
?>
</div>
<?php
}
?>
you can try the codex previous post
<?php previous_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ); ?>
you can use it in simpler and/or complex way something like this.
<?php previous_post_link(); ?>
for more info you can go through with codex http://codex.wordpress.org/Function_Reference/previous_post_link
I have an area of my index.php file that I'm customizing in a theme whereby I would like to display the most recent post and some additional meta. I was able to pull the featured image, post title (and link), & the date; but not the author. I've tried various examples from the WP boards with no luck. The latest attempt below:
<?php
$args = array( 'numberposts' => '1');
$recent_posts = wp_get_recent_posts( $args );
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
//$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
foreach( $recent_posts as $recent ){
$post_author = get_user_by( 'id', $recent->post_author );
echo '<div class="full-width" id="featured-post" style="background-image: url('. $feat_image .')">';
echo '<div class="row featured-post-meta"><div class="small-8 columns">';
echo '<h2><a href="' . get_permalink($recent["ID"]) . '" title="Read: '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a></h2>';
echo '<p>'. $post_author->display_name .' | '. get_the_time('F jS, Y') .'</p>';
echo '<a class="read-post" href="'. get_permalink($recent["ID"]) .'">Read the post</a>';
echo '</div></div></div>';
}
?>
Inside your foreach loop:
$post_author = get_user_by( 'id', $recent['post_author'] );
This gets you the user object which looks like what you were trying to do with $curauth.
You could then output $post_author->display_name in place of $curauth.
echo '<p>'. $post_author->display_name .' ...