I created an unordered list with the following html:
<ul id="infoBox">
<li class="facebook"><?php the_title(); ?> on Facebook</li>
<li class="twitter">Follow <?php the_title(); ?> on Twitter</li>
<li class="youtube">Watch <?php the_title(); ?> on Youtube</li>
</ul>
<?php echo get_field('value'); ?> is grabbing a string from the backend of my site. Sometimes, I do not have a string to display, so I want to create a conditional statement in jquery and/or php that basically says: If there is no field to get (if the field is left empty on the backend), do not display the list item at all. For instance, if a band doesn't have a youtube page, do not display the list item with a class of 'youtube' at all.
Any idea how I would go about this?
<ul id="infoBox">
<?php $response = get_field('facebook_page');
if(!empty($response)): ?><li class="facebook"><?php the_title(); ?> on Facebook</li><?php endif; ?>
<?php $response = get_field('twitter_page');
if(!empty($response)): ?><li class="twitter">Follow <?php the_title(); ?> on Twitter</li><?php endif; ?>
<?php $response = get_field('youtube_page');
if(!empty($response)): ?><li class="youtube">Watch <?php the_title(); ?> on Youtube</li><?php endif; ?>
</ul>
Would this work for you?
if(get_field('value') == '' || is_null(get_field('value'))
echo 'no value';
else
echo 'there is a value';
I hope understand your question and could help.
<ul id='infoBox'>
<?php
$name = the_title();
$potentialItems = array('facebook' => "$name on facebook",
'youtube' => "Watch $name on youtube",
'twitter' =? "Follow $name on twitter");
foreach($potentialItems as $k=>$v)
{
$gf = get_field($k.'_page');
if($gf)
{
echo "<li class='$k'><a href='$gf'>$v</a></li>";
}
}
?>
</ul>
Related
I am having problems pulling the pages through in PHP and HTML I have used :-
<li>
<!-- Pulling Categories from the database
dynamically -->
<?php
$nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject =
mysqli_fetch_assoc($nav_subjects)) {
?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
Which pulls the categories dynamically from the database and displays them with a drop down arrow just how I wanted but the pages will not show underneath them here's the code I have used for that bit:-
<!-- Categories listed correctly let's pull the pages for each one -->
<?php
if($nav_subject['id'] == $subject_id) {
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) {
?>
<ul>
<li>
<?php echo h($nav_page['menu_name']); ?>
</li>
</ul>
<?php } // while $nav_pages
} // if($nav_subject['id'] == $subject_id)
} // while $nav_subjects ?>
</li>
<?php
mysqli_free_result($nav_subjects);
mysqli_free_result($nav_pages);
?>
I am pulling in the SQL from another page which is loaded correctly as the categories load and display correctly.
I will be grateful for any ideas.
I have also tried to echo back the sql result but nothing is shown.
I have now got it working with the following code:-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject = mysqli_fetch_assoc($nav_subjects)) {?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<li><?php echo h($nav_page['menu_name']); ?></li>
<?php } ?>
<?php } ?>
</ul>
But now it is listing the secondary subjects as a child of the first instead of individual subjects of their own.
*********Resolved**********
please advise if you think the code could be better i've currently used :-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);?>
<?php while($nav_subject = mysqli_fetch_assoc($nav_subjects)){?>
<span class="opener"><?php echo h($nav_subject['menu_name']);?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<a href="<?php echo url_for('index.php?id=' . h(u($nav_page['id']))); ?>">
<?php echo h($nav_page['menu_name']); ?></a>
<?php } ?>
<?php mysqli_free_result($nav_pages); ?>
</ul>
<?php } ?>
<?php mysqli_free_result($nav_subjects); ?>
</li>
I've use Advanced Custom Fields to fill in contact details for 6 Schools - which all show on the Contact page.
Each school has a course page and I would like to display those contact details on the course page for that school.
I need school A's contact details to show on the "Courses at school A" page.
The course page name "School A" is identical to the get_sub_field('school_name').
<?php if(have_rows('schools', 45) ): ?> // 45 is post ID in WP
<?php while (have_rows('schools', 45) ): the_row();
// vars
$pagetitle = get_the_title(); // get page title in WP
$school = get_sub_field('school_name'); // get school name
if ($school == $pagetitle){ // IF they are the same THEN
?>
<article class="school-contacts">
<ul class="contact-info">
<li><strong><?php echo $school; ?></strong></li>
//other fields omitted for clarity
</ul>
</article>
<?php } ?>
<?php endwhile; endif; ?>
My problem is that ALL the schools show up when I do this.
I need to find a way so that only School A shows up on School A page, School B on School B page etc.
I've added in some checks (removed from this code for clarity) but they show that
School = School A
Pagetitle = School A
So the matching is working correctly.
I'm just stuck on how I can get the ACF output to ONLY show school A.
Solved it ... always the simplest things:
<?php if(have_rows('schools', 45) ):
while (have_rows('schools', 45) ): the_row();
// vars
$pagetitle = get_the_title();
$school = get_sub_field('school_name');
$logo = get_sub_field('logo');
$contact = get_sub_field('contact_name');
$title = get_sub_field('contact_title');
$email = get_sub_field('contact_email');
$website = get_sub_field('contact_website');
$phone = get_sub_field('contact_phone');
?>
<?php if ($school == $pagetitle) : the_row(); ?>
// needed : the_row(); to output the result
<article class="school-contacts">
<div class="school-logo"><img src="<?php echo $logo; ?>" width="100%" /></div>
<ul class="contact-info">
<li><strong><?php echo $school; ?></strong></li>
<li><?php echo $contact; ?></li>
<li><i><?php echo $title; ?></i></li>
<li><?php echo $phone; ?></li>
<li><?php echo $email; ?></li>
<li><?php echo $website; ?></li>
</ul>
</article>
<?php endif; endwhile; endif; ?>
maybe it is something simple but i am not really good with php
With this code i am able to load the static blocks styled in top of category in Magento and the subcategories in the bottom when the top category has child
What i am trying to do is that when there is no child category it should only load the block
otherwise because of styling it will hold the place that the subcat had position.
Please have a look on my code and maybe you understand:
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<div class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></div>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
$_catHtml = $this->getChildHtml('catalog.inline.subcat');
?>
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
// load the blocks styled with ul and il backgrounds
<ul class="inline-categories">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
<?php foreach ($_categories as $_category): ?>
<li<?php if($_i == 1):?> class="first"<?php endif ?>><a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>
<?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)</li>
<?php endforeach ?>
<?php endif; ?>
</ul>
// here i need something like an "else" because the if($count) allready understand that this is not on count so should only load this that is without style //
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
//end of this else thing, now should only load the cms/block alone without anything / end of page we are done...
You can use PHP's if/else statements like this:
<?php if ($_count): ?>
stuff if $_count is non-zero
<?php else: ?>
other stuff if $_count is zero
<?php endif; ?>
I have this template file:
<article class="box vest p10">
<h2 class="box_naslov"><?php echo $vst['naslov'] ?></h2>
<p><?php echo word_limiter($vst['opis'], 25) ?></p>
Izmeni Vest
<a role=obrisi href="<?php echo base_url('admin/vesti_delete/' . $vst['id_clanak']) ?>">ObriĊĦi Vest</a>
<?php
$slider = $vst['slider'] == 0 ? 'Ubaci u slider' : 'Izbaci iz slider-a';
$q = $vst['slider'] == 0 ? 'ubaci' : 'izbaci';
?>
<a role=slider href="<?php echo base_url('admin/vesti_slider/' . $vst['id_clanak'] . '?q=' . $q )?>"><?php echo $slider ?></a>
</article>
In my view file I have tried this:
<?php foreach ($vesti_slider as $vst): ?>
<?php $this->load->view('templates/admin/vesti', $vst) ?>
<?php endforeach ?>
I got error - ** Undefined variable: vst**. Is there some way to do this?
You need to pass data to a view as an array (or object), with the key becoming the variable name.
<?php foreach ($vesti_slider as $vst): ?>
<?php $this->load->view('templates/admin/vesti', array('vst' => $vst)) ?>
<?php endforeach ?>
I have some php generated magento top-links with a screenshot below:
My goal is to separate the "Log In" link and have it floated to the left of the same row.
I am hoping for an efficient way to select the last generated list item element and apply some CSS to it.
The code is below:
<ul class="links pull-right"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $_link): ?>
<?php if ($_link instanceof Mage_Core_Block_Abstract):?>
<?php echo $_link->toHtml() ?>
<?php else: ?>
<li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
<?php endif;?>
<?php if (! $_link->getIsLast()):?>|<?php endif;?>
<?php endforeach; ?>
</ul>
Any ideas would be greatly appreciated!
CSS offers a way to style the last-child of a collection, no tinkering with PHP required.
http://tinker.io/926d2
ul.links.pull-right :last-child {
margin-left: 2em;
}
I answered a similar question not long ago. This adds the class "last-item" to the last item processed.
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
Evening All,
Id try and limit the amount of business logic you are adding to your templates. As what you are looking to achieve is custom to this instance of magento I would create a very basic module. I would then look to either implement a new block or just a helper function that will return the data you want.
If your working on the block function ensure your class extends the Magento navigation class. ( Sorry I have not checked what this is ) Then create the action: E.g.
public function getNavigation()
{
$links = $this->getLinks();
$linkArray = array();
$linkCount = count($links);
$i;
foreach($links as $link) {
if($i == $linkCount) {
$last = true;
} else {
$last = false;
}
$linkArray[] = 'link' => $link->getLink()->toHtml(),
'isLast' => $last
$i++;
}
return $linkArray();
}
Your block would then have minimal logic applied. Mainly just iterating through the result set.
Hope that makes sense, If not let me know and I will get you what you require.