最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to display custom field on homepage

programmeradmin0浏览0评论

I am new to custom fields thing. I am working on a movie review site. And wants to add movie story or plot field, I have successfully added the custom field named Story and its content is displaying on single.php. However, can I display same on index.php? Below is my code.

<?php 
$meta = get_post_meta( get_the_ID(), 'Story' );
if( !empty($meta) ) {
    echo $meta[0];
}?>

I am new to custom fields thing. I am working on a movie review site. And wants to add movie story or plot field, I have successfully added the custom field named Story and its content is displaying on single.php. However, can I display same on index.php? Below is my code.

<?php 
$meta = get_post_meta( get_the_ID(), 'Story' );
if( !empty($meta) ) {
    echo $meta[0];
}?>
Share Improve this question edited Sep 19, 2018 at 21:16 Varsha Dhadge 3411 gold badge2 silver badges14 bronze badges asked Oct 20, 2015 at 11:25 user235377user235377 412 silver badges4 bronze badges 2
  • Please describe more "Story" is right name of meta. and whats u store in this field. this one array value stored? – Ravi Patel Commented Oct 20, 2015 at 11:56
  • Yes "Story" is my field name and yes array value stored. – user235377 Commented Oct 20, 2015 at 18:03
Add a comment  | 

2 Answers 2

Reset to default 1

Try this:

if ( ! function_exists( 'customField' ) ) :
function customField($name, $id=false, $single=false){
    global $post_type, $post;

    $name=trim($name);
    $prefix=''; // only if you need this
    $data=NULL;

    if($id!==false && !empty($id) && $id > 0)
        $getMeta=get_post_meta((int)$id, $prefix.$name, $single);
    else if(isset($post->ID) && $post->ID > 0)
        $getMeta=get_post_meta($post->ID,$prefix.$name, $single);
    else if('page' == get_option( 'show_on_front' ))
        $getMeta=get_post_meta(get_option( 'page_for_posts' ),$prefix.$name, $single);
    else if(is_home() || is_front_page() || get_queried_object_id() > 0)
        $getMeta=get_post_meta(get_queried_object_id(),$prefix.$name, $single);
    else
        $getMeta=get_post_meta(get_the_id(),$prefix.$name, $single);

    if(isset($getMeta[0])) $data=$getMeta[0];

    if($data===false || !is_numeric($data) && (empty($data) || is_null($data))) return NULL;

    $return = preg_replace(array('%<p>(<img .*?/>)</p>%i','%<p>&nbsp;</p>%i','/^\s*(?:<br\s*\/?>\s*)*/i'), array('$1','',''),$data);

    return (!empty($return)?$return:NULL);
}
endif;


echo customField("story"); // Default
echo customField("story", 55); // With custom page ID

This code work great becouse I build this for my works. If return empty, in place where you setup pharameters, you not save your data. Also be careful about prefix of your metabox if you use it.

Add this code on content.php because of index.php content comes from this file.

if u call outside of post use 
global $post;  
$meta = get_post_meta($post->ID , 'Story', true);

OR inner loop

$meta = get_post_meta(get_the_id(), 'Story', true);

if( !empty($meta) ) {
    echo $meta[0];
}
发布评论

评论列表(0)

  1. 暂无评论