get custom field value from taxonomy in wordpress - php

i make a custom field in categories, it save and update data successfully, but i want to show the value of custom field data in archive page, i search a lot about this but in vain
Please help me
here is my code
Create custom field:
add_action ( 'category_add_form_fields', 'extra_field');
add_action ( 'category_edit_form_fields', 'extra_field');
function extra_field($term) { //check for existing featured ID
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id");
?>
<table width="100%" border="0" cellspacing="3" cellpadding="0" style="margin-bottom:20px;">
<tr>
<td><strong>Image</strong></td>
</tr>
<tr>
<td><input type="text" size="40" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>" /></td>
</tr>
<tr>
<td><p>A quick brown fox jumps over the lazy dog.</p></td>
</tr>
</table>
<?php
}
Save / Update data:
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = $_POST['term_meta'];
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_category', 'save_taxonomy_custom_meta' );
add_action( 'create_category', 'save_taxonomy_custom_meta' );
One more thing, can i make an extra field in db in wp_terms, because it save in wp_options

Use this
first of all you get category id on taxonomy page.I suppose one category assign to each post.
$t_id = $term_id;
Then get value using this
get_option( "taxonomy_".$t_id );

I thanked to #yatendra to help me i got an idea from his answer so its work
here is answer
$queried_object = get_queried_object();
$t_id = $queried_object->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
echo "<img src=".$term_meta['custom_term_meta']." />";

I want to customize my category section so have use below code and it is working perfectly without any issue..
In
function.php
add_action ( 'edit_category_form_fields', function( $tag ){
$cat_title = get_term_meta( $tag->term_id, '_pagetitle', true );
?>
<tr class='form-field'>
<th scope='row'><label for='cat_page_title'><?php _e('Category Page Title'); ?></label></th>
<td>
<input type='text' name='cat_title' id='cat_title' value='<?php echo $cat_title ?>'>
<p class='description'><?php _e('Title for the Category '); ?></p>
</td>
</tr> <?php
});
add_action ( 'edited_category', function() {
if ( isset( $_POST['cat_title'] ) )
update_term_meta( $_POST['tag_ID'], '_pagetitle', $_POST['cat_title'] );
});
and call in index.php page for fronted
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
foreach( $categories as $category ) {
if($category->name !="Uncategorized")
{
$cat_title = get_term_meta( $category->term_id, '_pagetitle', true );
echo '
<div class="col-md-4">' . $category->name . '</div>
<div class="col-md-4">' . $category->description . '</div>
';
}
}
?>

Related

Additional image field in WooCommerce category

With help i've made custom field on WooCommerce's category, but i want to make it media field (to pick cover image and upload directly from form).
I'm using "How do I add custom fields to the categories in Woocommerce?"
// Add term page
function custom_product_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[category_cover]"><?php _e( 'Category Cover', 'category-cover' ); ?></label>
<input type="text" name="term_meta[category_cover]" id="term_meta[category_cover]" value="">
<p class="description"><?php _e( 'Enter a value for this field','category-cover' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_product_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[category_cover]"><?php _e( 'Category Cover', 'category-cover' ); ?></label></th>
<td>
<input type="text" name="term_meta[category_cover]" id="term_meta[category_cover]" value="<?php echo esc_attr( $term_meta['category_cover'] ) ? esc_attr( $term_meta['category_cover'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter an URL for category cover','category-cover' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
I dont know what should I tell more, but i will be really thankful for help me! :)

Saving metabox value not working [WP-PHP]

I'm trying to save a metabox input but it doesn't seem to work. I'm working with arrays (as I need my metabox to have 60 rows), so I assume the problem lies within them.
This is my metabox function for the admin (which works correctly showing the info I wanna show):
function mock_metabox() {
global $post;
// Nonce field
wp_nonce_field( basename( __FILE__ ), 'mock_fields' );
// init counter for meta array
$contadorglobal = 1;
$selecciones = array();
$equipos = array();
$equiposog = array();
while ( $contadorglobal <= 60 ){
$selecciones[$contadorglobal-1] = get_post_meta( $post->ID, '_seleccion_' . $contadorglobal, true );
$equipos[$contadorglobal-1] = get_post_meta( $post->ID, '_equipo_' . $contadorglobal, true );
$equiposog[$contadorglobal-1] = get_post_meta( $post->ID, '_equipoog_' . $contadorglobal, true );
$contadorglobal++;
}
// Output the fields
?>
<h3> Informacion del Mock </h3>
<table>
<tr>
<th> # </th>
<th> Jugador </th>
<th> Equipo </th>
<th> Equipo Original </th>
</tr>
<?
$contador = 1;
$teams = get_posts( array(
'post_type' => 'team',
'orderby' => 'title',
'order' => 'ASC',
'numberposts' => -1,
'post_status' => 'publish'
) );
while ( $contador <= 60 ){
?>
<tr>
<td><?php echo $contador ?></td>
<td><input type="text" name="<? 'jugador_' . $contador ?>" value="<?php echo $selecciones[$contador-1] ; ?>" />
<td><select name="<? 'equipo_' . $contador?>" ><?
foreach ( $teams as $team ) { ?>
<option value="<?php echo $team->ID; ?>" <?php checked( $equipos[$contador-1], $team->ID ); ?> > <?php echo $team->post_title; ?> </option> <? } ?> </select> </td>
<td><select name="<? 'equipoog_' . $contador ?>" ><?
foreach ( $teams as $team ) { ?>
<option value="<?php echo $team->ID; ?>" <?php checked( $equiposog[$contador-1], $team->ID ); ?> > <?php echo $team->post_title; ?> </option> <? } ?> </select> </td>
<? $contador++; ?>
</tr>
<?php } ?>
</table>
<?}
And this is the saving function (I thought the problem was with the while, but I tried removing it and it doesn't save any info either).
function mock_save_meta_box_data( $post_id ){
// verify taxonomies meta box nonce
if ( !isset( $_POST['mock_fields'] ) || !wp_verify_nonce( $_POST['mock_fields'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// guarda tipo traspaso
for ($contadorid = 1; $contadorid <= 60; $contadorid++) {
if ( isset( $_REQUEST['jugador_' . $contadorid] ) ) {
update_post_meta( $post_id, '_seleccion_' . $contadorid, $_POST['jugador_' . $contadorid] );
}
if ( isset( $_REQUEST['equipo_' . $contadorid] ) ) {
update_post_meta( $post_id, '_equipo_' . $contadorid, $_POST['equipo_' . $contadorid] );
}
if ( isset( $_REQUEST['equipoog_' . $contadorid] ) ) {
update_post_meta( $post_id, '_equipoog_' . $contadorid, $_POST['equipoog_' . $contadorid] );
}
}
}
add_action( 'save_post_mock', 'mock_save_meta_box_data' );
Any ideas? Thanks in advance!

WordPress: add check-boxes to custom taxonomy add/edit form

I need to add a list of check-boxes to custom taxonomy add/edit form.
I have this code for adding a text field for custom taxonomy form in my plugin and it works fine:
<?php
function taxonomy_edit_meta_field($term) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Term:' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'taxonomy_edit_meta_field', 10, 2 );
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
How I can add a list of check-boxes in the same way?
There were a problem with the saving not checked check-box input.
I have fixed it with input type="hidden" with the same value of "name" attribute, as in check-box input.
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Color:' ); ?></label></th>
<td>
<input type="hidden" value="0" name="term_meta[pa_color_attr]">
<input type="checkbox" <?php echo (!empty($term_meta['pa_color_attr']) ? ' checked="checked" ' : 'test'); ?> value="1" name="term_meta[pa_color_attr]" />
</td>
</tr>
you need to change the name of the hook.
add_action( 'edited_custom_tax', 'save_taxonomy_custom_meta', 10, 2 );

Echo woocommerce product thumbnail?

Im trying to echo WooCommerce thumbail of producs onto an orderform.
Not sure how though though. This is what I've got so far:
<?php echo woocommerce_get_product_thumbnail();?>
This give me the WooCommerce thumbnail placeholder. How do I pull the right one for each item in my orderform? The orderform has fields for order id, creationdate, status and price, so it does pull the proper id´s somewhere.
This is examples of the meta keys and other fields that pull info for each order on the same form, if any of this makes any sense.
<a href="<?php echo $order->get_view_order_url(); ?>">
<?php echo $order->get_order_number(); ?>
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order();
$order->populate( $customer_order );
$item_count = $order->get_item_count();
$customer_orders = get_posts(
apply_filters( 'woocommerce_my_account_my_orders_query',
array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types('view-orders'),
'post_status' => array_keys(wc_get_order_statuses())
)
)
);
This is how the php is set up. I added the first table class for the immage.
<tr>
<th class="order-number"><span class="nobr"><?php _e( 'Image', 'woocommerce' ); ?></span></th>
<th class="order-number"><span class="nobr"><?php _e( 'Order', 'woocommerce' ); ?></span></th>
<th class="order-date"><span class="nobr"><?php _e( 'Date', 'woocommerce' ); ?></span></th>
</tr>
Then Im trying to add the image.
<td class="order-image”>
<?php echo woocommerce_get_product_thumbnail(); ?>
</td>
<td class="order-number">
<a href="<?php echo $order->get_view_order_url(); ?>">
<?php echo $order->get_order_number(); ?>
</a>
</td>
<td class="order-date”>
<time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>
</td>
Keep in mind that WooCommerce already has all the items/costs/thumbnails for previous orders in the My Accounts section and customers can "order again" directly from their My Account area.
But, assuming you have the order ID you can get to the product thumbnails with the following. This will output a small list of items with their thumbnails and product name:
$order = wc_get_order( $order_id );
$items = $order->get_items();
if( $items ) {
echo '<ul class="ordered-items">';
foreach( $items as $item ){
$id = isset( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];
$product = wc_get_product( $id );
echo '<li>'. $product->get_image() . $product->get_title() . '</li>';
}
echo '</ul>';
}
Edit: To integrate into your code I would probably do the following
function so_28179558_get_order_thumbnail( $order ){
if( is_numeric( $order ) ){
$order = wc_get_order( $order_id );
}
if( is_wp_error( $order ) ){
return;
}
$order_thumb = '';
$items = $order->get_items();
if( $items ) {
foreach( $items as $item ){
$id = isset( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];
$product = wc_get_product( $id );
$order_thumb = $product->get_image();
continue;
}
}
return $order_thumb;
}
Then you can use it in your template like so:
<td class="order-image”>
<?php echo so_28179558_get_order_thumbnail( $order ); ?>
</td>

Show Custom Meta Field Linked to Custom Taxonomy on Front End

Hi I have a site with a Custom Post Type of 'Researcher', within that I have a taxonomy called 'Supervisors', and within that a meta field named 'Job Title'. I am trying to figure out how to display this 'Job Title' in the front end.
The code for the meta field (from functions.php) is as follows:
// Edit Supervisor Taxonomy
// Add term page
function jobtitle_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label>
<input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="">
<p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
</div>
<?php
}
add_action( 'supervisor_add_form_fields', 'jobtitle_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function jobtitle_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label></th>
<td>
<input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="<?php echo esc_attr( $term_meta['jobtitle'] ) ? esc_attr( $term_meta['jobtitle'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'supervisor_edit_form_fields', 'jobtitle_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_supervisor', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_supervisor', 'save_taxonomy_custom_meta', 10, 2 );
// End Edit Supervisor Taxonomy
Image showing the backend — http://d.pr/i/TS13
You will need:
The post ID of the researcher. On the single-researcher.php page (if your post type is named researcher) you should be able to use $post->ID to obtain the post ID of the researcher.
The name of the taxonomy. This can be obtained by reading the URL of the screenshot you posted previously. It should say: ...edit-tags.php?taxonomy=supervisors&.... I'll assume the taxonomy is named supervisors.
If you're on the single-researcher.php page, this might work... Note: $supervisor_tags is an array of tags for that researcher. Each researcher could have 0, 1, or more supervisors, so you might want to loop through them:
global $post;
$supervisor_tags = get_the_terms($post->ID, 'supervisors');
echo "<h2>Supervisors:</h2>";
foreach ($supervisor_tags as $tag){
$term_meta = get_option('taxonomy_' . $tag->term_id);
echo "<p>Name: " . $tag->name . " - Job Title: " . $term_meta['jobtitle'] . "</p>";
}
You can echo "<pre>"; print_r($variable); echo "</pre>"; to see what the $variable array looks like, if you're having trouble.

Categories