Using IF Within php While Loop - php

i have this code below it is the loop for get popular posts in wordpress
$First_Img = '<img src="'.YPE_Catch_First_Image().'">';
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'));
$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;
i want use this code
html .= if(has_post_thumbnail()) {
echo get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
} else {
echo $First_Img;
};
instead of this code
$html .= get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
but the server show the error unexpected 'if' (T-IF) please help me how i can use the conditional statement when i has post thumbnail print it and if don't has post thumbnail print the first image?

Incorrect:
$html .= if(has_post_thumbnail()) {
echo get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
} else {
echo $First_Img;
};
Correct:
if(has_post_thumbnail()) {
$html .= get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
} else {
$html .= $First_Img;
};

Try this instead:
if(has_post_thumbnail()) {
$html .= get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
} else {
$html .= $First_Img;
};
The .= operator essentially means "append to". The line of code you originally had meant "append whatever get_the_post_thumbnail(...) returns to $html".
This way, you check your condition with the if statement, and append what you want.

Try a ternary operator:
$html .= has_post_thumbnail() ? get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive')) : $First_Img;

You should do it like this:
if(has_post_thumbnail()) {
$html .= get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
} else {
$html .= $First_Img;
};

Related

how to add pagination WordPress in functions.php

The blog have over 100 posts page. I would like to add pagination in WordPress functions.php but how do I add the code in
functions.php
/**
* Shortcode News
*/
add_shortcode( 'news', 'new_bubble' );
function new_bubble(){
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '20',
'paged' => $paged
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
$string .= '<div class="row">';
while( $query->have_posts() ){
$query->the_post();
$string .= '<div class="col-md-4">';
$string .= '<div class="card">';
$string .= '<a href="' . get_the_permalink() . '">';
$string .= '<div class="card-img" style="background-image: url(' . get_the_post_thumbnail_url() . ');">';
$string .= '</div>';
$string .= '</a>';
$string .= '<div class="card-body">';
$string .= '<p class="post-date">' . get_the_date( 'j F Y' ) . '</p>';
$string .= '<p class="post-title">';
$string .= '<a href="' . get_the_permalink() . '">';
$string .= '' . get_the_title() . '';
$string .= '</a>';
$string .= '</p>';
$string .= '<div class="post-footer">';
$string .= '<div class="author">';
$string .= ''. get_avatar( get_the_author_email(), '50' ) . '';
$string .= '<div class="author-meta">';
$string .= '<h5 class="author-post-name">By ' . get_the_author() . '</h5>';
$string .= '</div>';
$string .= '</div>';
$string .= '</div>';
$string .= '</div>';
$string .= '</div>';
$string .= '</div>';
}
$string .= '</div>';
}
wp_reset_postdata();
return $string;
}
Everything is work fine but I would like to add pagination button

using php to place image names within a folder to feed HTML img src inside bootstrap carousel

I have a folder containing images that I want to put inside a Bootstrap carousel. Instead of hardcode everything in my HTML, I thought to use PHP to scan the image files and create my carousel list dynamically.
After various attempts, my best achievement was having a carousel listing the names of my images (rather than the image themselves).
My code is below. I have two question about it:
(obviously) Why is it not working?
Is there a better way to separate such a long PHP function from the HTML code (like a call with include but that would work with functions)?
<div class="container-fluid">
<?php
$img_folder = './img/img_animazione/';
function carousel_loop($folder)
{
echo '<div id="myCarousel" class="carousel slide" data-ride="carousel">';
echo '<ol class="carousel-indicators">';
$i = -1;
$images = scandir($folder);
foreach($images as $image) {
$i++; //$i is the index of the current loop.
if ($i == 0) {
echo '<li data-target="#myCarousel" data-slide-to="' . $i . '" class="active"></li>';
} else {
echo '<li data-target="#myCarousel" data-slide-to="' . $i . '"</li>';
}
}
echo '</ol>';
echo '<div class="carousel-inner">';
foreach($images as $image) {
if ($image === reset($images)) {
echo '<div class="item active">';
echo '<img src="' . $image . '" alt="'. $image . '">';
echo '</div>';
} else {
echo '<div class="item">';
echo '<img src="' . $image . '" alt="'. $image . '">';
echo '</div>';
}
}
echo '</div>';
echo '<a class="left carousel-control" href="#myCarousel" data-slide="prev">';
echo '<span class="glyphicon glyphicon-chevron-left"></span>';
echo '<span class="sr-only">Previous</span>';
echo '</a>';
echo '<a class="right carousel-control" href="#myCarousel" data-slide="next">';
echo '<span class="glyphicon glyphicon-chevron-right"></span>';
echo '<span class="sr-only">Next</span>';
echo '</a>';
echo '</div>';
}
carousel_loop($img_folder);
?>
</div>
That's not the way to use functions. You can't echo like that from inside the function. You have to build up the HTML inside the function and return it. Then either just echo the function or assign what it returns to a variable.
So like this:
<?php
function carousel_loop($folder)
{
html = "";
html .= '<div id="myCarousel" class="carousel slide" data-ride="carousel">';
html .= '<ol class="carousel-indicators">';
$i = -1;
$images = scandir($folder);
foreach($images as $image) {
$i++; //$i is the index of the current loop.
if ($i == 0) {
html .= '<li data-target="#myCarousel" data-slide-to="' . $i . '" class="active"></li>';
} else {
html .= '<li data-target="#myCarousel" data-slide-to="' . $i . '"</li>';
}
}
html .= '</ol>';
html .= '<div class="carousel-inner">';
foreach($images as $image) {
if ($image === reset($images)) {
html .= '<div class="item active">';
html .= '<img src="' . $image . '" alt="'. $image . '">';
html .= '</div>';
} else {
html .= '<div class="item">';
html .= '<img src="' . $image . '" alt="'. $image . '">';
html .= '</div>';
}
}
html .= '</div>';
html .= '<a class="left carousel-control" href="#myCarousel" data-slide="prev">';
html .= '<span class="glyphicon glyphicon-chevron-left"></span>';
html .= '<span class="sr-only">Previous</span>';
html .= '</a>';
html .= '<a class="right carousel-control" href="#myCarousel" data-slide="next">';
html .= '<span class="glyphicon glyphicon-chevron-right"></span>';
html .= '<span class="sr-only">Next</span>';
html .= '</a>';
html .= '</div>';
return html;
}
?>
<div class="container-fluid">
<?php
$img_folder = './img/img_animazione/';
echo carousel_loop($img_folder);
?>
</div>
Thanks to #Abraham A. answer, I finally was able to come up with a working (still "dirt") solution. I'll post the code below. The problems in my initial code were:
Using echo within a function instead of returning a (very long) HTML string in the function and "echoing" it from outside.
<img src= obviously wants a source and not just a filename. My first attempt was <img src= . $image, the correct form is <img src= . $folder . DIRECTORY_SEPARATOR . $image
It seems like [scandir()][1] creates an array with all the filenames within a folder AND ".", ".." (current and parent folder) in it. I got rid of the latters via $allFiles = scandir($folder); $images = array_diff($allFiles, array('.', '..')); as suggested here.
My working code is this one now.
<?php
function carousel_loop($folder)
{
$html = "";
$html .= '<div id="myCarousel" class="carousel slide" data-ride="carousel">';
$html .= '<ol class="carousel-indicators">';
$i = -1;
$allFiles = scandir($folder); // list all files in folders
$images = array_diff($allFiles, array('.', '..')); // drop current and parent folder from array list (".", "..")
foreach($images as $image) {
$i++; //$i is the index of the current loop.
if ($i == 0) {
$html .= '<li data-target="#myCarousel" data-slide-to="' . $i . '" class="active"></li>';
} else {
$html .= '<li data-target="#myCarousel" data-slide-to="' . $i . '"></li>';
}
}
$html .= '</ol>';
$html .= '<div class="carousel-inner">';
foreach($images as $image) {
if ($image === reset($images)) {
$html .= '<div class="item active">';
$html .= '<img src="' . $folder . DIRECTORY_SEPARATOR . $image . '" alt="' . $image . '" style="width:100%;">';
$html .= '</div>';
} else {
$html .= '<div class="item">';
$html .= '<img src="' . $folder . DIRECTORY_SEPARATOR . $image . '" alt="' . $image . '" style="width:100%;">';
$html .= '</div>';
}
}
$html .= '</div>';
$html .= '<a class="left carousel-control" href="#myCarousel" data-slide="prev">';
$html .= '<span class="glyphicon glyphicon-chevron-left"></span>';
$html .= '<span class="sr-only">Previous</span>';
$html .= '</a>';
$html .= '<a class="right carousel-control" href="#myCarousel" data-slide="next">';
$html .= '<span class="glyphicon glyphicon-chevron-right"></span>';
$html .= '<span class="sr-only">Next</span>';
$html .= '</a>';
$html .= '</div>';
return $html;
}
?>
<div class="container">
<?php
$img_folder = './img/img_animazione';
echo carousel_loop($img_folder);
?>
</div>

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.

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