On the homepage of our website, we have added a shortcode to show the product page of the featured product.
I have added a SKU 'featured' to the featured product, so I could use the following shortcode to show the featured product [product_page sku="featured"].
Now I would like to modify the code of the shortcode so the name of the product links to the full featured product page.
Below is the WooCommerce that sets up the product_page.
/**
* Show a single product page.
*
* #param array $atts Attributes.
* #return string
*/
public static function product_page( $atts ) {
if ( empty( $atts ) ) {
return '';
}
if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) {
return '';
}
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
);
if ( isset( $atts['sku'] ) ) {
$args['meta_query'][] = array(
'key' => '_sku',
'value' => sanitize_text_field( $atts['sku'] ),
'compare' => '=',
);
$args['post_type'] = array( 'product', 'product_variation' );
}
if ( isset( $atts['id'] ) ) {
$args['p'] = absint( $atts['id'] );
}
// Don't render titles if desired.
if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
}
// Change form action to avoid redirect.
add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );
$single_product = new WP_Query( $args );
$preselected_id = '0';
// Check if sku is a variation.
if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) {
$variation = new WC_Product_Variation( $single_product->post->ID );
$attributes = $variation->get_attributes();
// Set preselected id to be used by JS to provide context.
$preselected_id = $single_product->post->ID;
// Get the parent product object.
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'p' => $single_product->post->post_parent,
);
$single_product = new WP_Query( $args );
?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
var $variations_form = $( '[data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"]' ).find( 'form.variations_form' );
<?php foreach ( $attributes as $attr => $value ) { ?>
$variations_form.find( 'select[name="<?php echo esc_attr( $attr ); ?>"]' ).val( '<?php echo esc_js( $value ); ?>' );
<?php } ?>
});
</script>
<?php
}
// For "is_single" to always make load comments_template() for reviews.
$single_product->is_single = true;
ob_start();
global $wp_query;
// Backup query object so following loops think this is a product page.
$previous_wp_query = $wp_query;
// #codingStandardsIgnoreStart
$wp_query = $single_product;
// #codingStandardsIgnoreEnd
wp_enqueue_script( 'wc-single-product' );
while ( $single_product->have_posts() ) {
$single_product->the_post()
?>
<div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>">
<?php wc_get_template_part( 'content', 'single-product' ); ?>
</div>
<?php
}
// Restore $previous_wp_query and reset post data.
// #codingStandardsIgnoreStart
$wp_query = $previous_wp_query;
// #codingStandardsIgnoreEnd
wp_reset_postdata();
// Re-enable titles if they were removed.
if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
}
remove_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
You can see they address the product_title with $atts['show_title']. So I guess we should wrap this with a href link of the product link, but I'm afraid my knowledge in that area lacks a bit.
Can somebody assist me with this, please? Much appreciated!
Use below code snippet in your functions.php and have a look.
It will remove the existing title and add new with the anchor tag.
remove_action('woocommerce_single_product_summary','woocommerce_template_single_title',5);
add_action('woocommerce_single_product_summary', 'woocommerce_my_single_title',5);
if ( ! function_exists( 'woocommerce_my_single_title' ) ) {
function woocommerce_my_single_title() {
?>
<span>dfgdfg<?php the_title(); ?></span>
<?php
}
}
Note: If it does not remove then you can give some css to hide the existing title based on parent class.
I searched and did not find this question has been posted before.
At the top of each post the meta data is displayed including author, entry date, updated date, and taxonomy. Each of these also include a url. I want to display the metas but remove the links. I have found the php file and succesfully modified it to achieve desired result. What I need help with is how to make these modifications within my child theme so the changes are not overridden after a theme update.
I have been trying to get some guidance from the theme publisher on their github account but so far no response.
Customizr Free version 4.0.11
Here are the details:
I was able to locate the php file: core/front/models/content/post-metas/class-model-post_metas.php
And I was able to locate and modify the portion of the function that inserts the url link into the author-meta element. NOTE, the example below only removes links from the author meta tag. Similar modifications are done for the other metas. This is a private function so I don't think I can call this from functions.php.
I made changes to the function named: czr_fn_get_meta_author()
private function czr_fn_get_meta_author() {
$author_id = null;
if ( is_single() )
if ( ! in_the_loop() ) {
global $post;
$author_id = $post->post_author;
}
return apply_filters(
'tc_author_meta',
sprintf( '<span class="author vcard author_name"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>' ,
esc_url( get_author_posts_url( get_the_author_meta( 'ID', $author_id ) ) ),
esc_attr( sprintf( __( 'View all posts by %s' , 'customizr' ), get_the_author_meta( 'display_name', $author_id ) ) ),
get_the_author_meta( 'display_name', $author_id )
)
);//end filter
}
And here is the modified version:
private function czr_fn_get_meta_author() {
$author_id = null;
if ( is_single() )
if ( ! in_the_loop() ) {
global $post;
$author_id = $post->post_author;
}
return apply_filters(
'tc_author_meta',
sprintf( '<span class="author vcard author_name">%3$s</span>' ,
esc_url( get_author_posts_url( get_the_author_meta( 'ID', $author_id ) ) ),
esc_attr( sprintf( __( 'View all posts by %s' , 'customizr' ), get_the_author_meta( 'display_name', $author_id ) ) ),
get_the_author_meta( 'display_name', $author_id )
)
);//end filter
}
So I can figure out how to do the same for entry-meta and others. Now I just need to know how to make this work in my child theme so that it will not be erased by updates.
Please and thank you.
I have a potential solution to this issue. I was able to take a copy of the file I listed above 'class-model-post-metas.php' and place that in my child theme using the same directory structure. I then modified the file to check if the user level of the post 'author' is set to subscriber. If so, I strip out the href. Otherwise, normal behavior is performed.
The reason for this is that when adding a user forum plugin called CM Answers Pro, the users are able to login and register using social login (oAuth). I default all new registrations at subscriber level. So all my forum participants are held at subscriber level. Which is what created this issue in the first place. Every time a new post is added to the forum, the user gets tagged as the 'author' of the post. Even though they do not have Author level privileges.
When a single post from the Q&A forum is displayed, Customizr sets the post metas, including links, for the 'author' name and the entry date. These links create 404's because they are constructed making the assumption each post in the Q&A forum is a blog post.
The code below the modifications that correctly remove the links from the post metas when the user is set to subscriber level. The modifications were made to the following two functions: czr_fn_get_meta_date() and czr_fn_get_meta_author(). You can compare this to the original source code available on the Customizr GitHub repo. https://github.com/presscustomizr/customizr
<?php
class CZR_post_metas_model_class extends CZR_Model {
/* PUBLIC GETTERS */
public function czr_fn_get_cat_list( $limit = false, $sep = '' ) {
return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_categories' ) ) ? $this -> czr_fn_get_meta( 'categories', $limit, $sep ) : '';
}
public function czr_fn_get_tag_list( $limit = false, $sep = '' ) {
return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_tags' ) ) ? $this -> czr_fn_get_meta( 'tags', $limit, $sep ) : '';
}
public function czr_fn_get_author( $before = null ) {
return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_author' ) ) ? $this -> czr_fn_get_meta( 'author', array( $before ) ) : '';
}
public function czr_fn_get_publication_date( $permalink = false, $before = null ) {
return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_publication_date' ) ) ? $this -> czr_fn_get_meta( 'pub_date', array(
'',
$permalink,
$before = null ) ) : '';
}
public function czr_fn_get_update_date( $permalink = false, $before = null ) {
return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_update_date' ) ) &&
false !== czr_fn_post_has_update() ?
$this -> czr_fn_get_meta( 'up_date', array( '', $permalink ) ) : '';
}
/* END PUBLIC GETTERS */
/* HELPERS */
protected function czr_fn_get_meta( $meta, $params = array(), $separator = '' ) {
$params = is_array( $params ) ? $params : array( $params );
return czr_fn_stringify_array( call_user_func_array( array( $this, "czr_fn_meta_generate_{$meta}" ), $params ), $separator );
}
private function czr_fn_meta_generate_categories( $limit = false ) {
return $this -> czr_fn_meta_generate_tax_list( $hierarchical = true, $limit );
}
private function czr_fn_meta_generate_tags( $limit = false ) {
return $this -> czr_fn_meta_generate_tax_list( $hierarchical = false, $limit );
}
private function czr_fn_meta_generate_author( $before ) {
$author = $this -> czr_fn_get_meta_author();
$before = is_null($before) ? __( 'by ', 'customizr-child' ) :'';
return '<span class="author-meta">' . $before . $author . '</span>';
}
private function czr_fn_meta_generate_pub_date( $format = '', $permalink = false, $before = null ) {
$date = $this -> czr_fn_get_meta_date( 'publication', $format, $permalink );
$before = is_null($before) ? __( 'Published ', 'customizr' ) :'';
return $before . $date;
}
private function czr_fn_meta_generate_up_date( $format = '', $permalink = false, $before = null ) {
$date = $this -> czr_fn_get_meta_date( 'update', $format, $permalink );
$before = is_null($before) ? __( 'Updated ', 'customizr' ) :'';
return $before . $date;
}
protected function czr_fn_get_term_css_class( $_is_hierarchical ) {
$_classes = array();
if ( $_is_hierarchical )
array_push( $_classes , 'tax__link' );
else
array_push( $_classes , 'tag__link btn btn-skin-dark-oh inverted' );
return $_classes;
}
/**
* Helper
* Return the date post metas
*
* #package Customizr
* #since Customizr 3.2.6
*/
protected function czr_fn_get_meta_date( $pub_or_update = 'publication', $_format = '', $permalink = false ) {
if ( 'short' == $_format )
$_format = 'j M, Y';
$_format = apply_filters( 'czr_meta_date_format' , $_format );
$_use_post_mod_date = apply_filters( 'czr_use_the_post_modified_date' , 'publication' != $pub_or_update );
// get user level to be used to turn meta links on or off
$subscriber_level = get_the_author_meta('user_level');
if ($subscriber_level == 0) {
// user is a subscriber
return apply_filters(
'tc_date_meta',
sprintf( '<span><time class="entry-date %3$s" datetime="%4$s">%5$s</time></span>' ,
$permalink ? esc_url( get_the_permalink() ) : esc_url( get_day_link( get_the_time( 'Y' ), get_the_time( 'm' ), get_the_time( 'd' ) ) ),
$permalink ? esc_attr( the_title_attribute( array( 'before' => __('Permalink to: ', 'customizr'), 'echo' => false ) ) ) : esc_attr( get_the_time() ),
'publication' == $pub_or_update ? 'published updated' : 'updated',
$_use_post_mod_date ? esc_attr( get_the_modified_date('c') ) : esc_attr( get_the_date( 'c' ) ),
$_use_post_mod_date ? esc_html( get_the_modified_date( $_format ) ) : esc_html( get_the_date( $_format ) )
),
$_use_post_mod_date,
$_format
);//end filter
} else {
// user is not a subscriber
return apply_filters(
'tc_date_meta',
sprintf( '<time class="entry-date %3$s" datetime="%4$s">%5$s</time>' ,
$permalink ? esc_url( get_the_permalink() ) : esc_url( get_day_link( get_the_time( 'Y' ), get_the_time( 'm' ), get_the_time( 'd' ) ) ),
$permalink ? esc_attr( the_title_attribute( array( 'before' => __('Permalink to: ', 'customizr'), 'echo' => false ) ) ) : esc_attr( get_the_time() ),
'publication' == $pub_or_update ? 'published updated' : 'updated',
$_use_post_mod_date ? esc_attr( get_the_modified_date('c') ) : esc_attr( get_the_date( 'c' ) ),
$_use_post_mod_date ? esc_html( get_the_modified_date( $_format ) ) : esc_html( get_the_date( $_format ) )
),
$_use_post_mod_date,
$_format
);//end filter
}
}
/**
* Helper
* Return the post author metas
*
* #package Customizr
* #since Customizr 3.2.6
*/
// LET'S SEE IF WE CAN UPDATE THIS FUNCTION TO CHECK IF 'AUTHOR' IS ACTUALLY AN AUTHOR
// BECUASE POSTS FROM THE CM ANSWERS PLUGIN ARE SUBSCRIBERS
private function czr_fn_get_meta_author() {
$author_id = null;
if ( is_single() )
$subscriber_level = 0;
if ( ! in_the_loop() ) {
global $post;
$author_id = $post->post_author;
}
$subscriber_level = get_the_author_meta('user_level');
// if ($subscriber_level == 0) {
// print_r("Subscriber ");
// } else {
// print_r("Not Subscriber ");
// }
if($subscriber_level == 0) {
// user role is subscriber
return apply_filters(
'tc_author_meta',
sprintf( '<span class="author vcard author_name">%3$s</span>' ,
esc_url( get_author_posts_url( get_the_author_meta( 'ID', $author_id ) ) ),
esc_attr( sprintf( __( 'View all posts by %s' , 'customizr' ), get_the_author_meta( 'display_name', $author_id ) ) ),
get_the_author_meta( 'display_name', $author_id )
)
);//end filter
} else {
// user role is higher than subscriber level
return apply_filters(
'tc_author_meta',
sprintf( '<span class="author vcard author_name"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>' ,
esc_url( get_author_posts_url( get_the_author_meta( 'ID', $author_id ) ) ),
esc_attr( sprintf( __( 'View all posts by %s' , 'customizr' ), get_the_author_meta( 'display_name', $author_id ) ) ),
get_the_author_meta( 'display_name', $author_id )
)
);//end filter
}
}
/**
* Helper
* #return string of all the taxonomy terms (including the category list for posts)
* #param hierarchical tax boolean => true = categories like, false = tags like
*
* #package Customizr
* #since Customizr 3.0
*/
private function czr_fn_meta_generate_tax_list( $hierarchical, $limit = false ) {
$post_terms = $this -> czr_fn_get_term_of_tax_type( $hierarchical, $limit );
if ( ! $post_terms )
return;
$_terms_html_array = array_map( array( $this , 'czr_fn_meta_term_view' ), $post_terms );
return $_terms_html_array;
}
/**
* Helper
* #return string of the single term view
* #param $term object
*
* #package Customizr
* #since Customizr 3.3.2
*/
private function czr_fn_meta_term_view( $term ) {
$_is_hierarchical = is_taxonomy_hierarchical( $term -> taxonomy );
$_classes = czr_fn_stringify_array( apply_filters( 'czr_meta_tax_class', $this -> czr_fn_get_term_css_class( $_is_hierarchical ), $_is_hierarchical, $term ) );
// (Rocco's PR Comment) : following to this https://wordpress.org/support/topic/empty-articles-when-upgrading-to-customizr-version-332
// I found that at least wp 3.6.1 get_term_link($term->term_id, $term->taxonomy) returns a WP_Error
// Looking at the codex, looks like we can just use get_term_link($term), when $term is a term object.
// Just this change avoids the issue with 3.6.1, but I thought should be better make a check anyway on the return type of that function.
$_term_link = is_wp_error( get_term_link( $term ) ) ? '' : get_term_link( $term );
$_to_return = $_term_link ? '<a %1$s href="%2$s" title="%3$s"> <span>%4$s</span> </a>' : '<span %1$s> %4$s </span>';
$_to_return = $_is_hierarchical ? $_to_return : '<li>' . $_to_return . '</li>';
return apply_filters( 'czr_meta_term_view' , sprintf($_to_return,
$_classes ? 'class="'. $_classes .'"' : '',
$_term_link,
esc_attr( sprintf( __( "View all posts in %s", 'customizr' ), $term -> name ) ),
$term -> name
)
);
}
/**
* Helper to return the current post terms of specified taxonomy type : hierarchical or not
*
* #return boolean (false) or array
* #param boolean : hierarchical or not
* #package Customizr
* #since Customizr 3.1.20
*
*/
private function czr_fn_get_term_of_tax_type( $hierarchical = true, $limit = false ) {
//var declaration
$post_type = get_post_type( czr_fn_get_id() );
$tax_list = get_object_taxonomies( $post_type, 'object' );
$_tax_type_list = array();
$_tax_type_terms_list = array();
if ( empty($tax_list) )
return false;
//filter the post taxonomies
while ( $_tax_object = current($tax_list) ) {
// cast $_tax_object stdClass object in an array to access its property 'public'
// fix for PHP version < 5.3 (?)
$_tax_object = (array) $_tax_object;
//Is the object well defined ?
if ( ! isset($_tax_object['name']) ) {
next($tax_list);
continue;
}
$_tax_name = $_tax_object['name'];
//skip the post format taxinomy
if ( ! $this -> czr_fn_is_tax_authorized( $_tax_object, $post_type ) ) {
next($tax_list);
continue;
}
if ( (bool) $hierarchical === (bool) $_tax_object['hierarchical'] )
$_tax_type_list[$_tax_name] = $_tax_object;
next($tax_list);
}
if ( empty($_tax_type_list) )
return false;
$found = 0;
//fill the post terms array
foreach ($_tax_type_list as $tax_name => $data ) {
$_current_tax_terms = get_the_terms( czr_fn_get_id() , $tax_name );
//If current post support this tax but no terms has been assigned yet = continue
if ( ! $_current_tax_terms )
continue;
while( $term = current($_current_tax_terms) ) {
$_tax_type_terms_list[$term -> term_id] = $term;
if ( $limit > 0 && ++$found == $limit )
break 2;
next($_current_tax_terms);
}
}
/*if ( ! empty($_tax_type_terms_list) && $limit > 0 )
$_tax_type_terms_list = array_slice( $_tax_type_terms_list, 0, $limit );
*/
return empty($_tax_type_terms_list) ? false : apply_filters( "czr_tax_meta_list" , $_tax_type_terms_list , $hierarchical );
}
/**
* Helper : check if a given tax is allowed in the post metas or not
* A tax is authorized if :
* 1) not in the exclude list
* 2) AND not private
*
* #return boolean (false)
* #param $post_type, $_tax_object
* #package Customizr
* #since Customizr 3.3+
*
*/
private function czr_fn_is_tax_authorized( $_tax_object , $post_type ) {
$_in_exclude_list = in_array(
$_tax_object['name'],
apply_filters_ref_array ( 'czr_exclude_taxonomies_from_metas' , array( array('post_format') , $post_type , czr_fn_get_id() ) )
);
$_is_private = false === (bool) $_tax_object['public'] && apply_filters_ref_array( 'czr_exclude_private_taxonomies', array( true, $_tax_object['public'], czr_fn_get_id() ) );
return ! $_in_exclude_list && ! $_is_private;
}
/* Customizer: allow dynamic visibility in the preview */
function czr_fn_body_class( $_classes/*array*/ ) {
if ( ! czr_fn_is_customizing() )
return $_classes;
if ( 0 == esc_attr( czr_fn_opt( 'tc_show_post_metas' ) ) )
$_classes[] = 'hide-all-post-metas';
if (
( is_singular() && ! is_page() && ! czr_fn_is_real_home() && 0 == esc_attr( czr_fn_opt( 'tc_show_post_metas_single_post' ) ) ) ||
( ! is_singular() && ! czr_fn_is_real_home() && ! is_page() && 0 == esc_attr( czr_fn_opt( 'tc_show_post_metas_post_lists' ) ) ) ||
( czr_fn_is_real_home() ) && 0 == esc_attr( czr_fn_opt( 'tc_show_post_metas_home' ) )
)
$_classes[] = 'hide-post-metas';
return $_classes;
}
}//end of class
We are currently running Facebook Instant Articles on our site. The problem we are running into is that it grabs the default author and no subtitle. I have them both defined in CMB2:
$cmb->add_field( array(
'name' => __( 'Author:', 'cmb2' ),
'id' => $prefix . 'author',
'type' => 'text_medium',
) );
$cmb->add_field( array(
'name' => __( 'Lede:', 'cmb2' ),
'id' => $prefix . 'lede',
'type' => 'textarea',
) );
I can call it through the following:
$lede = get_post_meta( get_the_ID(), '_cmb_lede', true );
echo esc_html( $lede );
and:
$author = get_post_meta( get_the_ID(), '_cmb_author', true );
echo 'By ' . esc_html( $author );
Unfortunately this leads to an inconsistency in the IA Wordpress Plugin, which calls subtitles:
if ( ! is_null( $this->_subtitle ) ) {
return $this->_subtitle;
}
and Authors:
$authors = array();
$wp_user = get_userdata( $this->_post->post_author );
if ( is_a( $wp_user, 'WP_User' ) ) {
$author = new stdClass;
$author->ID = $wp_user->ID;
$author->display_name = $wp_user->data->display_name;
$author->first_name = $wp_user->first_name;
$author->last_name = $wp_user->last_name;
$author->user_login = $wp_user->data->user_login;
$author->user_nicename = $wp_user->data->user_nicename;
$author->user_email = $wp_user->data->user_email;
$author->user_url = $wp_user->data->user_url;
$author->bio = $wp_user->description;
$authors[] = $author;
}
/**
* Filter the post author(s).
*
* #since 0.1
*
* #param array $authors The current post author(s).
* #param int $post_id The instant article post.
*/
$authors = apply_filters( 'instant_articles_authors', $authors, $this->_post->ID );
return $authors;
Update:
I have made some progress. This is in my functions.php I am close but not quite there:
use Facebook\InstantArticles\Elements\Author;
add_action( 'instant_articles_after_transform_post', function ($ia_post) {
$instant_article = $ia_post->instant_article;
$post_id = $ia_post->get_the_id();
$author = get_post_meta( $post_id, '_cmb_author', true );
$instant_article->withHeader(addAuthor( Author::create()->withName($author) ));
} );
What is the proper way to overwrite in the functions.php these custom fields so that it can properly pull it into the Facebook Instant Articles plugin?
I am trying to make the theme I am using (DICE) into pulling through title text of images.
The code I am using is below;
if ( has_post_thumbnail() ) {
if ( $linking == 'lightbox' ) {
$alt = esc_attr(get_the_title( $post->ID ) );
$the_title = esc_attr(get_the_title( $post->ID ) );
$img = get_the_post_thumbnail( $post->ID, $size, array( 'alt' => $alt, 'title' => $the_title ) );
}
else
$img = get_the_post_thumbnail( $post->ID, $size );
}
elseif( $placeholder ) {
$img = btp_render_placeholder( $size, '', false );
}
else {
return;
}
The only line I have added is
$the_title = esc_attr(get_the_title( $post->ID ) );
with
'title' => $the_title
in the array aswell. The alt text is pulling through fine, so I'm unsure as to why the title text isn't pulling through?
Any help is appreciated. Thanks.
Using two separate variables to get the same information is bad programming practice.
Try to use only one variable, like this:
if ( $linking == 'lightbox' ) {
$post_title = esc_attr(get_the_title( $post->ID ) );
//The Image
$img = get_the_post_thumbnail( $post->ID, $size, array( 'alt' => $post_title, 'title' => $post_title ) );
}
In my wordpress theme I am creating some custom fields like
cp_street, cp_city, cp_mobile_no, cp_company_name
and much more. Which is displayed in single_ad_listing.php page, and code is
cp_get_ad_details( $post->ID, $cat_id );
and theme-function.php page code is
// 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>';
}
}
All fields display serially, but I want to like address cp_street, cp_city top of page and mobile no.( cp_mobile_no) right side of page and rest of fields display same place.
Please help me...
In this tutorial we will look at how the Advanced Custom Fields plugin can be used to create a professional home page .
i hope this like use full.. here simple code..