ACF Repeater Display row fields if field_name = page_name - php

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

Related

Issue in pulling pages from the database

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>

Get category name from Magento

I'm using Luxury theme in Magento. I'm trying to display current category name in the catalog/category/view.phtml file.
What I have done so far:
<div class="custom">
<?php if($crumbs && is_array($crumbs)): ?>
<div class="container">
<div class="col-md-12">
<ul>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemscope itemtype="http://data-vocabulary.org/Breadcrumb" <?php endif ?>>
<?php if($_crumbInfo['link']): ?>
<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($_crumbInfo['title']) ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="url" <?php endif ?>><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></a>
<?php else: ?>
<strong><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></strong>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span>| </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif ?>
</div>
I have taken this code from page/html/breadcrumbs.phtml.
I am totally new to Magento/PHP. It doesn't show any error but it's not displaying the name of the category, while it's visible in breadcrumbs header. What I am doing wrong here?
If you want to show category name on category page. You can achieve this by 2 ways.
<?php echo $this->getCurrentCategory()->getName(); ?>
Or
<?php echo Mage::registry('current_category')->getName() ?>
On our site (magento 1.9) we wanted to show the first parent category of the current product on our product pages and provide a link to it. I achieved this as follows - you should be able to reverse engineer my code to your own ends.
At first I did it by adding the following code directly to the catalog/product/view.phtml but have since migrated it into a custom helper in my own module.
Here's the code, see if it works for you.
//get an array of the IDs of every category to which the product belongs.
$categoryIds = $_product->getCategoryIds();
//set CatID to the second element of the array since the first element
//is the base category of which all others are children.
$_catID = $categoryIds[1];
//load the correct model
$_category = Mage::getModel('catalog/category')->load($_catID);
//get the level of the current category
$level = $_category->getLevel();
//This if statement prevents the function from trying to run on products
//which are not assigned to any category.
if($_category->getName()){
// we want the second level category, since the first is the base category.
// ie if we call the default category 'base' and the product is in category
//base->foo->bar->baz we want to return the link to foo.
// while current category is deeper than 2 we ask it for it's parent
//category until we reach the one we want
while ($level > 2){
$parent = $_category->getParentId();
$_category =Mage::getModel('catalog/category')->load($parent);
$level = $_category->getLevel();
}
//Now we can build the string and echo it out to the page
$caturl = $_category->getUrl_key();
$_linkstring = 'http://www.yourwebsite.com/' . $caturl . '.html';
echo 'in category:';
echo '<a href="' . $_linkstring . '" title="'. $_category->getName() .'">';
echo ' '. $_category->getName();
echo '</a>';
}
Get the category name, image, description from category id in magento
$category = Mage::getModel('catalog/category')->load(category_id);
echo $category->getName();
echo $category->getImageUrl();
echo $category->getDescription();
Please put the category id in the load function

display text based on category in wordpress

how can i solve the following problem. Basically I want to show different sub headings based on the wordpress category being used.
this is the following code:
$categ = the_category(' ');
if($categ == 'News'){
$second_header = " secondry header displayed here";
}else{
$second_header = "Error";
}
?>
<h2> <?php the_category(' '); ?></h2>
<p> <?php echo $second_header; ?></p>
Right now this is not working, instead of checking against the text 'news' is there any way to check against the category id?
thanks in advance.
You can use the following to store the current category id:
<?php $catID = the_category_ID($echo=false);?>
The echo false stops any echo on the page and stores the variable. You can then do the following:
<?php
$categ = the_category_ID($echo=false);
if($categ == 1)
{
$second_header = " secondry header displayed here";
}
else
{
$second_header = "Error";
}
?>
<h2> <?php the_category(' '); ?></h2>
<p> <?php echo $second_header; ?></p>
Alternatively to this you could also use the following (as I believe the_category_ID is now deprecated http://codex.wordpress.org/Function_Reference/the_category_ID):
$categories = get_the_category();
$categ = $categories[0]->cat_ID;
if($categ == 1)
{
$second_header = " secondry header displayed here";
}
else
{
$second_header = "Error";
}
?>
<h2> <?php the_category(' '); ?></h2>
<p> <?php echo $second_header; ?></p>
Different Text on some Category Pages:
<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php endif; ?>
if you need category id you will need to first get category through <?php get_the_category_by_ID( $cat_ID ); ?>
Because the_category_ID is deprecated, then you can do the same with some modification.
More here - http://codex.wordpress.org/Category_Templates

Joomla 3 - category link breaking if category title contains an ampersand &

I have Joomla v3.x.x template override that creates an isotope layout with some category filters.
The problem I have is that if an ampersand (&) is put into the title of the category, that particular category filter will not show.
Is the a way to escape or allow the ampersand (&) to show and not break the filter for that particular category?
Code:
<?php
defined('_JEXEC') or die;
JHtml::_('bootstrap.tooltip');
if (count($this->children[$this->category->id]) > 0 && $this->maxLevel != 0) : ?>
<?php foreach ($this->children[$this->category->id] as $id => $child) : ?>
<?php if ($this->params->get('show_empty_categories') || $child->numitems || count($child->getChildren())) : ?>
<?php $data_name = $this->escape($child->title); ?>
<?php $data_option = str_replace(' ', '', $data_name); ?>
<li class="btn btn-primary"><?php echo $this->escape($child->title); ?></li>
<?php if (count($child->getChildren()) > 0) : ?>
<?php
$this->children[$child->id] = $child->getChildren();
$this->category = $child;
$this->maxLevel--;
if ($this->maxLevel != 0) :
echo $this->loadTemplate('children');
endif;
$this->category = $child->getParent();
$this->maxLevel++;
?>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif;
This is the particular line that create the filters from the category titles:
<li class="btn btn-primary"><?php echo $this->escape($child->title); ?></li>
The issue is most likely tied to the rendering of the filter select. Try changing the following line:
<?php $data_name = $this->escape($child->title); ?>
to:
<?php $data_name = htmlspecialchars($child->title); ?>
And see if that resolves the issue.

Conditional Statement if no value exists in PHP or Jquery

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>

Categories