I have an foreach to add terms to a post on Wordpress Which is working great. Code below:
<h3>
<?php foreach($terms as $term) {?>
<?php echo $term->name;?>
<?php } ?>
</h3>
However I need to add a counter so that if there is more than one term in the <h3> it adds a / between them. For instance:
<h3>Term Name</h3>
<h3>Term Name / Term Name / Term Name</h3>
This is the code I have so far however its not working.
<?php
$i = 1;
foreach($terms as $term) {
if($i == 1){
echo ' / '.$term->name;
} else {
echo $term->name;
}
$i++;
} ?>
You don't need to use a counter. Just put each $term->name in to an array, and implode it:
echo implode(' / ', array_map(function($term) { return $term->name; }, $terms));
Here's a demo
Please try below code.
<?php $terms = array_values($terms);
if( sizeof($terms) > 1){ ?>
<h3><?php echo implode(' / ', array_map(function($term) { return $term->name; }, $terms)); ?></h3>
<?php }else{ $term = $terms[0]; ?>
<h3>echo $term->name;</h3>
<?php } ?>
<?php
$i = 1;
foreach($terms as $term) {
if($i > 1) {
echo ' / '.$term->name;
} else {
echo $term->name;
$i++;
}
}
?>
Here is the working solution :)
<?php
$terms = array("Term One","Term Two","Term Three","Term Four");
$i = 1;
$result="";
foreach($terms as $term)
{
if($i==1)
{
$result=$result.$term.' ';
$i++;
}
else
{
$result=$result."/".' ';
$result=$result.$term.' ';
}
}
echo $result;
?>
Related
I have created a function that allows me to display the next/previous category, by order of their position. This is working fine, the problem I am having is getting ACF fields to display specific to each category.
I currently have a colour picker ACF field for every category - which is different for each one, this is set to display on each link. At the moment, the ACF field is only bringing in the hex value from the current category that is being viewed, as opposed to the next/previous ones.
function next_prev_cat() {
$this_taxonomy = get_queried_object();
if (is_category()) {
$taxonomies = get_categories();
}
if (is_tag()) {
$taxonomies = get_tags();
}
foreach ($taxonomies as $position => $tax):
if ($this_taxonomy->term_id == $tax->term_id):
$next_tax = $position + 1;
$prev_tax = $position - 1;
break;
endif;
endforeach;
$showPrev;
$showNext;
if ($prev_tax < 0) {
$prev_tax=count($taxonomies) - 1;
$showPrev='';
} else {
$prev_tax;
$showPrev = $taxonomies[$prev_tax]->name;
}
if ($prev_tax == count($taxonomies)-2) {
$next_tax = 0;
$showNext = '';
} else {
$showNext = $taxonomies[$next_tax]->name;
}
$prevLink = get_term_link( $taxonomies[$prev_tax] );
$nextLink = get_term_link( $taxonomies[$next_tax] );
?>
<div class="area-links">
<a style="border:1px solid <?php the_field('brand_colour', $tax); ?>" href="<?php echo $prevLink;?>"><?php echo $showPrev; ?></a>
<a style="border:1px solid <?php the_field('brand_colour', $tax); ?>"href="<?php echo $nextLink;?>"><?php echo $showNext ?></a>
</div>
<?php
}
Here is updated version of code
function next_prev_cat() {
$this_taxonomy = get_queried_object();
$id_for_color=false;
if (is_category()) {
$taxonomies = get_categories();
}
if (is_tag()) {
$taxonomies = get_tags();
}
$colors=array();
foreach ($taxonomies as $position => $tax):
$id_for_color=$tax->term_id;
$colors[$id_for_color]=get_field('brand_colour', $id_for_color);
$cats=array_push($cats,$id_for_color );
if ($this_taxonomy->term_id == $tax->term_id):
$next_tax = $position + 1;
$prev_tax = $position - 1;
break;
endif;
endforeach;
$showPrev;
$showNext;
if ($prev_tax < 0) {
$prev_tax=count($taxonomies) - 1;
$showPrev='';
} else {
$prev_tax;
$prev_color=$colors[$taxonomies[$prev_tax]->term_id];
$showPrev = $taxonomies[$prev_tax]->name;
}
if ($prev_tax == count($taxonomies)-2) {
$next_tax = 0;
$showNext = '';
} else {
$next_color=$colors[$taxonomies[$next_tax]->term_id];
$showNext = $taxonomies[$next_tax]->name;
}
$prevLink = get_term_link( $taxonomies[$prev_tax] );
$nextLink = get_term_link( $taxonomies[$next_tax] );
?>
<div class="area-links">
<a style="border:1px solid <?php echo $prev_color; ?>" href="<?php echo $prevLink;?>"><?php echo $showPrev; ?></a>
<a style="border:1px solid <?php echo $next_color; ?>"href="<?php echo $nextLink;?>"><?php echo $showNext ?></a>
</div>
<?php}
I am display data from the database. Currently, I have 6 records in my database and I am getting my output like
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>
Now what I am doing is, I have to close the </ul> tag after 4th li tag and then start new ul after 4th li.
My expected output is
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
</ul>
<ul>
<li>Records5</li>
<li>Records6</li>
</ul>
is it possible?
I am using below code
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check=0;
$first4=0;
while ( $tyler_query->have_posts() ) {
$tyler_query->the_post();
if ($index < 4) {
if ($first4==0){?>
<ul>
<?php $first4=1;}?>
<li>
<!--output here-->
</li>
<?php if ($first4==4){?>
</ul>
<?php }?>
<?php }
else {
if ($check==0){?>
<ul>
<?php $check=1;}?>
<li>
<!--output here-->
</li>
<?php } $index++;}?>
</ul>
<?php }?>
You can just insert a closing tag followed by an opening tag, whenever it meets your condition. In the following after every third item:
<?php
$items = [
'Syd',
'Roger',
'Nick',
'David',
'Richard'
];
$i = 0;
echo '<ul>';
foreach($items as $item) {
if($i++%3 == 0)
echo '</ul><ul>';
echo '<li>' . $item . '</li>';
}
echo '</ul>';
Output:
<ul><li>Syd</li><li>Roger</li><li>Nick</li></ul><ul><li>David</li><li>Richard</li></ul>
It's quick example. Hope help you.
if ($tyler_query->have_posts()) {
$index = 0;
$check = 6;
?>
<ul>
<?php while ($tyler_query->have_posts()) {
$tyler_query->the_post(); ?>
<li><?php echo 'some_value' ?></li>
<?php if ($index % $check === 0 ) { ?>
</ul><ul>
<?php }
$index++;
} ?>
</ul>
<?php } ?>
your code would work if you echo the HTML tag/output you want to see in the browser.
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check = 0;
$first4 = 0;
while ($tyler_query->have_posts()) {
$tyler_query->the_post();
$output = "whatever the output object is";
if ($index < 4) {
if ($first4 == 0) {
echo '<ul>';
$first4 = 1; // increment so that the this if block wont trigger again
}
echo '<li>' . $output . '</li>';
// increment so that the next if block trigger once
if ($first4 == 4) {
echo '</ul>';
}
$first4++;
}
if ($index >= 4){
if ($check == 0) {
echo '<ul>';
$check = 1;
}
// assuming you want to have the rest of the data in this block.
// data 5 and above
else {
echo '<li>' . $output . '</li>';
}
}
$index++;
}
echo '</ul>';
}
?>
I have a bootstrap "tab" system going on where each tab is it's own category name:
<?php $categories= get_categories();
$firstCat = 1;
foreach ($categories as $cat) {
$trimmedCatName = str_replace(' ', '', $cat->cat_name);
echo '<li';
if ($firstCat == 1) {
echo ' class="active"';
}
echo '>'.''.$cat->cat_name.' <small style="color:#447294">('.$cat->category_count.')</small></li>';
$firstCat++;
}
?>
This above ^ code works fine and sets the tabs up nicely.
The problem I have is with cycling through the categories as "tab-content" and then for each separate category, showing all the post titles/images for that category. Here's what I have so far:
<div class="tab-content">
<?php $categories= get_categories();
$firstCat = 1;
foreach ($categories as $cat) {
$trimmedCatName = str_replace(' ', '', $cat->cat_name);
echo '<div class="tab-pane ';
if ($firstCat == 1) {
echo 'active';
}
echo '" id="#'.$trimmedCatName.'">'.
'<select class="image-picker">';
$posts = get_posts($cat);
if ($posts) {
foreach ($posts as $p) {
echo '<option>';
echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
echo '</option>';
}
}
echo '</select>';
$firstCat++;
}
?>
</div>
I'm confused on how to get this code correctly.
<div class="tab-content">
<?php $categories= get_categories();
$firstCat = 1;
foreach ($categories as $cat) {
$trimmedCatName = str_replace(' ', '', $cat->cat_name);
echo '<div class="tab-pane ';
if ($firstCat == 1) {
echo 'active';
}
echo '" id="#'.$trimmedCatName.'">'.
'<select class="image-picker">';
$posts = get_posts(array('category' => $cat->term_id));
if ($posts) {
foreach ($posts as $p) {
echo '<option>';
echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
echo '</option>';
}
}
echo '</select>';
$firstCat++;
}
?>
</div>
notice i replaced $cat->term_id
if you are still getting nothing try hardcoding the category ID and see if you get any results.
try this..
add_action('init','test');
function test(){
var_dump(get_posts(array('category' => 1)));
}
I have a following code that shows category by level, but I have to show all the sub categories from that specific category.
<?php
$_cat = new Mage_Catalog_Block_Navigation();
$currentCat = $_cat->getCurrentCategory();
$subCats = Mage::getModel('catalog/category')->load($currentCat->getId())- >getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php ////////////////////////level-3///////////////////////////// ?>
<?php $category = Mage::registry('current_category');
$category->getParentCategories();
if ( $category->getLevel() == 3 ) : ?>
<div class="cat_drop_ser_wrap">
<?php $currentCat = Mage::getModel('catalog/category')->load($currentCat- >getId()) ?>
<select class="select_class" onchange="window.location.href=this.value">
<option value="#">-Select</option>
<?php foreach($subCatIds as $subCatId): ?>
<?php $subCat = Mage::getModel('catalog/category')->load($subCatId); ?>
<?php if($subCat->getIsActive()): ?>
<option value="<?php echo $subCat->getUrl() ?>">
<?php echo $subCat->getName(); ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?><!--if--level-3-->
Thanks in advance.
Ok in case anyone needs it following will show the sub categories and so on:
<?php //////sub////// ?>
<?php
$category_levels = Mage::getModel('catalog/category')->load($category->getId());
$subcategories = $category_levels->getChildrenCategories();
if (count($subcategories) > 0){
foreach($subcategories as $subcategory){
$category_levels_two = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategoriess = $category_levels_two->getChildrenCategories();
if (count($subcategoriess) > 0){
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_dropdown_label');
$text = $category_levels_two->getCategoryDropdownLabel();
echo '<div class="empty_serch_select_1 cat_drop_ser_wrap 2">';
echo '<label>' . $text . '</label>';
echo '<select disabled="disabled">';
echo '<option value="#">-Select</option>';
echo '<select>';
echo '</div>';
break;
}
}
}
?>
<?php //////sub-sub////// ?>
<?php
$category_levels = Mage::getModel('catalog/category')->load($category->getId());
$subcategories = $category_levels->getChildrenCategories();
echo '<div class="empty_serch_select_2 cat_drop_ser_wrap 3">';
if (count($subcategories) > 0){
foreach($subcategories as $subcategory){
$category_levels_two = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategoriess = $category_levels_two->getChildrenCategories();
if (count($subcategoriess) > 0){
foreach($subcategoriess as $subcategorys){
$category_levels_three = Mage::getModel('catalog/category')->load($subcategorys->getId());
$subcategoriesss = $category_levels_three->getChildrenCategories();
if (count($subcategoriesss) > 0){
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_dropdown_label');
$text = $category_levels_three->getCategoryDropdownLabel();
echo '<label>' . $text . '</label>';
break;
}
}
}
}
}
echo '<select disabled="disabled">';
echo '<option value="#">-Select</option>';
echo '<select>';
echo '</div>';
?>
<?php //////sub-sub-sub////// ?>
<?php
$category_levels = Mage::getModel('catalog/category')->load($category->getId());
$subcategories = $category_levels->getChildrenCategories();
echo '<div class="empty_serch_select_2 cat_drop_ser_wrap 3">';
if (count($subcategories) > 0){
foreach($subcategories as $subcategory){
$category_levels_two = Mage::getModel('catalog/category')->load($subcategory->getId());
$subcategoriess = $category_levels_two->getChildrenCategories();
if (count($subcategoriess) > 0){
foreach($subcategoriess as $subcategorys){
$category_levels_three = Mage::getModel('catalog/category')->load($subcategorys->getId());
$subcategoriesss = $category_levels_three->getChildrenCategories();
if (count($subcategoriesss) > 0){
foreach($subcategoriesss as $subcategorys){
$category_levels_three = Mage::getModel('catalog/category')->load($subcategorys->getId());
$subcategoriessss = $category_levels_three->getChildrenCategories();
if (count($subcategoriessss) > 0){
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_dropdown_label');
$text = $category_levels_three->getCategoryDropdownLabel();
echo '<label>' . $text . '</label>';
break;
}
}
}
}
}
}
}
echo '<select disabled="disabled">';
echo '<option value="#">-Select</option>';
echo '<select>';
echo '</div>';
?>
I want the page http://zanifesto.com/product/boise-infographic/ go back to http://zanifesto.com/gallery, not the Infographics category page at the "Back to" link at the top.
When I replace the .home_url etc etc you see below with the gallery url mentioned above, it errors out.
I am looking to understand why the php code doesn't act like I am replacing one url for another.
<div class="product_navigation desktops">
<?php
$term_list = '';
$j=0;
foreach ($terms as $term) {
if($term->parent==0){
$j++;
if( $j <= 1 ){
$term_list .= '' . $term->name . '';
}
}
}
if(strlen($term_list) > 0){ echo '<div class="nav-back">‹ '. __('Back to ', 'theretailer').$term_list.'</div>'; };
?>
<?php if (function_exists('be_previous_post_link')) { ?>
<div class="nav-next-single"><?php be_next_post_link( '%link', '', true,'', 'product_cat' ); ?></div>
<div class="nav-previous-single"><?php be_previous_post_link( '%link', '', true,'', 'product_cat' ); ?></div>
<div class="nav-prev-next-txt"><?php _e('Prev / Next', 'theretailer'); ?></div>
<?php } ?>
<div class="clr"></div>
</div>
<?php } ?>
this code hack could do what you want...
<?php
$term_list = '';
$j=0;
foreach ($terms as $term) {
if($term->parent==0){
$j++;
if( $j <= 1 ){
$url=home_url() . '/' . $category_slug . '/'. $term->slug;
if($url=='http://zanifesto.com/product-category/infographics'){
$url='http://zanifesto.com/gallery';
}
$term_list .= '' . $term->name . '';
}
}
}
if(strlen($term_list) > 0){ echo '<div class="nav-back">‹ '. __('Back to ', 'theretailer').$term_list.'</div>'; };
?>