Easy one, but I can't find an answer.
I wanna to list sub-categories to create tabs in code below. This one works fine in my output. But the first tab needs a <li class= "selected" > to show the content on page load .
How to force the first echo result to include a class on <li> element ?
.
.
.
[ Output ]
Actual: .......... Category 1 | Category 2 | Category 3
Needed: .......... Category 1* | Category 2 | Category 3
*class="selected"
<ul class="cd-tabs-navigation">
<?php
$argumentos = array(
'hide_empty' => 1,
'child_of' => 44,
);
$categories = get_categories($argumentos);
foreach($categories as $category) {
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}
?>
</ul>
.
.
[ EDIT ]
The solution by Lal. Thank you everyone!
foreach($categories as $category) {
if(++$i==1)
echo '<li class="selected"><a class="selected" data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
else
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}
I would suggest you to use a counter.. Not sure if this is the right way to do it..
Change your foreach as follows
$i=0;
foreach($categories as $category) {
if($i==0)
echo '<li class="selected"><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
else
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
$i++;
}
OR
As suggested in the answer here, you could use array_shift() to process the first item in the array separatley.
That is, do it as below
$first = array_shift($categories);
echo '<li class="selected"><a data-content="' . $first ->slug . '" href="#' . $first ->slug . '">' . $first ->name . '</a></li>';
foreach($categories as $category) {
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}
Read more about array_shift() in the docs
Lal's solution will work fine (as long as you increment $i), but here's a simpler way to do it without an extra variable for the counter :
foreach($categories as $index => $category){
if(!$index) echo "This is the first element";
else echo "This is not the first element";
}
(not sure if this should be an edit or an answer since it's basically the same method but with a slightly simpler synthax, I'll post as an answer for now)
You can use this syntax for foreach. Using this syntax you have in $k the index of the iteration, while in $v the value of the array you're iterating. So if $k == 0 you're in the first iteration.
$a = array("first", "second", "third", "fourth");
foreach ($a as $k=>$v) {
echo $k . "--->" . $v ;
if($k == 0){
echo "First iteration!";
//Do your stuff
}
echo "<br>";
}
Related
I am displaying a list of filters using Isotope. The functionality works when the first part of the code works however, when the screen is resized the posts don't filter when it executes the last part... Any help is much appreciated!
<?php
$filterLinks = array();
$newarray = array();
$starter = array('name'=>'View All','slug'=>'*');
$filterLinks[] = $starter;
$taxterms = get_terms( $customTax );
if ( ! empty( $taxterms ) && ! is_wp_error( $taxterms ) ){
foreach ( $taxterms as $taxterm ) {
$datafilter = '.' . $taxterm->slug;
$newarray = array(
'name' => $taxterm->name,
'slug' => $datafilter,
);
$filterLinks[] = $newarray;
}
}
echo '<ul id="filters" class="desk-filters button-group">' ."\n";
foreach ($filterLinks as $links) {
echo '<li><button class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</button></li>'."\n";
}
echo '</ul>';
// Drop down menu on mobile
echo '<div id="filters" class="resp-filters button-group">'."\n";
echo '<div class="resp-filter-btn">Select Filter</div>'."\n";
echo '<div class="resp-filter-content">'."\n";
foreach ($filterLinks as $links) {
echo '<a class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</a>'."\n";
}
echo '</div>'."\n";
echo '</div>';
?>
Functionality Works:
echo '<ul id="filters" class="desk-filters button-group">' ."\n";
foreach ($filterLinks as $links) {
echo '<li><button class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</button></li>'."\n";
}
echo '</ul>';
Functionality Doesn't Work:
// Drop down menu on mobile
echo '<div id="filters" class="resp-filters button-group">'."\n";
echo '<div class="resp-filter-btn">Select Filter</div>'."\n";
echo '<div class="resp-filter-content">'."\n";
foreach ($filterLinks as $links) {
echo '<a class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</a>'."\n";
}
echo '</div>'."\n";
echo '</div>';
Is this because i'm executing it twice, just hiding one on desktop and showing the other on mobile?
Looking for some direction. Thanks in advance!
You're using id="filters" twice, which is improper use of the id attribute, since it should be unique. Not sure if that is of any concern?
I've been racking my brain on this one.
Background:
The following code works, and is grabbing the correct taxonomy term information for the relevant WordPress post.
Problem:
ACF is outputting terms in order of newest to oldest.
WordPress's $game->name; is outputting terms in order of oldest to newest.
This basically means that the tooltip is not matching the image from get_field('icon');
$games = get_the_terms(get_the_ID(), 'games')
foreach ($games as $game) {
$term = array_pop($games);
if ( get_field('icon', $term) ) {
echo '<img src="' . get_field('icon', $term ) . '" alt="' . $game->name . '" data-placement="bottom" data-toggle="tooltip" title="' . $game->name . '" />';
}
}
I have so far tried:
Updating $games to $games = get_the_terms(get_the_ID(), 'games', array( 'order' => 'DESC') ) (no impact).
Reversing the order of the array for $games using array_reverse.
Suggestions would be enormously appreciated.
You appear to be deliberately doing this in your code by using array_pop??
Simply use the $game variable from your foreach loop:
$games = get_the_terms(get_the_ID(), 'games')
foreach ($games as $game) {
//$term = array_pop($games);
if ( get_field('icon', $game) ) {
echo '<img src="' . get_field('icon', $game ) . '" alt="' . $game->name . '" data-placement="bottom" data-toggle="tooltip" title="' . $game->name . '" />';
}
}
I am new to PHP, currently getting error saying
Fatal error: Call to a member function url() on a non-object on line 8
Below is the code I am trying
<?php
$subpages = $site->pages()->children()->visible();
$image_url = $subpages->image()->url();
$title = $subpage->title()->text();
foreach($subpages as $subpage) {
echo '<div class="col-md-4">';
echo '<h2>' . $title . '</h2>';
echo '<a href="' . $subpage->url() . '" title="' . $title . '">';
echo '<img src="' . $image_url . '" alt="' . $title . '" class="img-responsive img-thumbnail">';
echo '</a>';
echo '</div>';
}
?>
Here's your code corrected:
<?php
$subpages = $site->children()->visible();
foreach($subpages as $subpage) {
$image_url = $subpage->image()->url();
$title = $subpage->title()->html();
echo '<div class="col-md-4">';
echo '<h2>' . $title . '</h2>';
echo '<a href="' . $subpage->url() . '" title="' . $title . '">';
echo '<img src="' . $image_url . '" alt="' . $title . '" class="img-responsive img-thumbnail">';
echo '</a>';
echo '</div>';
}
?>
What's wrong with your code:
You should use $site->children() to list all children of the site
See kirby docs
You're defining $image_url and $title before the foreach which is not correct. I moved them just at the beginning of the foreach
loop. Also corrected image_url to use subpage instead of subpages.
You're using text() on your title. That doesn't exist, use either kirbytext() or html() depending on what you want to do. See the
docs.
You should check what are the values and methods in $subpages? i think there is problem when you assigning the values to the variable.
I have a theme options page and a loop which grabs my categories. This code works fine and I am able to save my options. The code like this:
function drop_elements_function(){
$my_cats = get_categories();
$i = 1;
foreach( $my_cats as $my_cat ) :
$my_categories[$my_cat->cat_ID] = array(
'value' => $my_cat->cat_ID,
'label' => $my_cat->cat_name
);
$i++;
endforeach;
$options = get_option('sandbox_theme_social_options');
echo'<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';
foreach ( $my_categories as $category ){
$label = $category['label'];
$selected = '';
if ( $options['Drop_Elements'] == $category['value'] )
$selected = 'selected="selected"';
echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
}
echo '</select>';
//print_r($options['Drop_Elements']);
}/*Function end*/
The output looks like this:
http://www.vasinternetposao.com/img.png
Problem: Now when my theme is installed for the first time i am getting the output like the screen shot above but obviously my option is not yet saved to the database (user must click the save button in order to select that category). So i was thinking to do something like this:
1.) User install the theme for the first time and then output looks like this:
http://www.vasinternetposao.com/img2.png
2.) If user unintentionally select "Choose your category" and clicks the "save button"
the output will be again:
http://www.vasinternetposao.com/img2.png
3.) If the user select the real category (not "Choose Your Category") then "Choose Your Category" Disappears:
http://www.vasinternetposao.com/img.png
This is my attempt but it is not working:
function drop_elements_function(){
$my_cats = get_categories();
$i = 1;
foreach( $my_cats as $my_cat ) :
$my_categories[$my_cat->cat_ID] = array(
'value' => $my_cat->cat_ID,
'label' => $my_cat->cat_name
);
$i++;
endforeach;
$options = get_option('sandbox_theme_social_options');
echo'<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';
foreach ( $my_categories as $category ){
$label = $category['label'];
$selected = '';
if ( $options['Drop_Elements'] == $category['value'] ){
$selected = 'selected="selected"';
echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
}
elseif(!isset($options['Drop_Elements'])){
$selected = 'selected="selected"';
echo '<option selected="selected" value="Choose Your Category">Choose Your Category</option>';
echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . '>' . $label . '</option>';
}
}/*Foreach close*/
echo '</select>';
//print_r($options['Drop_Elements']);
}/*Function end*/
Can it be done with PHP? Any help is appreciated! Thank you!
I think what you want to do is something like the following example:
echo '<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';
echo '<option selected="selected" value="Choose Your Category">Choose Your Category</option>';
foreach ( $my_categories as $category ){
$label = $category['label'];
$selected = ( $options['Drop_Elements'] == $category['value'] ) ? " selected='selected' " : "";
echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
}
echo "</select>";
I'm trying to highlight a menu item depending if it's on the current page. The code below is currently highlighting every menu item because it's within the foreach loop. How can I highlight the taxonomy term if it's on a certain page id and ONLY that term (not every one)?
<?php $args = array( 'taxonomy' => 'blog' );
$terms = get_terms('blog', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
echo $postid;
foreach ($terms as $term) {
$i++;
$absolute = get_bloginfo('url');
$postid = get_the_ID();
if($postid == "561") {
$term_list .= '<a class="menu-active" style="padding-left:30px;width:88%!IMPORTANT;" href="' . $absolute . '/blog/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
} else {
$term_list .= '<a style="padding-left:30px;width:88%!IMPORTANT;" href="' . $absolute . '/blog/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
} } ?>
<?php echo $term_list; } ?>
You will have to query the taxonomy terms belonging to the current page and compare their id-s in the foreach loop.
The get_the_ID() function returns the id of the current post, not the current taxonomy.
Here is a link to a wordpress related question which shows how you can query current taxonomy terms:
https://wordpress.stackexchange.com/questions/20431/how-to-get-taxonomy-term-of-the-current-page-and-populate-queries-in-the-templat