I'm using a PHP if else statement to ignore a custom field 'thingstodo' if it's empty. I have got the following code to work but it's putting unwanted line-breaks after every paragraph tag in my content. Where am I going wrong?
<?php if (get_field('gettingthere')): ?>
<div class="inprofile3col">
<h2>Getting there</h2>
<span class="content">
<?php echo get_post_meta($post->ID, 'gettingthere', true); ?>
</span>
<?php endif; ?>
<!-- ends Getting there -->
<!-- Things to do begins -->
<?php if (get_field('thingstodo')): ?>
<h2>Things to do</h2>
<?php endif; ?>
<span class="content">
<?php
$value = get_field('thingstodo');
if ($value) {
echo $value;
} else {
echo '';
}
?>
</span>
</div>
Here
<span class="content">
<?php
$value = get_field('thingstodo');
if ($value ) {echo $value ;} else {echo '';}
?>
</span>
you still have an output if the field is empty: the empty span-tag:
<span class="content">
echo '';
</span>
You should remove line breaks from thingstodo and avoid empty html tags. Here is the updated mark up:
<?php if( get_field('gettingthere') ): ?>
<div class="inprofile3col">
<h2>Getting there</h2>
<span class="content">
<?php echo get_post_meta( $post->ID, 'gettingthere', true ) ; ?>
</span>
<?php endif; ?>
<!-- ends Getting there -->
<!-- Things to do begins -->
<?php if( get_field('thingstodo') && !empty(trim(get_field('thingstodo')))) { ?>
<h2>Things to do</h2>
<span class="content">
<?php
$value = preg_replace('/\s\s+/',' ',trim(get_field('thingstodo')));
if ($value) {echo $value ;} else {echo '';}
?>
</span>
<?php } ?>
</div>
Related
I am attempting to display the correct HTML under a "p" tag with PHP based on a true/false value which is pulled from WordPress ACF plugin, for every individual post. It's my first time using PHP and I'm not sure how to go about this.
My code is:
<p class="applied-grid-filter">
<?php if (the_field('relocation', $job->ID) === 1) ?>
</p>
Currently, if the post has "relocation" marked as false - an empty applied-grid-filter "p" tag appears. If the post has "relocation" marked as true - an applied-grid-filter "p" tag with the number "1" for true appears.
How can I make it so that when it is marked false, no P tag is made at all - and when the post is marked true, a P tag with textcontent of "Relocation" is made?
edit full context:
<section class="job-search-grid">
<?php if (count($jobs)) { ?>
<div class="job-search-grid-container">
<?php foreach ($jobs as $job) { ?>
<div class="job-search-grid-item">
<h2 class="job-search-grid-title"><?= esc_html($job->post_title) ?></h2>
<div class="salary-location-container">
<h2 class="job-search-grid-salary"><?php the_field('minimum_salary', $job->ID) ?>K
- <?php the_field('maximum_salary', $job->ID) ?>K</h2>
<div class="job-search-location-icon-container">
<img
class="job-search-location-icon"
src="<?= get_template_directory_uri() ?>/assets/location-icon.png"
/>
<p class="job-search-grid-location">Louisville, KY</p>
</div>
</div>
<p class="job-search-grid-description">
<?php the_field('short_description', $job->ID) ?>
</p>
<div class="applied-grid-filters">
<?php foreach (get_terms('industry', ['object_id' => $job->ID]) as $term) { ?>
<p class="applied-grid-filter"><?= esc_html($term->name) ?></p>
<?php } ?>
<?php foreach (get_terms('job_type', ['object_id' => $job->ID]) as $term) { ?>
<p class="applied-grid-filter"><?= esc_html($term->name) ?></p>
<?php } ?>
<?php
if (the_field('relocation', $job->ID) === 1) { ?>
<p class="applied-grid-filter">Relocation</p>
<?php } ?>
<?php
if (the_field('travel', $job->ID) === 0) { ?>
<p class="applied-grid-filter">No Travel</p>
<?php } ?>
</div>
<a class="job-view-more" href="<?php the_permalink($job->ID) ?>">View More</a>
</div>
<?php } ?>
</div>
<?php } else { ?>
<p class="no-results">No results found! Try another search.</p>
<?php } ?></section>
Just move the html code inside your php code. You also might wanna use get_field instead of the_field. Also consider using the equal == instead of the identical === comparison operator which will be tolerant if the return value is not a number but for example true/false.
Also have a look at https://www.php.net/manual/en/language.operators.comparison.php for this.
<?php
if (get_field('relocation', $job->ID) == 1) { ?>
<p class="applied-grid-filter"></p>
<?php }; ?>
I am trying to change a CSS class, based on the contents of a variable called $project_cats which contains a category name. While $project_cats appears to be valid throughout my script, it is not valid within one of my if statements, at least that is my assumption.
How can I access $project_cats from within my if statement?
<?php
if ( $query->have_posts() )
{
?>
<div id="myresource">
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<?php
$terms = get_the_terms($post->id,"project-type");
$project_cats = NULL;
if ( !empty($terms) ){
foreach ( $terms as $term ) {
$project_cats .= strtolower($term->name) . ' ';
}
}
//$project_cats does not appear to have content in the below if statement
if ($project_cats == "webinars") {
$iconclass = "iconsmind-Video-4";
}
?>
<div class="nectar-icon-list" data-icon-color="default" data-icon-style="border" data-icon-size="small" data-animate="">
<div class="nectar-icon-list-item">
<div class="list-icon-holder" data-icon_type="icon" style="background-color:#1963af;">
<i class="icon-default-style <?php echo $iconclass; ?>" data-color="default"></i>
</div>
<div class="content">
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php echo $project_cats;?>
</div>
</div>
</div>
<?php
}
?>
</div>
<?php
}
else
{
echo "No Results Found.";
}
?>
I just realized what's happening, in the if statement the name of the category is having a space ' ' appended to it, hence my if statement was never TRUE. Fixed.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
<?php if (get_the_author_meta('description')) { ?>
<?php
$author_ID = get_the_author_meta('ID');
$username = get_the_author_meta('display_name', $author_ID); ?>
<div class="mm-author-box">
<figure class="mm-author-box-avatar">
<?php echo get_avatar($author_ID, 70); ?>
</figure>
<?php if ($author_ID === 4) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
<?php else { ?>
<?php if ($author_ID === 9) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
<?php } ?>
It is a code to display author name and hyper link it.
From the first if statement if author has a description then go inside
Second if statement: if authors ID is 4 then execute the code below if not then
Else, there is one extra div inside the else statement which is for <div class="mm-author-box"> which is outside the if statement.
The problem is that when I put this code it breaks the page.. Only the header of the website loads and the content below it doesn't because i have placed this code in the php file which is a template for the page content.
I think there is some syntax problem coz I used the code without else statement and it worked.
You missed to close a curly bracket in the end. Add below line as a last line and try :
<?php } ?>
I don't know how strict that template engine is, but this could be the problem;
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
You're closing the div inside the if which was started outside the div. So , place that last </div> below the <?php } ?> and see if that helps
<?php if (get_the_author_meta('description')) {
$author_ID = get_the_author_meta('ID');
$username = get_the_author_meta('display_name', $author_ID); ?>
<div class="mm-author-box">
<figure class="mm-author-box-avatar">
<?php echo get_avatar($author_ID, 70); ?>
</figure>
<?php if ($author_ID === 4) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
<?php } else if ($mh_author_ID === 9) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
<?php } ?>
</div>
<?php } ?>
And as AnkiiG pointed out, you missed a closing tag, which I fixed without knowing while formatting my answer
Your if else structure is like this,
<?php if (get_the_author_meta('description')) { ?>
<?php if ($author_ID === 4) { ?>
<?php } ?>
<?php else { ?>
<?php if ($mh_author_ID === 9) { ?>
<?php } ?>
You're not closing the first if statement
I've got this PHP code:
<div class="connect_wrap <?php echo $this->class; ?> block" <?php echo $this->cssID; ?>>
<?php if(!$this->empty): ?>
<?php foreach($this->entries as $entry): ?>
<div class="entry block <?php echo $entry->class; ?>">
<div class="tab">
<ul class="tabs">
<li class="tab-link current" data-tab="Kunden">Kunden</li>
<li class="tab-link" data-tab="Loesungen">Lösungen</li>
</ul>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM kunden WHERE partner=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Kunden" class="tab-content current">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/kunden-detail/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM solutions WHERE solution_provider=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Loesungen" class="tab-content">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/synaptic-commerce-solution/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif;?>
In that code, I've got two DB queries. My problem is, if there is no data for both queries, the div with the class "connect_wrap" should not be displayed.
How can I do that?
Thanks in advance.
do you want to check if the query has data in it and its not empty ? if so try num_rows it will return the number of rows that the query found/affected for example to check if th query returned one or more rows
if($result->num_rows >= 1) {
//do stuf
} else {
// no result found
}
Move the query execution up or preferably: not within the html but in a different file/class. Then add a check before the content_wrap div: if($kundenResult->num_rows >= 1 || $solutionsResult->num_rows >= 1). If you just keep the individual checks like you already have, it will only show the content_wrap div if either or both of the queries return rows of data.
this code out put is here....(look at the footer subscribe section )
<?php if(!empty($this->items))foreach($this->items as $level): ?>
<?php
$formatedPrice = sprintf('%1.02f',$level->price);
$dotpos = strpos($formatedPrice, '.');
$price_integer = substr($formatedPrice,0,$dotpos);
$price_fractional = substr($formatedPrice,$dotpos+1);
?>
<div class="level akeebasubs-level-<?php echo $level->akeebasubs_level_id ?>">
<p class="level-title">
<span class="level-price">
<?php if(AkeebasubsHelperCparams::getParam('currencypos','before') == 'before'): ?>
<span class="level-price-currency"><?php echo AkeebasubsHelperCparams::getParam('currencysymbol','€')?></span>
<?php endif; ?>
<span class="level-price-integer"><?php echo $price_integer ?></span><?php if((int)$price_fractional > 0): ?><span class="level-price-separator">.</span><span class="level-price-decimal"><?php echo $price_fractional ?></span><?php endif; ?>
<?php if(AkeebasubsHelperCparams::getParam('currencypos','before') == 'after'): ?>
<span class="level-price-currency"><?php echo AkeebasubsHelperCparams::getParam('currencysymbol','€')?></span>
<?php endif; ?>
</span>
<span class="level-title-text">
<a href="<?php echo JRoute::_('index.php?option=com_akeebasubs&view=level&slug='.$level->slug.'&format=html&layout=default')?>">
<?php echo $this->escape($level->title)?>
</a>
</span>
</p>
<div class="level-inner">
<div class="level-description">
<div class="level-description-inner">
<?php if(!empty($level->image)):?>
<img class="level-image" src="<?php echo JURI::base()?><?php echo trim(AkeebasubsHelperCparams::getParam('imagedir','images/'),'/') ?>/<?php echo $level->image?>" />
<?php endif;?>
<?php echo JHTML::_('content.prepare', AkeebasubsHelperMessage::processLanguage($level->description));?>
</div>
</div>
<div class="level-clear"></div>
<div >
<button onclick="window.location='<?php echo JRoute::_('index.php?option=com_akeebasubs&view=level&slug='.$level->slug.'&format=html&layout=default')?>'">
<?php echo JText::_('COM_AKEEBASUBS_LEVELS_SUBSCRIBE')?>
</button>
</div>
</div>
</div>
<?php endforeach; ?>
this is the language file contain that name
COM_AKEEBASUBS_LEVELS_SUBSCRIBE="Subscribe Now"
I want to name those three sections butons as 6month,12month,24month
How will change the code ?
If you are unable to get the value using PHP, try changing the value of the button's name on page load
Here is the JQUERY
$(document).ready(function){
$('div.akeebasubs-awesome-description').each(function(
substription = $(this).children('H4').html()
button = $(this).parent().next().children('button')
$(button).attr(name,substription.split(' ')[0]+'month')
})
})
and the JAVASCRIPT
divs = document.getElementsByClassName('akeebasubs-awesome-description');
for (var i in divs){
divs[i].parentElement.nextElementSibling.children[0].name = divs[i].children[0].innerHTML.split(' ')[0] + 'month'
}
This will take the text of the H4 and make that the corresponding button's name