Wordpress - Terms of custom taxonomy doesn't show in select dropdown - php

I explain my problem :
I needed to do a specific search bar for a client. Instead of giving the possibility to the user to type a search I need to put a dropdown with choices, make him select what he wants and then show every posts related to his choice.
I have one custom post type with two taxonomies for it.
My code for the select :
$pre_form = '<form role="search" method="get" class="search-form" id="searchform" action="' . esc_url(home_url('/')) . '">
<div>';
$taxonomies = 'aeronef';
$args = array('order' => 'ASC', 'hide_empty' => true);
function get_terms_dropdown_test($taxonomies, $args)
{
$myterms = get_terms($taxonomies, $args);
$output = "<select name='s'>";
foreach ($myterms as $term) {
$term_slug = $term->slug;
$term_name = $term->name;
$link = $term_slug;
$output .= "<option name='" . $link . "' value='" . $link . "'>" . $term_name . "</option>";
}
$output .= "</select>";
return $output;
}
echo $pre_form;
echo get_terms_dropdown_test($taxonomies, $args);
echo '<input type="submit" class="search-submit" value="' . esc_attr_x('Search', 'submit button') . '" />
</div>
</form>';
The problem is : It works perfectly fine when I use the taxonomy "gamme" (it shows me all the terms, i can select it, it shows me all the posts with this term and i can go on the detail of the post) but when i try with "aeronef" it shows me no terms. But I don't have an error. I checked everything, I have terms in it, WP recognize it as a taxonomy and can find terms if I put an ID to search, but with the "get_terms" function it finds the taxonomy but shows no terms.
Thx for helping !

Related

Wordpress - Woocommerce list product variations combinations in the same dropdown

I would like to show the product variations combinations in the same dropdown show as:
I override the variations.php into the woocommerce folder in my child theme. This function show the information well:
$available_variations = $product->get_available_variations();
if (count($available_variations) > 0) {
$output = '<div class="product-variations-dropdown">
<select id="available-variations" class="form-select product-selector mb-4" name="available_variations">';
$output .= '<option value="">' . __('Choose a variation') . '</option>';
foreach ($available_variations as $variation) {
$option_value = array();
foreach ($variation['attributes'] as $attribute => $term_slug) {
$taxonomy = str_replace('attribute_', '', $attribute);
$attribute_name = get_taxonomy($taxonomy)->labels->singular_name; // Attribute name
$term_name = get_term_by('slug', $term_slug, $taxonomy)->name; // Attribute value term name
$option_value[] = $attribute_name . ': ' . $term_name;
}
$option_value = implode(' | ', $option_value);
$output .= '<option value="' . $variation['variation_id'] . '">' . $option_value . '</option>';
}
$output .= '
</select>
</div>';
echo $output;
}
Next, the problem I have is that I don't know how when selecting the add to cart button in the drop-down menu, it activates the button, show price and shows the clean button. Once obtained, I will not know for sure if adding to the cart will add the product/variation correctly.
UPDATE:
I have seen that internally Wordpress works like this. Each selector contains one parameter. Is it possible that it is not in the correct php file? I mean this change may need to be done before you get to variations.php.

wordpress : how to add commas between meta terms on single custom post type

extendeing may last question about how to change the display of meta on single custom post type, with a great thanks to TimRDD for his helpful answer, now i have another question.
the working generating code
<?php
//get all taxonomies and terms for this post (uses post's post_type)
foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo '- '.$taxonomy;
foreach ($object_terms as $term) {
echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
}
}
}
}
?>
displays the terms in a single row but without commas between them such as :
(- Proceeding2015 - keywordsbusinessconsumerresearch).
I need your help please to put (:) after every set of terms and commas between terms to display them such as :
(- proceeding : 2015 - keywords : business, consumer, research).
You're code is ok, you just need to modify the output a bit. Try this code:
//get all taxonomies and terms for this post (uses post's post_type)
foreach ((array) get_object_taxonomies($post->post_type) as $taxonomy) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo ': (- ' . $taxonomy . ': ';// I modify the output a bit.
$res = '';
foreach ($object_terms as $term) {
$res .= '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf(__("View all posts in %s"), $term->name) . '" ' . '>' . $term->name . '</a>, ';
}
echo rtrim($res,' ,').')';// I remove the last trailing comma and space and add a ')'
}
}
Hope it works.
I've not tested this code, but I have linted it. Based on your description, this should do it.
<?php
//get all taxonomies and terms for this post (uses post's post_type)
I moved this out of the fornext.
$taxonomies = get_object_taxonomies($post->post_type);
foreach ( $taxonomies as $taxonomy ) {
I moved this into an if statement. If the assignment fails (nothing is returned) it should fail the if and skip all of this.
if ($object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'))) {
$holding = array();
foreach ($object_terms as $term) {
Instead of outputting it immediately, I am building an array.
$holding[] = '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
} // foreach ($object_terms as $term)
Here is where we do that output. I'm using the explode() function. This will output each element of the array and put a ', ' after all of them except for the last one.
echo '- '.$taxonomy . ': ' .explode(', ',$holding) . ' ';
} // if ($object_terms)
} // foreach ( $taxonomies as $taxonomy )
?>
I do hope I got it right.
Cheers!
=C=

Saving Post Meta in Wordpress Admin Menu w/ Foreach Loop

Here's the long and short:
I'm looking to add a global controller for post meta tags in Wordpress. I already have an established Wordpress custom admin page within our theme. I also already have added meta boxes to individual pages which store key value pairs in a more polished fashion. I have been able to call all key value pairs in a foreach loop so that they display in the administrative page. I'm having difficulty getting the input from these values on the admin panel page to properly update when the submit_button(); is clicked. I've gotten the update_post_meta function to outright erase the previous values but I have not been able to get the update_post_meta function to properly pass the $_POST value into the value at the database.
I know that the following code is not working as it's halfway through two attempts. Any help is appreciated:
$pages = get_pages();
foreach ( $pages as $page ) {
$post_id = $page->ID;
$meta = get_post_meta($post_id);
/* $value1 = get_post_meta( $post_id, 'value1', true );
$value2 = get_post_meta( $post_id, 'value2', true ); */
echo '<h3>' . $page->post_title . '</h3>';
echo '<label for="options_value1_' . $post_id . '">Value 1:</label>';
echo '<input type="text" id="options_value1_' . $post_id . '" name="options_value1" value="' . $meta['value1'][0] . '" size="25" /><br />';
echo '<label for="options_value2_' . $post_id . '">Value 2:</label>';
echo '<input type="text" id="options_value2_' . $post_id . '" name="options_value2" value="' . $meta['value2'][0] . '" size="25" /><hr />';
}
foreach ( $pages as $page ) {
update_post_meta($post_id, 'value1', $_POST('value1');
update_post_meta($post_id, 'value2', $_POST('value2');
}
Hope this helps:
$post_id is defined in the first loop, but not in the second loop.
Also you have a syntax error in the second loop. You're not closing the parenthesis of update_post_meta() and you're not using braces for $_POST. It should be $_POST[], not $_POST().
Also, the POST parameter you're looking for is options_value1 and options_value 2, because those are the names of the fields from the first loop.
Second loop should go like this:
foreach ( $pages as $page ) {
$post_id = $page->ID;
update_post_meta($post_id, 'value1', $_POST['options_value1']);
update_post_meta($post_id, 'value2', $_POST['options_value2']);
}
Hope it helps.

Highlight menu item depending on current page

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

get_category_link() is returning nothing

I am using the get_categories() function to manually create myself a nav menu. I have a custom taxonomy I'm using called Category and I'm trying to return the link for it for my tags in the menu using the get_category_link() function.
foreach ($categories as $category) {
if ($category->parent == 0) { //Check to see it is a parent
$output .= '<li>';
$output .= '' . $category->name . ''; //display parent taxonomy category
}
}
But it always returns <a href="">. I can echo out the $category->cat_ID successfully so I know it is passing the ID into the function but I don't know why it's returning blank.
Am I missing something? Is it because these are custom taxonomies? They have slugs.
You need something like this for custom taxonomies:
$tax = 'cars';
$cats = get_terms( $tax, '' );
if ($cats) {
foreach($cats as $cat) {
$output .= "<li>";
$output .= '<a href="' . esc_attr(get_term_link($cat, $tax)) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a>';
$output .= "</li>";
}
}
Although you can easily add to the top of the script to get an array of all taxonomies to feed in if you wanted.

Categories