increment id for dynamically generated div - php

I want to dynamically generate video player container divs that will have a unique id. The elements are generated through Visual Composer in WordPress and the final html should look something like this:
<div style="width: 100%; display: inline-block; position: relative;">
<div style="margin-top: '. $custom_margin .'"></div>
<div id="player_1" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>
</div>
<div style="width: 100%; display: inline-block; position: relative;">
<div style="margin-top: '. $custom_margin .'"></div>
<div id="player_2" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>
</div>
<div style="width: 100%; display: inline-block; position: relative;">
<div style="margin-top: '. $custom_margin .'"></div>
<div id="player_3" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>
</div>
The user should be able to add as many div elements with unique ids as he likes. This is how my php looks right now:
EDIT: The function is called for every single video container
public function vc_custvideo_html($atts){
extract(
shortcode_atts(
array(
'custom_width' => '',
'custom_height' => '',
),
$atts
)
);
$percent = $custom_height / $custom_width;
$custom_margin = number_format( $percent * 100, 2 ) . '%';
$html = '';
$html.= '<div style="width: 100%; display: inline-block; position: relative;">';
$html.= '<div style="margin-top: '. $custom_margin .'"></div>';
$html.= '<div id="player_" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>';
$html.= '</div>';
return $html;
}
I can understand that i have to use a foreach loop to generate the unique ids but i'm really new at php and I need help.

Something like that
$i = 0;
foreach ($playerInstance as $k => $currPlayer {
vc_custvideo_html($currPlayer, $i)
$i++;
}
public function vc_custvideo_html($atts, $index){
extract(
shortcode_atts(
array(
'custom_width' => '',
'custom_height' => '',
),
$atts
)
);
$percent = $custom_height / $custom_width;
$custom_margin = number_format( $percent * 100, 2 ) . '%';
$html = '';
$html.= '<div style="width: 100%; display: inline-block; position: relative;">';
$html.= '<div style="margin-top: '. $custom_margin .'"></div>';
$html.= '<div id="player_'.$index.'" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>';
$html.= '</div>';
return $html;
}
Or with a for loop maybe

Assuming the function is called once per Video, use an outer foreach and a counter:
$counter=1;
foreach($arrayOfVideos as $video){
$this->vc_custvideo_html($video, $counter);
$counter++;
}
And inside your function, just use the $counter variable:
// [...]
$html.= '<div style="margin-top: '. $custom_margin .'"></div>';
$html.= '<div id="player_"' . $counter . ' style="position:absolute;top:0;left:0;right:0;bottom:0"></div>';
$html.= '</div>';

Related

Display PHP While Loop Horizontally & Vertically Using Flexbox

Having a bit of an issue here. Could use some insight. I'm displaying user created events to visitors to my website. I'm using a while loop to display everything that hasn't not yet already passed. My goal is to display two separate events right next to each other, then another two below that and so on. Currently I'm using the flex box css property to achieve this, but it's only displaying the output vertically and not the way I want it to, meaning it's only putting one event per line. Here is my current output for displaying the events.
include 'db_connect.php';
$event_type = $_POST['event_type'];
$state = $_POST['state'];
$current_date = date("Y-m-d");
$sql = "SELECT * FROM events WHERE event_type LIKE '%".$event_type."%' AND state LIKE '%".$state."%' AND end_date > '$current_date' ORDER By end_date ASC";
$result = mysqli_query($conn, $sql);
if (isset($_POST['search-events']) && !empty($event_type) && !empty($state)) {
while($row = mysqli_fetch_array($result)) {
$event_name= $row['event_name'];
$image = $row['image'];
$start_date = $row['start_date'];
$end_date = $row['end_date'];
$start_time = $row['start_time'];
$end_time = $row['end_time'];
$street = $row['street'];
$city = $row['city'];
$state = $row['state'];
$zip_code = $row['zip_code'];
$id = $row['event_id'];
echo '<div class="filter-wrap">';
echo '<div id="filter-boxes">';
echo '<div id="list_image"><img src="'.$image.'"></div>';
echo '<div id="list_name">'.$event_name.'</div>';
echo '<div id="list_date">'.$start_date. ' - ' .$end_date. '</div>';
echo '<div id="list_time">' .$start_time. ' - ' .$end_time. '</div>';
echo '<div id="list_location">'.$street.''.$city.''.$state.''.$zip_code.'</div>';
echo '</div>';
echo '</div>';
}
}
Then there's the css that I'm using.
.filter-wrap {
display: flex;
flex-direction: row;
padding: 10px;
justify-content: center;
align-items: center;
}
#filter-boxes {
border: 2px solid #999;
text-align: center;
width: 50%;
height: 150px;
}
As you can see, I'm using the flex property inside the container that holds each of the individual boxes that holds each event. I have the flex direction set to row since I want it to display horizontally, then go the next line after it runs out of room on each line.
I tried a few things. I tried switching to the css column count property but didn't get the results I was expecting. I honestly probably didn't tweak with that property enough, but I have my heart set on the flex box property. I also tried setting the flex direction to column and also tried adding an inline-block display property to the boxes that are suppose to repeat on the while loop with each event. I'm finding this online that are kind of similar to my issue, but not quite. One uses javascript, but this can obviously also be accomplished somehow with php. I also found several articles talking about centering the content using flexbox, which is not the goal here.
Try move your .filter-wrap div element to outside of the while() {} loop.
Your current coding:
while() {
echo '<div class="filter-wrap">';
echo '<div id="filter-boxes">';
// content goes here...
echo '</div>';
echo '</div>';
}
will result in following structure where each .filter-wrap only container a single child .filter-boxes, which will always results in vertical presentation:
<div class="filter-wrap">
<div id="filter-boxes"> content </div>
</div>
<div class="filter-wrap">
<div id="filter-boxes"> content </div>
</div>
<div class="filter-wrap">
<div id="filter-boxes"> content </div>
</div>
For horizontal presentation, the correct structure should be one .filter-wrap consists of multiple .filter-boxes childs:
<div class="filter-wrap">
<div id="filter-boxes"> content </div>
<div id="filter-boxes"> content </div>
<div id="filter-boxes"> content </div>
</div>
So you can try change your coding to:
echo '<div class="filter-wrap">';
while() {
echo '<div id="filter-boxes">';
// content goes here...
echo '</div>';
}
echo '</div>';
Snippet for demo to you the coding logic result. It is in JS but you just have to apply the same in your PHP
Hope it helps and Happy coding!
var result_1 = document.getElementById('result_1');
var result_2 = document.getElementById('result_2');
var content_1 = '';
var content_2 = '';
content_2 = '<div class="filter-wrap">';
for (var i = 1; i <= 5; i++)
{
// result 1
content_1 += '<div class="filter-wrap"> <div class="filter-boxes">content ' + i + '</div> </div>';
// result 2
content_2 += '<div class="filter-boxes">content ' + i + '</div>';
}
content_2 += '</div>';
result_1.insertAdjacentHTML('beforeend', content_1);
result_2.insertAdjacentHTML('beforeend', content_2);
.filter-wrap {
display: flex;
flex-direction: row;
flex-wrap: wrap;
padding: 10px;
justify-content: center;
align-items: center;
}
.filter-boxes {
border: 2px solid #999;
text-align: center;
width: 50%;
height: 50px;
/* for two columns display */
max-width: 49%;
}
#result_1,
#result_2 {
background-color: lightgray;
border: 1px dashed black;
margin: 3px;
}
result 1
<div id="result_1"></div>
result 2
<div id="result_2"></div>

Place 2 PHP generated HTML tables side by side

I have generated 2 HTML tables with PHP. The first table is always ontop of the 2nd table. I can't seem to get them to be side by side. the 2nd one is always under the first table.
I've also tried adding an html style of float left as well as inline in the html for the tables and it still doesn't come out side by side. Any help would be greatly appreciated!
//add table for bids and asks
function build_table($bidarray){
// start table
$html = '<table style="display: inline-block;">';
// header row
$html .= '<tr>';
foreach($bidarray[0] as $key=>$value){
$html .= '<th>' . htmlspecialchars($key) . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $bidarray as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . htmlspecialchars($value2) . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
$bidarray = array(
array('Company'=>'cardsltd', 'Min Qty'=>'5', 'Max Qty'=>'10', '$/box'=>'5.00'),
);
$askarray = array(
array('Company'=>'comp', 'Min Qty'=>'4', 'Max Qty'=>'9', '$/box'=>'4.00'),
);
echo build_table($bidarray) . build_table($askarray) ;
You need to use <div>s.
Take a <div>
Put two <div>s in it containing a table each.
<div>
<div style="float:left; width: 49%">
<table>
...
</table>
</div>
<div style="float:left; width: 49%">
<table>
...
</table>
</div>
</div>
This will put your tables side by side.
Also, we can change/adjust/manage widths.
49% is just for demo purposes.
PHP Code:
//define class for table
$html = '<div class="half-width"><table> ...';
CSS code:
.half-width
{
position: relative;
width: 100%;
padding-right: 15px; /*optional*/
padding-left: 15px; /*optional*/
-ms-flex: 0 0 50%;
flex: 0 0 50%;
max-width: 50%;
}
Also you can add float:right;width:50% for defined class (here class name is half-width)

How do I align a list of links/picture on the right side of the page, and stop them from overlapping?

I want to generate a list of links to other pages for my website (using a mix of PHP/HTML/CSS) and align them on the right side of the page without them overlapping. I am able to generate the links/pictures, but the problem I am having is they overlap on top of each other when I try to use position absolute.
<style>
body{
background: lightblue;
margin: 25px;
}
.title{
font-size: 20px;
}
.recipe{
width: 60%;
}
.related{
position: absolute;
float: right;
right: 10px;
width: 25%;
list-style-position: inside;
}
.a{
float: right;
right: 5px;
}
.relatedImages{
}img{
width: 15%;
height: 17%;
}
</style>
<title><?php $recipeInfo['title']; ?></title>
<body>
<br>
<?php
//Title and image of recipe
echo '<br><br><div class="title">' .$recipeInfo['title']. '</div><br>
<div class="mainImage"><image src="' . $recipeInfo['image']. '"> </div>
<br><h2> Ingredients </h2>';
//Unfinished (Needs to be styled correctly)
//Generating related links with clickable images
for($r = 0; $r < $relatedLinks[$r]; $r++){
echo '<div class = "related">
<a href = "recipeInfo.php?id='.$relatedLinks[$r]['id']. '">'.$relatedLinks[$r]['title'].'<br>
<image src = "https://spoonacular.com/recipeImages/' . $relatedLinks[$r]['image'] . '"></a>
<br>
</div>';
};
;
//Loop that generates a list of the ingredients used
for($i = 0; $i < $recipeInfo['extendedIngredients'][$i]; $i++){
$amount = $recipeInfo['extendedIngredients'][$i]['amount'];
$unit = $recipeInfo['extendedIngredients'][$i]['unit'];
$ingrName = $recipeInfo['extendedIngredients'][$i]['name'];
echo '<div class = "ingredients">' . $amount , " " , $unit , " " , $ingrName .' </div>';
}
//Instructions with error handling for no instructions found
$instructions = $recipeInfo['instructions'];
if($instructions == ""){
$instructions = "Whoops, there are no available instructions for this recipe.";
}
echo '<br><h2> Insructions </h2>
<div class="recipe">' . $instructions . '</div><br>';
//Unfinished, but will hopefully print a better list of instructions than just a dense paragraph
//for($j = 0; $j < sizeOf($recipeInstr); $j++){
// echo '<h3>' .$recipeInstr[$j]['name'].'</h3>';
// for($n = 0; $n < $recipeInstr[$j]['steps']; $n++){
// echo '<div class="instruction">'. $n , " " , $recipeInstr[$j]['steps'][$n]['step'] . '<div>';
// }
//}
?>
</body>
</html>
The intended effect I am going for is one similar to how youtube has related videos on the right side of the page.
Well cou could try this code:
<body>
<br>
<div class="maincontent">
<div class="main">
<?php
//Title and image of recipe
echo '<br><br><div class="title">' .$recipeInfo['title']. '</div><br>
<div class="mainImage"><image src="' . $recipeInfo['image']. '"> </div>
<br><h2> Ingredients </h2>';
//Loop that generates a list of the ingredients used
for($i = 0; $i < $recipeInfo['extendedIngredients'][$i]; $i++){
$amount = $recipeInfo['extendedIngredients'][$i]['amount'];
$unit = $recipeInfo['extendedIngredients'][$i]['unit'];
$ingrName = $recipeInfo['extendedIngredients'][$i]['name'];
echo '<div class = "ingredients">' . $amount , " " , $unit , " " , $ingrName .' </div>';
}
//Instructions with error handling for no instructions found
$instructions = $recipeInfo['instructions'];
if($instructions == ""){
$instructions = "Whoops, there are no available instructions for this recipe.";
}
echo '<br><h2> Insructions </h2>
<div class="recipe">' . $instructions . '</div><br>';
//Unfinished, but will hopefully print a better list of instructions than just a dense paragraph
//for($j = 0; $j < sizeOf($recipeInstr); $j++){
// echo '<h3>' .$recipeInstr[$j]['name'].'</h3>';
// for($n = 0; $n < $recipeInstr[$j]['steps']; $n++){
// echo '<div class="instruction">'. $n , " " , $recipeInstr[$j]['steps'][$n]['step'] . '<div>';
// }
//}
?>
</div>
<div class="sidelinks">
<?php
//Unfinished (Needs to be styled correctly)
//Generating related links with clickable images
for($r = 0; $r < $relatedLinks[$r]; $r++){
echo '<div class = "related">
<a href = "recipeInfo.php?id='.$relatedLinks[$r]['id']. '">'.$relatedLinks[$r]['title'].'<br>
<image src = "https://spoonacular.com/recipeImages/' . $relatedLinks[$r]['image'] . '"></a>
<br>
</div>';
};
;
</div>
</div>
</body>
</html>
and css code:
<style>
.maincontent {
display: grid;
grid-template-columns: 70% 30%;
}
body{
background: lightblue;
margin: 25px;
}
.title{
font-size: 20px;
}
.recipe{
width: 60%;
}
.related{
list-style-position: inside;
}
.a{
float: right;
right: 5px;
}
.relatedImages{
}img{
width: 15%;
height: 17%;
}
</style>
this should work, i couldnt test it

Custom HTML for WordPress Image Gallerys, only outputs half the function

I took the following code from an example and adjusted it so a standard gallery in wordpress would output as a Flexslider and Carousel. I can output one of them just fine, but I added additional *outputs for a Carousel as well, and now only the Carousel prints out. Any help on how I can get the whole thing to output would be appreciated
add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr) {
global $post;
if (isset($attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
if (!$attr['orderby'])
unset($attr['orderby']);
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ('RAND' == $order) $orderby = 'none';
if (!empty($include)) {
$include = preg_replace('/[^0-9,]+/', '', $include);
$_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
$attachments = array();
foreach ($_attachments as $key => $val) {
$attachments[$val->ID] = $_attachments[$key];
}
}
if (empty($attachments)) return '';
// Here's your actual output, you may customize it to your need
$output = "<div class=\"wordpress-gallery\">\n";
$output = "<div id=\"sliding\" class=\"flexslider flexslider--post-content\">\n";
//$output .= "<div class=\"preloader\"></div>\n";
$output .= "<ul class=\"slides flexslider__slides\">\n";
// Now you loop through each attachment
foreach ($attachments as $id => $attachment) {
// Fetch the thumbnail (or full image, it's up to you)
// $img = wp_get_attachment_image_src($id, 'medium');
// $imgThumbnail = wp_get_attachment_image_src($id, 'thumbnail');
$img = wp_get_attachment_image_src($id, 'full');
$output .= "<li class=\"slide flexslider__slide cover\">\n";
$output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";
$output .= "</li>\n";
}
$output .= "</ul>\n";
$output .= "</div>\n";
$output = "<div id=\"carousel\" class=\"flexslider flexslider--post-content-carousel\">\n";
$output .= "<ul class=\"slides flexslider__slides\">\n";
foreach ($attachments as $id => $attachment) {
$imgThumbnail = wp_get_attachment_image_src($id, 'thumbnail');
$output .= "<li >\n";
$output .= "<img src=\"{$imgThumbnail[0]}\" alt=\"\" />\n";
$output .= "</li>\n";
}
$output .= "</ul>\n";
$output .= "</div>\n";
$output .= "</div>\n";
return $output;
}
the html that gets outputed so far (it doesn't output the #sliding div, only the #carousel):
<div id="carousel" class="flexslider flexslider--post-content-carousel">
<div class="flex-viewport" style="overflow: hidden; position: relative;">
<ul class="slides flexslider__slides" style="width: 1400%; transition-duration: 0s; transform: translate3d(0px, 0px, 0px);">
<li class="flex-active-slide" style="width: 210px; float: left; display: block;">
<img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-300x300.jpg" alt="" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-1-300x300.jpg" alt="" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-2-300x300.jpg" alt="" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-3-300x300.jpg" alt="" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-4-300x300.jpg" alt="" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-5-300x300.jpg" alt="" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="//localhost:3002/test-site/wp-content/uploads/2016/01/test-image-6-300x300.jpg" alt="" draggable="false">
</li>
</ul>
</div>
<ul class="flex-direction-nav">
<li class="flex-nav-prev"><a class="flex-prev flex-disabled" href="#" tabindex="-1">Previous</a></li>
<li class="flex-nav-next"><a class="flex-next" href="#">Next</a></li>
</ul>
</div>
You have a few errors, that I can see.
// Here's your actual output, you may customize it to your need
$output = "<div class=\"wordpress-gallery\">\n";
$output = "<div id=\"sliding\" class=\"flexslider flexslider--post-content\">\n";
If you wish to append data, you need to put a period . before your equals = like this .= on the second $output assignment. Anytime you are appending but not putting .= after the variable name, you are basically reassigning it a new value instead of adding to it.
Also change this line:
$output = "<div id=\"carousel\" class=\"flexslider flexslider--post-content-carousel\">\n";
to:
$output .= "<div id=\"carousel\" class=\"flexslider flexslider--post-content-carousel\">\n";
The problem lies in the fact that you were basically resetting the variable with a new output instead of appending the data to it.
Hope this helps you.
Be sure to go through your code and double check variable assignments to make sure you are appending rather than resetting.
You have two errors in your code, that lies in following lines:
$output = "<div id=\"sliding\" class=\"flexslider flexslider--post-content\">\n";
[...]
$output = "<div id=\"carousel\" class=\"flexslider flexslider--post-content-carousel\">\n";
You are resetting $output variable two times, thus what was written to it before is lost, so you should replace them with:
$output .= "<div id=\"sliding\" class=\"flexslider flexslider--post-content\">\n";
[...]
$output .= "<div id=\"carousel\" class=\"flexslider flexslider--post-content-carousel\">\n";

Foreach not displaying multiple contents

I have a little problem in my Shortcode which I am creating for WordPress. The foreach loop is not displaying multiple posts each time. I'm not sure why is it doing this because if I var_dump the $post variable it shows that both the posts are available to that variable, can someone help me out please?
CODE:
function notes_shortcode($atts) {
global $post;
$atts = shortcode_atts( array( 'category' => $args["category"]), $atts, 'notes' );
$args = array( 'category_name' => $atts["category"]);
$posts = get_posts( $args );
$date = get_the_date( 'd', $post->ID );
$month = get_the_date( 'M', $post->ID );
foreach( $posts as $post ) {
setup_postdata($post);
$imgURL = getpostImage( $post->ID );
$title = get_the_title( $post->ID );
$content = substr(get_the_content() , 0, 125);
$post = '<div class="animated fadeInUp" data-animation="fadeInUp" data-delay="200" style="opacity: 0;">';
$post .= '<div class="col-md-4 bloglist">';
$post .= '<div class="post-content">';
$post .= '<div class="post-image">';
$post .= '<div class="flexslider blog-slider">';
$post .= '<div class="overlay" style="opacity: 0;"></div>';
$post .= '<div class="flex-viewport" style="overflow: hidden; position: relative;">';
$post .= '<ul class="slides" style="width: 800%; -webkit-transition: 0s; transition: 0s; -webkit-transform: translate3d(-350px, 0px, 0px);">';
$post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li class="flex-active-slide" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"></li>';
$post .= '</ul></div></div></div>';
$post .= '<div class="date-box"><span class="day">' . $date . '</span>';
$post .= '<span class="month">' . $month . '</span> </div>';
$post .= '<div class="post-text">';
$post .= '<h3>' . $title . '</h3>';
$post .= '<p> ' . $content . '<br>';
$post .= ' Read More</p></div></div></div></div>';
return $post;
}
}
add_shortcode( 'notes', 'notes_shortcode' );
function getpostImage($postid) {
if (has_post_thumbnail($post->ID)){
$imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID), 'thumbnail' );
$imgURL = $imgArray[0];
return $imgURL;
}
}
Thanks ..
Okay, I solved the problem. I used the WordPress Filters to hook the $buff variable and returned it outside the loop, below is the solution if anyone needs it.
function notes_shortcode($atts) {
global $post;
global $buf;
$atts = shortcode_atts( array( 'category' => $args["category"], 'posts_per_page' => $args["posts_per_page"]), $atts, 'notes' );
$args = array( 'category_name' => $atts["category"], 'posts_per_page' => $atts["posts_per_page"] );
$posts = get_posts( $args );
$date = get_the_date( 'd', $post->ID );
$month = get_the_date( 'M', $post->ID );
$buf = '';
$postHolder = array();
foreach( $posts as $post ) {
setup_postdata($post);
$imgURL = getpostImage( $post->ID );
$title = get_the_title( $post->ID );
$content = substr(get_the_content() , 0, 125);
$buf .= '<div class="animated fadeInUp" data-animation="fadeInUp" data-delay="200" style="opacity: 0;">';
$buf .= '<div class="col-md-4 bloglist">';
$buf .= '<div class="post-content">';
$buf .= '<div class="post-image">';
$buf .= '<div class="flexslider blog-slider">';
$buf .= '<div class="overlay" style="opacity: 0;"></div>';
$buf .= '<div class="flex-viewport" style="overflow: hidden; position: relative; max-width: 350px; max-height: 175px; padding-bottom: 15px; margin-bottom: 15px;">';
$buf .= '<ul class="slides" style="width: 800%; -webkit-transition: 0s; transition: 0s; -webkit-transform: translate3d(-350px, 0px, 0px);">';
$buf .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$buf .= '<li class="flex-active-slide" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$buf .= '<li style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$buf .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"></li>';
$buf .= '</ul></div></div></div>';
$buf .= '<div class="date-box"><span class="day">' . $date . '</span>';
$buf .= '<span class="month">' . $month . '</span> </div>';
$buf .= '<div class="post-text">';
$buf .= '<h3>' . $title . '</h3>';
$buf .= '<p> ' . $content . '<br>';
$buf .= ' Read More</p></div></div></div>';
$buf .= apply_filters( 'post_class', '</div>', $atts );
}
$buf .= apply_filters( 'post_class', '', $atts );
return $buf;
}
add_shortcode( 'notes', 'notes_shortcode' );
function getpostImage($postid) {
if (has_post_thumbnail($post->ID)){
$imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID), 'thumbnail' );
$imgURL = $imgArray[0];
return $imgURL;
}
}
Thankyou all for the help ..
You are returning the function, that means that your are making the function end. What you can do is send the array and then loop it later.Use the code below
function notes_shortcode($atts) {
global $post;
$atts = shortcode_atts( array( 'category' => $args["category"]), $atts, 'notes' );
$args = array( 'category_name' => $atts["category"]);
$posts = get_posts( $args );
$date = get_the_date( 'd', $post->ID );
$month = get_the_date( 'M', $post->ID );
$array = array();
foreach( $posts as $post ) {
setup_postdata($post);
$imgURL = getpostImage( $post->ID );
$title = get_the_title( $post->ID );
$content = substr(get_the_content() , 0, 125);
$post = '<div class="animated fadeInUp" data-animation="fadeInUp" data-delay="200" style="opacity: 0;">';
$post .= '<div class="col-md-4 bloglist">';
$post .= '<div class="post-content">';
$post .= '<div class="post-image">';
$post .= '<div class="flexslider blog-slider">';
$post .= '<div class="overlay" style="opacity: 0;"></div>';
$post .= '<div class="flex-viewport" style="overflow: hidden; position: relative;">';
$post .= '<ul class="slides" style="width: 800%; -webkit-transition: 0s; transition: 0s; -webkit-transform: translate3d(-350px, 0px, 0px);">';
$post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li class="flex-active-slide" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"></li>';
$post .= '</ul></div></div></div>';
$post .= '<div class="date-box"><span class="day">' . $date . '</span>';
$post .= '<span class="month">' . $month . '</span> </div>';
$post .= '<div class="post-text">';
$post .= '<h3>' . $title . '</h3>';
$post .= '<p> ' . $content . '<br>';
$post .= ' Read More</p></div></div></div></div>';
$array[] = $post;
}
return $array;
}
add_shortcode( 'notes', 'notes_shortcode' );
function getpostImage($postid) {
if (has_post_thumbnail($post->ID)){
$imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID), 'thumbnail' );
$imgURL = $imgArray[0];
return $imgURL;
}
}
Hope this helps you

Categories