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 }; ?>
Related
I have a simple MySQL table that stores four fields - CATEGORY, TITLE, DESCRIPTION, IMAGE as well as a unique ID for each row.
I use ORDER BY CATEGORY to display all of them on the page through one query.
SELECT
RESOLUTIONS.CATEGORY,
RESOLUTIONS.ID,
RESOLUTIONS.TITLE,
RESOLUTIONS.DESCRIPTION,
RESOLUTIONS.IMAGE
FROM
RESOLUTIONS
ORDER BY
RESOLUTIONS.CATEGORY
I want to create a jump menu that will jump to the first row of each category on the page. Is this possible using php? I know how to create a jump menu that jumps to an ID anchor of a div, but how can I make a unique identifier (that I can jump to) inside the repeat region?
Here is the repeat code I have now...
<?php
while(!$DETAILS->atEnd()) {
?>
<div class="row g-my-10 g-color-black">
<?php if($DETAILS->getColumnVal("IMAGE")!= "") { ?>
<div class="col-md-9">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<? } else { ?>
<div class="col-md-12">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php } ?>
</div>
<?php
$DETAILS->moveNext();
}
$DETAILS->moveFirst(); //return RS to first record
?>
In each iteration check if the category is different than the one before and create an anchor.
<?php
// set any initial value that does not match an empty category (if you have them)
$lastCategory = false;
while(!$DETAILS->atEnd()) {
<div class="row g-my-10 g-color-black">
<?php
// this category is different than the last: create anchor
if ($lastCategory !== $DETAILS->getColumnVal("CATEGORY")) {
echo '<a name="category-' . $DETAILS->getColumnVal("CATEGORY") . '"></a>';
// set the compare-value to the current category
$lastCategory = $DETAILS->getColumnVal("CATEGORY");
}
?>
<?php if($DETAILS->getColumnVal("IMAGE")!= "") { ?>
<div class="col-md-9">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<? } else { ?>
<div class="col-md-12">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php } ?>
</div>
<?php
$DETAILS->moveNext();
}
$DETAILS->moveFirst(); //return RS to first record
?>
Note: You might have to put the anchor within the column or even the <h2>, depending on your grid system (is that bootstrap?). You maybe also want to refactor the generation of the title/description column, so you don't repeat yourself. Something like
<?php
if ($DETAILS->getColumnVal("IMAGE")!= "") {
$cols = '9';
} else {
$cols = '12';
}
?>
<div class="col-md-<?php echo $cols ?>">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php
if ($DETAILS->getColumnVal("IMAGE")!= "") {
?>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<?php
}
?>
Also note that according to PSR-1 you should use either <?php or the echo shortcut <?= but not just <?
And I know it's a matter of personal choice and also depends on what your file mainly contains (HTML or PHP), but I personally prefer echoing HTML code snippets instead of opening and closing PHP tags, because I find it easier to read (there is also a very marginal performance advantage)
How can I use multi "if" in one tag like <header>.
each div related to a tab. so I want to make a condition for each tab, for showing.
<?php
$mainbox = get_field('on');
$guide = get_field('install_guide');
$required = get_field('required');
?>
<section class="fieldset">
<header>
<?php
if (!empty($mainbox)){
<div class="download-taber active"> tab1</div> //// if true show this otherwise hide it
<div class="attr-taber">tab2</div>} //// if true show this otherwise hide it
elseif (!empty($guide)){
<div class="install-guide-taber">tab3</div>} //// if true show this otherwise hide it
elseif (!empty($required)){
<div class="system-required-taber">tab4</div>} //// if true show this otherwise hide it
?>
</header>
</section>
<?php endif; ?>
Simply (list statement there for ease of swapping values for testing):
<?php
list($mainbox, $guide, $required) = [true, true, true];
?>
<section class="fieldset">
<header>
<?php if($mainbox) { ?>
<div class="download-taber active">tab1</div>
<div class="attr-taber">tab2</div>
<?php } ?>
<?php if($guide) { ?>
<div class="install-guide-taber">tab3</div>
<?php } ?>
<?php if($required) { ?>
<div class="system-required-taber">tab4</div>
<?php } ?>
</header>
</section>
If all variables are set to true, all tabs will be shown.
To embade html in PHP you have to keep your html code in the echo tag of php
try this.
<?php
$mainbox = get_field('on');
$guide = get_field('install_guide');
$required = get_field('required');
?>
<section class="fieldset">
<header>
<?php
if (!empty($mainbox)){
echo "<div class='download-taber active'>tab1</div>"; //// if true show this otherwise hide it
echo "<div class='attr-taber'>tab2</div>";} //// if true show this otherwise hide it
elseif (!empty($guide)){
echo "<div class='install-guide-taber'>tab3</div>";} //// if true show this otherwise hide it
elseif (!empty($required)){
echo "<div class='system-required-taber'>tab4</div>";} //// if true show this otherwise hide it
?>
</header>
</section>
<?php endif; ?>
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>
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.
Basically, I need for the image at the bottom of the page with no text wrapped around it to be in the place of test post 4 (it's going to be a Google Adsense block but I am using images as a placeholder for now). I'm not sure how to do this, right now the code is the following:
<div class="google_ad_post">
<?php if ($count == 3) { ?>
<br /><img src="****" alt="post ad">
<?php } ?>
<?php $count++; ?>
</div>
Yet, the image is still at the bottom of the page. How can I fix this?
Here is the image:
I can't post pictures just yet so the URL to the image is http://i.imgur.com/7rw5B.jpg
I have to see your code to help you better with the Scripting Part, but something logical like this should work:
<?php
$count = 0;
foreach( $posts as $post ) : ?>
<?php if ($count == 3) { ?>
<div class="google_ad_post">
<img src="****" alt="post ad">
</div>
<?php } else { ?>
<div class="post" id="post-<?php the_ID(); ?>><div class="content"><?php the_content(); ?></div>
<?php } ?>
<?php $count++; ?>
<?php endforeach; ?>