$i = 0;
while ($slider_query->have_posts()) {
$slider_query->the_post();
$html ='<div class= "col-lg-3 order-lg-'.$i.' col-md-6 p-0">';
$html .= '<div class= "ss-pic">';
$html .= '<img src="'.get_the_post_thumbnail_url(get_the_ID(), 'full').'"/>';
$html .='</div>';
$html .='</div>';
$html ='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
$html .= '<div class= "ss-text">';
$html .= '<h3>'.get_the_title().'</h3>';
$html .= get_the_content();
$html .= 'Explore';
$html .='</div>';
$html .='</div>';
I want to pre-increment the $i. Can anyone please help me out? I know its a basic, but I have a bit of confusion over the concatenation
So firstly you can pre-increment an integer variable by prepending ++ to it, e.g.
++$i
. That will work for you, e.g.
'<div class= "col-lg-3 order-lg-'.++$i.' col-md-6 p-0">'
However, you also have another problem which is preventing you from seeing the results as you are expecting. Every time you write $html = - which is twice within your loop, you overwrite the contents of $html. The previous contents of the variable are destroyed. Therefore, at whatever time you come to echo $html to the output, you'll only ever see the last version.
You need to concatenate all the HTML together into a single string without overwriting it.
Put
$html = "";
just before the loop starts, and then change
$html ='<div class= "col-lg-3 order-lg-'.$i.' col-md-6 p-0">';
to
$html .='<div class= "col-lg-3 order-lg-'.++$i.' col-md-6 p-0">';
(note the .= there, and also the ++$i which you forgot)
and lower down, change
$html ='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
to
$html .='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
(again just changing the = to .=.)
Demo: http://sandbox.onlinephpfunctions.com/code/7e1ac7b039867eedd22d90dfcdc03e8990419a8f
$i = 0;
while ($slider_query->have_posts()) {
$slider_query->the_post();
$html ='<div class= "col-lg-3 order-lg-'.$i+1.' col-md-6 p-0">';
$html .= '<div class= "ss-pic">';
$html .= '<img src="'.get_the_post_thumbnail_url(get_the_ID(), 'full').'"/>';
$html .='</div>';
$html .='</div>';
$html ='<div class= "col-lg-3 order-lg-"'.$i+2.'"col-md-6 p-0">';
$html .= '<div class= "ss-text">';
$html .= '<h3>'.get_the_title().'</h3>';
$html .= get_the_content();
$html .= 'Explore';
$html .='</div>';
$html .='</div>';
I dont think you can increase a value in one loop. you need to add it with numeric value since you want a different value for each div in one loop
Related
I'm generating html code with a string this way
foreach ($busquedas as $busqueda) {
$checked = $busqueda->porDefecto ? "checked" : ' ';
$radio_html = "<input type='radio' radio-id='".$busqueda->id."' name='default' class='radio-default' value='Por defecto' checked =".$checked." > Por defecto";
$html .= "<div class='col-md-12 search-div'>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'>".$busqueda->nom."</div>";
$html .= "<div class='col-md-6'>".$_SESSION['user_rol'] == 0?$radio_html:''."</div>";//if $radio_html is shwon the paren div col-md-6 is not shown
$html .="</div>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'><button class='btn btn-default load_search_btn' search_id='".$busqueda->id."'>Cargar</button></div>";
$html .= "<div class='col-md-6'><button class='btn btn-default delete_search_btn' search_id='".$busqueda->id."'>Eliminar</button></div>";
$html .="</div>";
$html .="</div>";
}
When $radio_tml is shown the parent div with class col-md-6 is not on the code but if $radio_html is shown the div is shown too,I thought some tag is not closed but I can't see it
For me your code implies that when you have
$_SESSION['user_rol'] == 0
the div section after that is not closed
You should add brackets as IncredibleHat was saying or add the </div> in the if statement (and not only in the else)
I have the following website: https://www.ajagrawal.com
Currently in my website, I am trying to edit the hero section and replace the name, email and button fields with a custom wordpress form that was made with the plugin. Right now the emails are stored in Wordpress, but I want them in Mailchimp.
I know which code in my theme that form pertains to which is the following:
if(function_exists('newsletter_form')):
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= '<form method="post" action="'.home_url('/').'?na=s" onsubmit="return newsletter_check(this)">';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="text" name="nn" required="" placeholder="'.esc_html($name_placehodler).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-user"></span></div>';
$output .= '</div>';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="email" name="ne" required="" placeholder="'.esc_html($email_placeholder).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-envelope"></span></div>';
$output .= '</div>';
$output .= '<input class="newsletter-submit c-btn type-1 size-4 full" type="submit" value="'.esc_html($btn_text).'">';
$output .= '</form>';
$output .= '</div>';
$output .= '</div>';
endif;
Here is my mailchimp shortcode that represents the form.
[mc4wp_form id="2198"]
I've tried editing the code and inserting it multiple ways and even ended up crashing the site and still no luck. Considering my goal, is this the right approach (putting the shortcode in the PHP code)?
If so, how can this be done?
You can use the do_shortcode function.
Examples in the documentation:
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
// Store the short code in a variable.
$var = do_shortcode( '' );
echo $var;
So in your case something like:
echo do_shortcode( '[mc4wp_form id="2198"]' );
should do the trick.
EDIT:
Something like this should work:
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= do_shortcode( '[mc4wp_form id="2198"]' );
$output .= '</div>';
$output .= '</div>';
Then do whatever you want with output - probably will have to echo it...
I have created a post carousel within a WordPress page-template, but all other PHP (such as Advanced Custom Fields content and such) get blocked. The wp_debug shows no errors and I can't find any error within the code.
Below is the code I have used to create the carousel with recent posts from a custom post type:
When I entirely remove the slider all PHP below it does work/load, but I can't seem to find a mistake in the code.
<div class="carousel-inner" role="listbox">
<?php
$i = 2;
global $post;
$args = array(
'numberposts' => -1,
'post_type' => 'work',
'orderby' => 'date',
'order' => 'ASC',
);
$myposts = get_posts($args);
if($myposts):
$chunks = array_chunk($myposts, $i);
$html = "";
foreach($chunks as $chunk) {
($chunk === reset($chunks)) ? $active = "active" : $active = "";
$html .= '<div class="item '.$active.'">';
foreach($chunk as $post) {
$html .= '<div id="timeline-item" class="col-lg-6 col-md-6 col-sm-6 col-xs-6"><div><h6 style="text-align: left;">';
$html .= get_the_date('Y');
$html .= '</h6><h2 style="text-align: left;">';
$html .= get_the_title();
$html .= '</h2><p style="text-align: left;">';
$html .= get_post_field('post_content');
$html .= '</p></div></div>';
};
$html .= '</div>';
};
echo $html;
endif;
?>
</div>
You are overriding $post. In WordPress never override $post
Try replacing this part.
foreach($chunk as $slide) {
$html .= '<div id="timeline-item" class="col-lg-6 col-md-6 col-sm-6 col-xs-6"><div><h6 style="text-align: left;">';
$html .= get_the_date('Y', $slide);
$html .= '</h6><h2 style="text-align: left;">';
$html .= get_the_title($slide);
$html .= '</h2><p style="text-align: left;">';
$html .= get_post_field('post_content', $slide);
$html .= '</p></div></div>';
};
Not sure if this will solve the rest of the problems. It might.
And if it doesn't at least your code will be a bit cleaner.
EDIT
Seeing the full script this is very likely the problem.
line 109 if( have_rows('skill-bars') will take the wrong $post which you broke.
I am using the following code to create a shortcode for use in the WYSIWYG.
function consultants( $atts, $content = null ) {
$output = '';
$consultant_query = new WP_Query('post_type=consultants&showposts=10');
if ($consultant_query->have_posts()) :
$output .= '<div class="col-md-12">';
while ($consultant_query->have_posts()) : $consultant_query->the_post();
$output .= '<div class="col-xs-12 col-sm-5 col-md-4 kam-tile-own-bg"><h1>' .the_title(). '</h1> ';
if(has_post_thumbnail())
{
$output .= get_the_post_thumbnail($post->ID,'wpbs-featured-avatar');
} else {
$output .= '
<img class="kam-avatar" width="62" height="62" src="'.get_template_directory_uri().'/images/avatar#2x.jpg" class="attachment-featured_image wp-post-image" alt="" />'; }
$output .= '</div>';
endwhile;
$output .= '</div>';
endif;
wp_reset_postdata();
return $output;
}
The code works fine - HOWEVER it fails on the .the_title(). it throws this at the top of the page it has no respect for the tags or the in which it is contained.
Many thanks
instead of the_title(); use get_the_title();
I have a problem with embeded html in foreach loop:
HTML:
<div class="head">
<div class="wins">
<div class="images">
<img src="images/1.jpg" alt="">
<img src="images/2.jpg" alt="">
<img src="images/3.jpg" alt="">
</div>
<div class="Bar">
<span>1</span>Cat1
<span>2</span>Cat2
<span>3</span>Cat3
</div>
</div>
I want to print html with this function :
function Bar($array){
$box .= '<div class="images">';
foreach($array as $key => $value){
$box .= ' <img src="'.$value['image'].'" alt="">';
}
$box .= '</div>';
return $box;
}
I want to break the loop after <div class="images"> and continue loop after the <div class="Bar"> . But Im confusing about this issue. Please show me the right way.
Thanks in advance
Two options:
Run through the loop twice.
or
Store each section in a separate variable and merge them later on, i.e.
function Bar($array){
foreach($array as $key => $value){
$images .= ' <img src="'.$value['image'].'" alt="">';
$bar .= ' <span>....';
}
$box .= '<div class="images">';
$box .= $images;
$box .= '</div>';
$box .= '<div class="Bar">';
$box .= $bar;
$box .= '</div>';
return $box;
}
function Bar($array){
$divImage=$divBar=array();
foreach($array as $key => $value){
$divImage[]= " <a href='#' ><img src='{$value['image']}' alt=''></a>";
$divBar[]= " <a href='#' rel='$key' ><span>$key</span>Cat$key</a>";
}
$divImage="<div class='images'>".implode("\r\n",$divImage)."</div>";
$divBar="<div class='bar'>".implode("\r\n",$divBar)."</div>";
$box="<div class='wins'>
$divImage
$divbar
</div>";
return $box;
}
maybe something like that?
$divimg = '<div class="images">';
$divbar = '<div class="Bar">';
foreach(...) {
$divimg .= ' ... ';
$divbar .= ' ... ';
}
$divimg .= '</div>';
$divbar .= '</div>';
$divimg .= $divbar;
return $divimg;