I have this code here the product tags can be animals, life or market. What I am trying to do is separate their list, but this code creates a new ul for every tag after animals and I do not know why, can anyone help?:
<?php
$versionvalues = get_the_terms( $product->id, 'product_tag');
if($versionvalues){
$previousTag = 'animals';
foreach ( $versionvalues as $versionvalue ) {
$currentTag = $versionvalue->name;
if($currentTag != $previousTag){
echo '</ul><ul class="products" style="text-align:center;">';
$previousTag = $currentTag;
}else{
$previousTag = $currentTag;
}
}
}
?>
Maybe wordpress doesn't allow me to put get_the_terms values into a variable. I did an echo of the $previousTag in the loop is keeps returning animals.
I have realized that is code is inside my woocommerce loop for my products, so $versionvales gets updated for each product and $previousTag will also reset to animals for each product.
You need to initialize $previousTag = 'animals' before entering the foreach loop, otherwise it will be reinitialized to equal 'animals' at the start of each iteration:
if($versionvalues){
$previousTag = 'animals';
foreach ( $versionvalues as $versionvalue ) {
.....
}
}
You reinitialized the $previousTag in each loop.
<?php
$versionvalues = get_the_terms( $product->id, 'product_tag');
if($versionvalues){
$previousTag = 'animals';
foreach ( $versionvalues as $versionvalue ) {
$currentTag = $versionvalue->name;
if($currentTag != $previousTag){
echo '</ul><ul class="products" style="text-align:center;">';
$previousTag = $currentTag;
} else {
$previousTag = $currentTag;
}
}
}
?>
just do not overwrite the variable each time
$versionvalues = get_the_terms( $product->id, 'product_tag');
if($versionvalues){
$previousTag = 'animals';
foreach ( $versionvalues as $versionvalue ) {
$currentTag = $versionvalue->name;
if($currentTag != $previousTag){
echo '</ul><ul class="products" style="text-align:center;">';
$previousTag = $currentTag;
}else{
$previousTag = $currentTag;
}
}
}
?>
Related
I'm trying to get some numeric values out, recovered thanks to: wp_get_post_terms, which retrieves me the daughter taxonomies of customized taxonomies, where prices are inserted in them in numerical form.
The goal is to display prices using sort and rsort, in the order decided.
When recovering the values, however, I am wrong to add elements to the array and sort them later
code:
foreach ($qprice as $r)
{
$children = $r->term_id;
$child_terms = get_term_children($r->term_id, 'servizi_pro');
$all_terms = wp_get_post_terms($r->ID, 'servizi_pro');
foreach ( $all_terms as $term )
{
if (!in_array($term->term_id, $child_terms))
continue;
$price = $term->name;
$final_price = [$price];
}
rsort($final_price, SORT_NUMERIC);
foreach($final_price as $pr_f) {
echo '<span class="numberCircle"><span>'.str_pad($pr_f, 9).''.'€'.'</span></span>';
}
}
plus:
$final_price = [];
foreach ($qprice as $r)
{
$children = $r->term_id;
$child_terms = get_term_children($r->term_id, 'servizi_pro');
$all_terms = wp_get_post_terms($r->ID, 'servizi_pro');
foreach ( $all_terms as $term )
{
if (!in_array($term->term_id, $child_terms))
continue;
$price = $term->name;
$final_price[] = $price;
}
}
sort($final_price, SORT_NUMERIC);
foreach($final_price as $pr_f) {
echo '<span class="numberCircle"><span>'.str_pad($pr_f, 9).'€'.'</span></span>';
}
You need to initialize the $final_price array before the inner foreach loop, and push onto the array rather than replacing it each time through the loop.
foreach ($qprice as $r)
{
$children = $r->term_id;
$child_terms = get_term_children($r->term_id, 'servizi_pro');
$all_terms = wp_get_post_terms($r->ID, 'servizi_pro');
$final_price = [];
foreach ( $all_terms as $term )
{
if (!in_array($term->term_id, $child_terms))
continue;
$price = $term->name;
$final_price[] = $price;
}
rsort($final_price, SORT_NUMERIC);
foreach($final_price as $pr_f) {
echo '<span class="numberCircle"><span>'.str_pad($pr_f, 9).''.'€'.'</span></span>';
}
}
I got a ACF Reaper field with a couple of rows I am trying to show. However, I only want the show row if it has a checkbox checked (Checkbox is a subfield within the repeater). I am trying to accomplish that by using if in_array as described in ACF documentation under "Conditional logic":
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) )
I am outputting the result in a WordPress shortcode. For now, my code kinda works, except it shows all results within the repeater field (also those that are unchecked). What am I missing ??
My code:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo get_field( 'upload_dokument' );
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
Managed to solve issue and with the help from #maggiathor's answer. For some reason echo was causing issue. I had to use return insted:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
$content = '<ul class="dokumenter">';
foreach( $rows as $row ) {
if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$pdf = $row['upload_dokument'];
$content = $content . '<li>' . $pdf . '</li>';
}
}
}
$content = $content . '</ul>';
return $content;
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
You cannot use get_sub_field() within the foreach loop, either you need to use a have_rows-while-loop or access it from the associative array:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo $row['upload_dokument'];
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
I read wordpress code documents about how to iterate menu items, and then I wrote this code in index.php:
<?php
$menu_name = 'top_menu';
$array_menu = wp_get_nav_menu_items($menu_name);
foreach ((array)$array_menu as $mol) ;
{
echo $mol;
}
?>
But it doesn't work. And is returning nothing. Casting to array didn't help so.
I need to echo items menu title one by one. without any html list tags.
$navItem is an object, so you can not just echo it, try to echo its properties like this:
foreach ($array_menu as $navItem ) {
echo '<li>'.$navItem->title.'</li>';
}
This might help:
<?php
function get_menu_items_by_registered_slug($menu_slug) {
$menu_items = array();
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_slug ] ) ) {
$menu = get_term( $locations[ $menu_slug ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
}
return $menu_items;
}
$show_menus = [];
$menus = get_menu_items_by_registered_slug('primary');
foreach( $menus as $menu ) {
$show_menus[] = $menu->title;
}
echo '<pre>';
print_r($show_menus);
?>
I've been stuck on this issue all day, and I just keep getting nowhere with it, hoping someone can help!
I'm building a site for a restaurant that has multiple locations and need to list out each drink that exists at each specific location. These drinks need to then be sorted by category (brown, white, tomato, beer, wine). I feel like I'm extremely close to a solution, but I've been banging my head for the last while.
Here's my code:
$drinks = get_posts('post_type=drinks&numberposts=-1');
$show_brown_title = false;
$show_white_title = false;
$show_tomato_title = false;
$show_wine_title = false;
$show_beer_title = false;
if ( $drinks ) {
foreach( $drinks as $drink ) {
$id = $drink->ID;
$drink_location = $drink->drink_location;
if($drink->drink_category == 'Brown' && $drink_location && in_array($site_slug, $drink_location)) {
if($show_brown_title == false) {
echo '<h4><span>Brown</span> Cocktails</h4>';
echo '<ul>';
$show_brown_title = true;
}
echo '<li>';
echo '<span class="drink_title">'.$drink->post_title.'</span>';
echo '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
echo '<span class="drink_price">'.$drink->price_oz.'</span>';
echo '</li>';
}
if($drink->drink_category == 'White' && $drink_location && in_array($site_slug, $drink_location)) {
if($show_white_title == false) {
echo '<h4><span>White</span> Cocktails</h4>';
echo '<ul>';
$show_white_title = true;
}
echo '<li>';
echo '<span class="drink_title">'.$drink->post_title.'</span>';
echo '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
echo '<span class="drink_price">'.$drink->price_oz.'</span>';
echo '</li>';
}
}
}
For the most part, this works. However, I'm running into 2 issues.
I can't figure out how to close the unordered list after I'm done with each category.
This groups by category for the most part, however, if I have a drink that comes later, it will not actually put it into the right category, it'll just go into whatever category is at the bottom. I'm not sure if this is because I'm not closing the unordered list, or if because of the order that the posts are being pulled from WP.
Let me know if I'm explaining this alright, and I really appreciate any help you guys can offer!
Z
Instead of get_posts, you should use WP_Query. Info: https://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts/50762#50762
$show_brown_title = false;
$show_white_title = false;
$show_tomato_title = false;
$show_wine_title = false;
$show_beer_title = false;
$args = array(
'post_type' => 'drinks',
'numberposts' => '-1'
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
while( $query->have_posts() ): $query->the_post();
echo '<pre>';
print_r( $query );
echo '<pre>';
endwhile;
endif;
wp_reset_postdata();
Inside the loop you will construct again your listing, use the print_r step by step, use your objects elements smart.
Aproach #2:
$show_brown_title = false;
$show_white_title = false;
$show_tomato_title = false;
$show_wine_title = false;
$show_beer_title = false;
foreach((get_the_category()) as $cat) {
$args = array(
'post_type' => 'drinks',
'numberposts' => '-1',
'cat' => $cat->cat_ID
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
echo '<ul>';
while( $query->have_posts() ): $query->the_post();
echo '<h4><span>'. $query->category_name .'</span> Cocktails</h4>';
echo '<li>';
echo '<span class="drink_title">'.$query->post_title.'</span>';
echo '<span class="drink_ingredients">'.$query->drink_ingredients.'</span>';
echo '<span class="drink_price">'.$query->price_oz.'</span>';
echo '</li>';
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
}
With the specification that $query->category_name maybe should be written other way. Use print_r to see the correct field name/element of your object.
Bellow is my solution.
Note: get_posts uses the orderby parameter to order the posts by drink_category.
The code is simplified to only start <ul> when drink_category changes and to end </ul> on category change too. Code also checks that html to output has been assigned a value and appends to it a final </ul> in case one is not appended already.
$drinks = get_posts('post_type=drinks&numberposts=-1&orderby=drink_category');
$current_category = '';
$html = '';
if ( $drinks ) {
foreach( $drinks as $drink ) {
$id = $drink->ID;
$drink_location = $drink->drink_location;
if ($current_category != $drink->drink_category) {
if ($current_category != '') {
$html .= '</ul>';
}
$current_category = $drink->drink_category;
$html .= '<h4><span>' . $current_category . '</span> Cocktails</h4>';
$html .= '<ul>';
}
if($drink_location && in_array($site_slug, $drink_location)) {
$html .= '<li>';
$html .= '<span class="drink_title">'.$drink->post_title.'</span>';
$html .= '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
$html .= '<span class="drink_price">'.$drink->price_oz.'</span>';
$html .= '</li>';
}
}
}
if (strlen($html) > 0 && !endsWith($html, '</ul>')) {
$html .= '</ul>';
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
How to get category deep with space padding on this function. At the moment i have select box with all categories have the same level.
<?php
add_action('add_meta_boxes', 'my_custom_metabox');
function my_custom_metabox() {
add_meta_box('custom-taxonomy-dropdown','Brands','taxonomy_dropdowns_box','post','side','high');
}
function taxonomy_dropdowns_box( $post ) {
global $brand_taxonomy, $taxonomy_name;
wp_nonce_field('custom-dropdown', 'dropdown-nonce');
$terms = get_terms( $brand_taxonomy, 'hide_empty=1&hierarchical=1;');
if ( is_a( $terms, 'WP_Error' ) ) {
$terms = array();
}
$object_terms = wp_get_object_terms( $post->ID, $brand_taxonomy, array('fields'=>'ids'));
if ( is_a( $object_terms, 'WP_Error' ) ) {
$object_terms = array();
}
// you can move the below java script to admin_head
?>
<?php
wp_dropdown_categories('show_option_none=Select category&show_count=1&hierarchical=1&taxonomy=ad_cat');
echo "Brand:";
echo "<select id='custombrandoptions' name='custombrands[]'>";
echo "<option value='0'>None</option>";
foreach ( $terms as $term ) {
if ( in_array($term->term_id, $object_terms) ) {
$parent_id = $term->term_id;
echo "<option value='{$term->term_id}' selected='selected'>{$term->name}</option>";
} else {
echo "<option value='{$term->term_id}'>{$term->name}</option>";
}
}
echo "</select><br />";
echo '<input type="text" value="'.$meta = get_post_meta($post->ID, 'cat_include', true).'" />';
}
source: http://paste.php.lv/dc485b1e6f37f09f916fccc6ae70ed2f?lang=php
I am not sure where your problem is located but maybe you are looking fpr str_repeat http://php.net/manual/en/function.str-repeat.php ..
so you might use these way:
echo '<option value="" ..>'.str_repeat(' ',$currentLevel+1).'</option>';
(I do not understand how you get the deep of level.. perhaps it is a good idea to iterate recusrivly through the hierarchy..)