Bootstrap Post Carousel blocking other PHP in page-template - php

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.

Related

How to concatenate increment operator to PHP

$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

Conditional Joomla module position and span

I am having trouble setting up a joomla conditional module position and span.
Although I understand HTML, I have trouble with php. According to the tutorials, I have to add
<?php if ($this->countModules( 'user1' )) : ?>
<div class="user1">
<jdoc:include type="modules" name="user1" style="rounded" />
</div>
<?php endif; ?>
Although when I add something like it, I receive an error.
The code that I want to change is:
<?php
//no direct accees
defined ('_JEXEC') or die('resticted aceess');
class Helix3FeatureTitle {
private $helix3;
public function __construct($helix){
$this->helix3 = $helix;
$this->position = 'title';
}
public function renderFeature() {
$app = JFactory::getApplication();
$menuitem = $app->getMenu()->getActive(); // get the active item
if($menuitem) {
$params = $menuitem->params; // get the menu params
if($params->get('enable_page_title', 0)) {
$page_title = $menuitem->title;
$page_title_alt = $params->get('page_title_alt');
$page_subtitle = $params->get('page_subtitle');
$page_title_bg_color = $params->get('page_title_bg_color');
$page_title_bg_image = $params->get('page_title_bg_image');
$style = '';
if($page_title_bg_color) {
$style .= 'background-color: ' . $page_title_bg_color . ';';
}
if($page_title_bg_image) {
$style .= 'background-image: url(' . JURI::root(true) . '/' . $page_title_bg_image . ');';
}
if( $style ) {
$style = 'style="' . $style . '"';
}
if($page_title_alt) {
$page_title = $page_title_alt;
}
$output = '';
$output .= '<div class="sp-page-title"'. $style .'>';
$output .= '<div class="container" id="pagetitle">';
$output .= '<div class="row">';
$output .= '<div class="col-md-6 col-sm-8">';
$output .= '<h1>'. $page_title .'</h1>';
if($page_subtitle) {
$output .= '<p class="lead">'. $page_subtitle .'</p>';
}
$output .= '</div>';
$output .= '<div class="col-md-6 col-sm-4 hidden-xs">';
$output .= '<jdoc:include type="modules" name="datetime" style="none" />';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
return $output;
}
}
}
}
and what I want to do is tell the template that when the module position 'datetime' is empty, the "title" position should have 12span and be "text-center".
Otherwise if module position 'datetime' has a module, the 'title' position should have 6span and be "text-left".
I have tried different methods and I receive different errors each time. This is why I did not mention a spcecific error. For example, if I use this method:
if($this->countModules('datetime')) { $output .= '<div class="col-md-6 col-sm-4 hidden-xs">'; $output .= '<jdoc:include type="modules" name="datetime" style="none" />'; $output .= '</div>'; };
I receive the following error:
Fatal error: Call to undefined method Helix3FeatureTitle::countModules() in ...
I would really appreciate some assistance. Thank you.
The problem is that you are trying to us countModules() in a place where it isn't going to work because that method is from a different class.
From the looks of it what this method is doing is generating the Joomla template.
So what you can do is
$output .="<?php if ($this->countModules( 'user1' )) : ?>";
$output .= '<div class="col-md-6 col-sm-4 hidden-xs">';
$output .= '<jdoc:include type="modules" name="datetime" style="none" />';
$output .= '</div>';
$output .= '<?php endif; ?>';
So that it will generate the code for the condition. I haven't tested because I don't have the class you are using but it should work.
Updated to fix quoting.

Wrapping every 3 elements in a loop leaves an empty wrapper

I am wrapping every 3 elements in my loop in a wrapper div like this:
$query = array(
'post_type' => 'post',
);
$i = 1;
$posts = new WP_Query( $query );
$out = '<div class="wrapper">';
if ($posts->have_posts()){
while ($posts->have_posts()){
$posts->the_post();
$out.= '<div class="content">
//content here
</div>';
if($i % 3 == 0) {
$out .= '</div><div class="wrapper">';
}
$i++;
}
}
$out .= '</div>';
wp_reset_postdata();
return '<section>'.$out.'</section>';
Which creates a good wrapping html minus one little thing that's bothering me:
<section>
<div class="wrapper">
<div class="content"></div>
</div>
<div class="wrapper">
<div class="content"></div>
</div>
<div class="wrapper"></div>
</section>
If I have exactly 6 posts (or any multiple of 3, and modulo is doing this like it should) I'll get an extra empty wrapper. Which is really not needed.
So what conditional should I include in my query to ensure that I don't get empty wrappers?
Add the wrapper inside:
$query = array(
'post_type' => 'post',
);
$i = 1;
$posts = new WP_Query( $query );
$out = '';
$endingNeeded = false;
if ($posts->have_posts()){
while ($posts->have_posts()){
if($i % 3 == 1) {
$out .= '<div class="wrapper">';
$endingNeeded = true;
}
$posts->the_post();
$out.= '<div class="content">
//content here
</div>';
if($i % 3 == 0) {
$out .= '</div>';
$endingNeeded = false;
}
$i++;
}
}
if($endingNeeded) {
$out .= '</div>';
}
wp_reset_postdata();
return '<section>'.$out.'</section>';

How can i get first image if the post has no post thumbnail in wordpress

I have created the sidebar widget for popular, recent and most commented posts in my theme. I have some posts which don't contain the image thumbnail.
This is the popular query posts for 5 posts in my widget
<?php if (!empty($popular_posts)) { ?>
<div class="tab-pane fade in active" id="popular">
<div class="row">
<!-- ********************************************** -->
<!-- Popular Posts Tab -->
<!-- ********************************************** -->
<?php
$YPE_options = get_option( 'YPE_sidebar_option_name' );
$popular = new WP_Query( array(
'posts_per_page' => $popular_limit,
'meta_key' => 'post_views_count', // this is function within functions.php for counting post veiews
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
while ( $popular->have_posts() ) : $popular->the_post();
$html = '<article>';
$html .= '<section class="bootstrap-nav-thumb">';
$html .= '<p>';
$html .= '<a href="' . get_permalink() . '">';
$html .= get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive '.$YPE_options['YPE_sidebar_PRC_thumb_style'].''));
$html .= '</a>';
$html .= '</p>';
$html .= '</section>';
$html .= '<aside class="bootstrap-title-info">';
$html .= '<p>';
$html .= ''.get_the_title().'';
$html .= '</p>';
$html .= '<p class="text-muted">' . get_the_date() . '||'. getPostViews(get_the_ID()) . '</p>';
$html .= '</aside>';
$html .= '</article>';
echo $html;
endwhile;
?>
</div> <!-- End row of popular posts -->
</div> <!-- End tab-pane of popular posts -->
<?php } ?>
how can i add conditional statement to this code
$html .= '<a href="' . get_permalink() . '">';
$html .= get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive '.$YPE_options['YPE_sidebar_PRC_thumb_style'].''));
$html .= '</a>';
Note: i want to say if has post thumbnail put the posts thumbnail and if not has the thumbnail put the first image instead of the post thumbnail
$thumb = get_the_post_thumbnail(get_the_ID());
if(!empty($thumb))
$image = $thumb;
else {
$image = '<img src="';
$image .= catch_that_image();
$image .= '" alt="" />'; }
And put your function as
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
Something like this could work:
$html .= '<a href="' . get_permalink() . '">';
if ( has_post_thumbnail() ) {
$html .= get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive '.$YPE_options['YPE_sidebar_PRC_thumb_style'].''));
} else {
$html .= "<img src='". echo wp_get_attachment_image_src(0,'thumbnail') .'" class="img-responsive ' .$YPE_options['YPE_sidebar_PRC_thumb_style'].' " />';
};
html .= '</a>';

wordpress ignores formatting when using template tags in functions.php

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();

Categories