Friday, June 8, 2018

How to display pages with custom fields

h1. Use any of these useful functions
/* Pages: get featured img url */

function page_featured_img_url($size) {

 global $wp_query; 
 $page = $wp_query->get_queried_object(); 
 $page_id = $page->ID;

 $thumb_url_array = $image = wp_get_attachment_image_src(get_post_thumbnail_id($page_id), $size, true);
 $thumb_url = $thumb_url_array[0];

 return $thumb_url;
}

/* Pages: show title */

function page_show_title() {

 global $wp_query; 
 $page = $wp_query->get_queried_object(); 
 $page_id = $page->ID;

 $thumb_url = get_the_title($page_id);

 echo $thumb_url;
}

/* Pages: get custom field */

function page_show_meta($alias) {

 global $wp_query; 
 $page = $wp_query->get_queried_object(); 
 $page_id = $page->ID;

 $subtitle = esc_attr( get_post_meta( $page_id, $alias, true ) ); 
 
 echo $subtitle;
 
}

No comments:

Post a Comment