Conditional Joomla module position and span - php

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.

Related

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>';

Using IF Within php While Loop

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;
};

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

Issue on output's return value

Currently I am using a file with css and all.I have a problem as i am using php output between many html tags. The menu consist of the hard coded pages which is included with the php require_once. However, i also want to add in some pages from my database.
The Problem: The menu only shows 1 page name from my database when i have 2. It is displaying the 2nd page's name i have made. However clicking on it, it calls up the content from the 1st page.
The page name from my database is added using php output after the <ul id="menu-toc">
The page's content from my database is added at the bottom of the "The Page" section.
update: I've changed the placing of the while loop before the to after the
Pattern of css
<div class="bb-item" id="abscess">
<div class="content">
<div class="scroller">
Content
</div>
</div>
</div>
The Page
function find_condition() {
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE visible = 1 ";
$query .= "AND subject_id = 2 ";
$query .= "ORDER BY position ASC";
$page_set = mysqli_query($connection, $query);
confirm_query($page_set);
return $page_set;
}
$two = find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
}
echo $output;
?>
</ul>
</div>
<div class="bb-custom-wrapper">
<div id="bb-bookblock" class="bb-bookblock">
<?php require_once("abscess.php"); ?>
<?php require_once("apthousulcer.php"); ?>
<?php require_once("bad breath.php"); ?>
<?php
$two= find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
$content = $page['content'];
$output .= "<div class=\"bb-item\" id=\"";
$output .= $pagearray;
$output .= "\">";
$output .= "<div class=\"content\">";
$output .= "<div class=\"scroller\">";
$output .= "<h2>";
$output .= $pagearray;
$output .= "</h2>";
$output .= $content;
$output .= "</div>";
$output .= "</div>";
$output .= "</div>";
}
echo $output;
?>
You're starting a new <ul> each time through the while loop. You should just do that once, before the loop. You're also reinitializing $output each time through the loop, but not printing it in the loop. So you're just printing the last assignment of output after the loop is done.
$two = find_condition();
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
echo $output;
}
?>

adding for each clause into variable

I have the following variable $prod_list_contents which is a mix of html and php METHODS to display the products from my database:
$prod_list_contents .= '<div style="width:100%;">';
$prod_list_contents .= '';
while ($listing = tep_db_fetch_array($listing_query)) {
$rows++;
$prod_list_contents .= '
<div class="cat_prod_box">
<div class="image">
<div class="cat_image_table">
<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES_CAT . $listing['products_image'], $listing['products_name'], 255, 340, 'class="cat_image_round"') . '
<div class="hidden_thing">Click to View</div>
</a>
</div>
</div>
<div class="cat_product_info" >';
$prod_list_contents .= '<div class="span_style_num">Style: '. $listing['products_model'] . '</div>';
$prod_list_contents .= '<div class="span_colors">Colors: red, blue</div>';
$prod_list_contents .= '<div class="span_product_name">' . $listing['products_name'] . '</div>';
$prod_list_contents .= '<div class="span_price">CAD ' .$currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) .'</div>' ;
$prod_list_contents .= '</div></div>';
}
$prod_list_contents .= ' </div>' .
' '
;
echo $prod_list_contents;
I am trying to incorporate a color table into the same div that is being displayed through this same variable. My color table is derived through the following bit of php:
<div >
<?php
$ctr = 0;
$clr=1;
foreach($products_options_array as $products_options_array2) {
$ctr++;
//if it is a color image, or a color hex
if($products_options_array2['color_image']!='')
{
$clr_sec = "style=\"background-image:url(images/pattern/".$products_options_array2['color_image'].") !important; height:28px; width:28px;\"" . "class=\"testy\"";
}
else {
$clr_sec = "style=\"background-color:".$products_options_array2['color_code']." !important; height:28px; width:28px;float:left;\"" . "class=\"testy\"";
}
?>
<div <?php echo $clr_sec;?>> </div>
<?php
$clr++;
} ?>
</div>
I have tried adding it into the prod_list_contents variable but I don't think I can because of the php here is not METHODS. and requires semi colons etc. Is there another way to go about this? Or am I able to add it in and I'm just being stupid?
If i understand your question correctly you wan to add $clr_sec to $prod_list_contents.
Just change
<div <?php echo $clr_sec;?>> </div>
to
$prod_list_contents.='<div '.$clr_sec.'</div>';

Categories