Php 从自定义URL元字段和快捷码分类中获取URL
更新:多亏了你们这些可爱的人的帮助,我成功地拼凑出一个解决方案来解决这个问题的短代码部分。请参阅下面的解决方案。 我正在努力用我的属性在我的设置的快捷码版本中输出URL。最后一部分。我已经将用于实现的所有代码分割成一个文件以供参考。它在查看产品时工作得非常好,但在使用快捷码输出列表属性列表时就没有这么好了。似乎无法获取要显示的URL 我为添加到WooCommerce属性中的URL字段设置了以下代码Php 从自定义URL元字段和快捷码分类中获取URL,php,wordpress,woocommerce,hook-woocommerce,Php,Wordpress,Woocommerce,Hook Woocommerce,更新:多亏了你们这些可爱的人的帮助,我成功地拼凑出一个解决方案来解决这个问题的短代码部分。请参阅下面的解决方案。 我正在努力用我的属性在我的设置的快捷码版本中输出URL。最后一部分。我已经将用于实现的所有代码分割成一个文件以供参考。它在查看产品时工作得非常好,但在使用快捷码输出列表属性列表时就没有这么好了。似乎无法获取要显示的URL 我为添加到WooCommerce属性中的URL字段设置了以下代码 <?php /** * Register term fields */ add_acti
<?php
/**
* Register term fields
*/
add_action( 'init', 'register_attributes_url_meta' );
function register_attributes_url_meta() {
$attributes = wc_get_attribute_taxonomies();
foreach ( $attributes as $tax ) {
$name = wc_attribute_taxonomy_name( $tax->attribute_name );
add_action( $name . '_add_form_fields', 'add_attribute_url_meta_field' );
add_action( $name . '_edit_form_fields', 'edit_attribute_url_meta_field', 10 );
add_action( 'edit_' . $name, 'save_attribute_url' );
add_action( 'create_' . $name, 'save_attribute_url' );
}
}
/**
* Add term fields form
*/
function add_attribute_url_meta_field() {
wp_nonce_field( basename( __FILE__ ), 'attrbute_url_meta_nonce' );
?>
<div class="form-field">
<label for="attribute_url"><?php _e( 'URL', 'domain' ); ?></label>
<input type="url" name="attribute_url" id="attribute_url" value="" />
</div>
<?php
}
/**
* Edit term fields form
*/
function edit_attribute_url_meta_field( $term ) {
$url = get_term_meta( $term->term_id, 'attribute_url', true );
wp_nonce_field( basename( __FILE__ ), 'attrbute_url_meta_nonce' );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="attribute_url"><?php _e( 'URL', 'domain' ); ?></label></th>
<td>
<input type="url" name="attribute_url" id="attribute_url" value="<?php echo esc_url( $url ); ?>" />
</td>
</tr>
<?php
}
/**
* Save term fields
*/
function save_attribute_url( $term_id ) {
if ( ! isset( $_POST['attribute_url'] ) || ! wp_verify_nonce( $_POST['attrbute_url_meta_nonce'], basename( __FILE__ ) ) ) {
return;
}
$old_url = get_term_meta( $term_id, 'attribute_url', true );
$new_url = esc_url( $_POST['attribute_url'] );
if ( ! empty( $old_url ) && $new_url === '' ) {
delete_term_meta( $term_id, 'attribute_url' );
} else if ( $old_url !== $new_url ) {
update_term_meta( $term_id, 'attribute_url', $new_url, $old_url );
}
}
/**
* Show term URL
*/
add_filter( 'woocommerce_attribute', 'make_product_atts_linkable', 10, 3 );
function make_product_atts_linkable( $text, $attribute, $values ) {
$new_values = array();
foreach ( $values as $value ) {
if ( $attribute['is_taxonomy'] ) {
$term = get_term_by( 'name', $value, $attribute['name'] );
$url = get_term_meta( $term->term_id, 'attribute_url', true );
if ( ! empty( $url ) ) {
$val = '<a target="_blank" href="' . esc_url( $url ) . '" title="' . esc_attr( $value ) . '">' . $value . '</a>';
array_push( $new_values, $val );
} else {
array_push( $new_values, $value );
}
} else {
$matched = preg_match_all( "/\[([^\]]+)\]\(([^)]+)\)/", $value, $matches );
if ( $matched && count( $matches ) == 3 ) {
$val = '<a target="_blank" href="' . esc_url( $matches[2][0] ) . '" title="' . esc_attr( $matches[1][0] ) . '">' . sanitize_text_field( $matches[1][0] ) . '</a>';
array_push( $new_values, $val );
} else {
array_push( $new_values, $value );
}
}
}
$text = implode( ', ', $new_values );
return $text;
}
// CUSTOM SHORTCODE FOR ATTR
add_shortcode( 'product_attributes', 'get_product_attributes' );
function get_product_attributes() {
$url = get_term_meta( $term->term_id, 'attribute_url', true );
$taxonomies = get_terms( array(
'taxonomy' => 'pa_normerstandarder',
'hide_empty' => false
) );
if ( !empty($taxonomies) ) :
$output = '<table class="standarder-tabell"><thead> <th>Norm/standard</th> <th>Beskrivelse</th> <th>Url</th> </thead><tbody>';
foreach( $taxonomies as $category ) {
if( $category->parent == 0 ) {
$output.= '<tr class="norm-'. esc_attr( $category->name ) .'">';
$output.= '<td>'.esc_attr( $category->name ). '</td>';
$output.= '<td>'.esc_attr( $category->description ). '</td>';
$output.= '<td>' . esc_url( $url ) . '</td>';
$output.='</tr>';
}
}
$output.='</tbody></table>';
endif;
return $output;
}
您认为$term->term\u id
在您的获取产品属性
函数中会是什么?您没有定义任何名为$term
的参数,它不是通过全局导入的–所以它应该是什么?get\u product\u attributes()
需要知道$term
是什么,-就像您在编辑属性\u url\u meta\u字段
中所做的一样,感谢你们两位。有了这些,我设法想出了一个解决办法。更新了原来的帖子。
// CUSTOM SHORTCODE FOR ATTR
add_shortcode( 'product_attributes', 'get_product_attributes' );
function get_product_attributes() {
$terms = get_terms( array(
'taxonomy' => 'pa_normerstandarder',
'hide_empty' => false
) );
if ( !empty($terms) ) :
$output = '<table class="standarder-tabell"><thead> <th>Norm/standard</th> <th>Beskrivelse</th> <th>Url</th> </thead><tbody>';
foreach( $terms as $term ) {
$url = get_term_meta( $term->term_id, 'attribute_url', true );
if( $category->parent == 0 ) {
$output.= '<tr class="norm-'. esc_attr( $term->name ) .'">';
$output.= '<td>'.esc_attr( $term->name ). '</td>';
$output.= '<td>'.esc_attr( $term->description ). '</td>';
$output.= '<td>'.esc_attr( $url ). '</td>';
$output.='</tr>';
}
}
$output.='</tbody></table>';
endif;
return $output;
}