I need to display all taxonomy terms in a select box (form) for my search engine.
With my code i can only display 1 but i want to see all.
I need also to display in a first choice the name of the taxonomy
<select name="chambre" class="form-control selectform" data-live-search="true">
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'chambre');
if ($terms) {
$output = array();
foreach ($terms as $term) {
$output[] = '<option value="' . $term->name_id . '">' . $term->name . '</option>';
}
echo join(' ', $output);
};
?>
</select>
Related
I create a custom post type with ACF.
I have two different taxonomy for this custom type :
Project type
House type
In House type, i have 4 different category :
all types
House
Appartement
Building
Each post need 'all type' and one other (for example : all type, house)
i would like to display the main category : House and not display : all types
How can i hide 'all types' ?
My code :
<div class="detailfiche">
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'housetype');
if ($terms) {
$output = array();
foreach ($terms as $term) {
$output[] = '<span>' .$term->name .'</span>';
}
echo join( ' ', $output );
};
?>
</div>
Try this
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'housetype');
if ($terms)
{
$output = array();
foreach ($terms as $term)
{
if ($term->term_id != 222)
{
$output[] = '<span>' . $term->name . '</span>';
}
}
echo join(' ', $output);
};
?>
Change 222 in this line to your taxonomy id for exclude
$term->term_id != 222
I try to create list of child category with images thumbs.
I add new field to taxonomy (ACF plugin) “image_category” and now I try to add this image to the list:
<?php
$args = array('parent' => 3);
$categories = get_categories( $args );
$term = get_queried_object();
$image_category = get_field('image_category', $term);
foreach($categories as $category) {
echo '<div class="col-md-3 col-sm-12 col-xs-12 text-center pb-3"><img src="' . $image_category . '"><h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
echo '<p> <strong>Number post:</strong> '. $category->count . '</p></div>';
}
?>
I see category list, name, number post. Only image dont work.
Image link dont show. I have on consol only “img src=(unknown)”. What am I doing wrong?
I am using Woocommerce Product Add-on plugin to allow visitor to select multiple options on simple product. As the product is simple product Woocommerce display an Add to cart button on products page view instead of Choose option button linking on Product detail page where visitor can choose options.
I am looking to create a function that will display Choose option button on product by ID.
Here is my starting code.
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button' );
function change_product_button() {
global $product;
$id = $product->id;
if ($id == 91){
echo '' . __('View Product', 'woocommerce') . '';
} else {
// display usual add to cart button
}
}
Or maybe we can force product by id as non purchasable to simulate a variable product.
This is a working solution
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button', 10, 2 );
function change_product_button($html, $product) {
$values = array(190, 91);
$id = $product->id;
if(in_array($id, $values)){
$html= '' . __('Choose an option', 'woocommerce') . '';
} else {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
I was wondering if you could help me display a taxonomies child links, for example, I have a custom post type of "courses" with a custom taxonomy of "study_type" and categories like "security", "professional" etc. Currently I am correctly displaying the category title and description. I need to know how to get a permalink for the courses that fall into the categories and display them underneath the category description. Thanks for any help or advice you have to offer. Here is me code:
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$taxonomy = $type;
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
}
}
}
?>
use get_term_children
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$terms = get_terms( $type, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
$termchildren = get_term_children( $term->term_id, $type );
if($termchildren){
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
}
}
} ?>
I am not using the nivo slider wordpress plugin, instead I am using regular nivo slider jquery and implemented it to work in wordpress, and currently my "read more" button is as follows:
<a href='".get_permalink()."'>Read More</a>
What I want to implement is something like a get_permalinkpage? So basically I want to be able to make the read more link to a wordpress page of my choosing instead of the post's permalink. But I do not know how to implement a custom option to the posts page that would allow the user to say "Choose from pages to link the nivo slider slide to: (then shows pages on website)" and then output that selection.
Any help? This is the last thing I need to implement for our website!
Right, I have your answer here as it's something I did myself recently. This is really a question about custom metaboxes. Here's some resources on it - I got sent a link on it from a mate who recommends this;
http://www.deluxeblogtips.com/meta-box/
And in the Bones theme the author recommends this one;
https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
I will post some code here if you want to quickly get going with it. I would place the following code in a file in it's own and include it from your functions.php, ie;
require_once('includes/metabox-post.php');
Create an includes directory in your theme directory and create a file containing this code;
<?php
/* CUSTOM METABOX -----------------------------------------------------*/
//We create an array called $meta_box and set the array key to the relevant post type
$meta_box_post['post'] = array(
//This is the id applied to the meta box
'id' => 'post-format-meta',
//This is the title that appears on the meta box container
'title' => 'My Custom Metabox',
//This defines the part of the page where the edit screen section should be shown
'context' => 'normal',
//This sets the priority within the context where the boxes should show
'priority' => 'high',
//Here we define all the fields we want in the meta box
'fields' => array(
array(
'name' => 'Home Slider Link',
'desc' => 'You can create a custom link for the home slider image (ie to link to the shop). If left blank, it will by default link through to this post.',
'id' => 'home-slide-link',
'type' => 'text',
'default' => ''
)
)
);
add_action('admin_menu', 'meta_add_box_post');
//Add meta boxes to post types
function meta_add_box_post() {
global $meta_box_post;
foreach($meta_box_post as $post_type => $value) {
add_meta_box($value['id'], $value['title'], 'meta_format_box_post', $post_type, $value['context'], $value['priority']);
}
}
//Format meta boxes
function meta_format_box_post() {
global $meta_box_post, $post;
// Use nonce for verification
echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box_post[$post->post_type]['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>'.
'<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
break;
case 'textarea':
echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
break;
case 'select':
echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
foreach ($field['options'] as $option) {
echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' /<br /> '. $field['desc'];
break;
}
echo '<td>'.'</tr>';
}
echo '</table>';
}
// Save data from meta box
function meta_save_data_post($post_id) {
global $meta_box_post, $post;
//Verify nonce
if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
//Check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
//Check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($meta_box_post[$post->post_type]['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
add_action('save_post', 'meta_save_data_post');
?>
The this will add a new custom metabox to your posts which you can type in an alternative url into. This custom option will have the id home-slide-link. To use that url you would include the following in your template loop whilst building up the list of Nivoslider images;
<?php
if ( get_post_meta($post->ID, 'home-slide-link', true) ) :
$slideLink = get_post_meta($post->ID, 'home-slide-link', true);
else :
$slideLink = get_permalink();
endif;
echo '<img src="image link in here" />';
?>
So if the post has a url set for the slider link then it uses that, if not it defaults to the permalink.
Hope this helps you a bit!
Here's my solution based on yours.
<div id="slider">
<?php
$captions = array();
$tmp = $wp_query;
$wp_query = new WP_Query('cat='.$category.'&posts_per_page=$n_slices' );
if($wp_query->have_posts()) :
while($wp_query->have_posts()) :
$wp_query->the_post();
$captions[] = '<p>'.get_the_title($post->ID).'</p><p>'.get_the_excerpt().'</p>';
$image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'nivothumb');
$mykey_values = get_post_custom_values('home-slide-link');
?>
<a href="<?php echo $mykey_values[0] ?>">
<img src="<?php echo $image[0]; ?>" class="attachment-nivothumb wp-post-image" title="#caption<?php echo count($captions)-1; ?>" alt="<?php the_title_attribute(); ?>" />
</a>
<?php
endwhile;
endif;
$wp_query = $tmp;
?>
</div><!-- close #slider -->
<?php
foreach($captions as $key => $caption) :
?>
<div id="caption<?php echo $key; ?>" class="nivo-html-caption">
<?php echo $caption;?>
</div>
<?php
endforeach;
?>