在我的wordpress主题中,我正在创建一些自定义字段,如
cp_street,cp_city,cp_mobile_no,cp_company_name
等等。其中显示在 single_ad_listing.php 页面中,代码为
cp_get_ad_details( $post->ID, $cat_id );
和theme-function.php页面代码是
// display all the custom fields on the single ad page, by default they are placed in the list area
if ( ! function_exists('cp_get_ad_details') ) {
function cp_get_ad_details( $post_id, $category_id, $location = 'list' ) {
global $wpdb;
// see if there's a custom form first based on category id.
$form_id = cp_get_form_id( $category_id );
$post = get_post( $post_id );
if ( ! $post )
return;
// if there's no form id it must mean the default form is being used
if ( ! $form_id ) {
// get all the custom field labels so we can match the field_name up against the post_meta keys
$sql = "SELECT field_label, field_name, field_type FROM $wpdb->cp_ad_fields";
} else {
// now we should have the formid so show the form layout based on the category selected
$sql = $wpdb->prepare( "SELECT f.field_label, f.field_name, f.field_type, m.field_pos FROM $wpdb->cp_ad_fields f "
. "INNER JOIN $wpdb->cp_ad_meta m ON f.field_id = m.field_id WHERE m.form_id = %s ORDER BY m.field_pos ASC", $form_id );
}
$results = $wpdb->get_results( $sql );
if ( ! $results ) {
_e( 'No ad details found.', APP_TD );
return;
}
// allows to hook before ad details
cp_action_before_ad_details( $results, $post, $location );
foreach ( $results as $result ) {
// external plugins can modify or disable field
$result = apply_filters( 'cp_ad_details_field', $result, $post, $location );
if ( ! $result )
continue;
$disallow_fields = array( 'cp_price', 'cp_currency' );
if ( in_array( $result->field_name, $disallow_fields ) )
continue;
$post_meta_val = get_post_meta( $post->ID, $result->field_name, true );
if ( empty( $post_meta_val ) )
continue;
if ( $location == 'list' ) {
if ( $result->field_type == 'text area' )
continue;
if ( $result->field_type == 'checkbox' ) {
$post_meta_val = get_post_meta( $post->ID, $result->field_name, false );
$post_meta_val = implode( ", ", $post_meta_val );
}
$args = array( 'value' => $post_meta_val, 'label' => $result->field_label, 'id' => $result->field_name, 'class' => '' );
$args = apply_filters( 'cp_ad_details_' . $result->field_name, $args, $result, $post, $location );
if ( $args )
echo '<li id="' . $args['id'] . '" class="' . $args['class'] . '"><span>' . esc_html( translate( $args['label'], APP_TD ) ) . ':</span> ' . appthemes_make_clickable( $args['value'] ) . '</li>';
} elseif ( $location == 'content' ) {
if ( $result->field_type != 'text area' )
continue;
$args = array( 'value' => $post_meta_val, 'label' => $result->field_label, 'id' => $result->field_name, 'class' => 'custom-text-area dotted' );
$args = apply_filters( 'cp_ad_details_' . $result->field_name, $args, $result, $post, $location );
if ( $args )
echo '<div id="' . $args['id'] . '" class="' . $args['class'] . '"><h3>' . esc_html( translate( $args['label'], APP_TD ) ) . '</h3> ' . appthemes_make_clickable( $args['value'] ) . '</div>';
}
}
所有字段都是连续显示的,但我想在页面右侧显示地址cp_street, cp_city
,在页面右侧显示移动号码(cp_mobile_no),其余字段显示相同的位置。
请帮帮我......
答案 0 :(得分:0)
在本教程中,我们将了解如何使用Advanced Custom Fields插件创建专业主页。