WordPress从function.php加载自定义字段
问题描述:
I\'m new in wordpress. I\'m trying load custom field of post from function.php. Below is code for function post grid layout function where I use custom field:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
$args = array( \'post_type\' => \'post\', \'category_name\' => \'category\', \'posts_per_page\' => -1, \'orderby\' => \'ID\', \'order\' => \'ASC\' ); // The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { $c = 1; $bpr = 5; while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <div class=\"member\"> <div class=\"div-block-image\"> <?php the_post_thumbnail(); ?> </div> <div class=\"div-block-29 w-clearfix\"> <div class=\"text-block-21\"><?php the_title(); ?></div> <div class=\"text-block-22\">subTitle</div> <div class=\"text-block-23\">Text...</div> <a href=\"<?php the_permalink() ?>\" class=\"more w-inline-block\"> <div class=\"text-block-24\">More</div> </a> <p><?php echo get_post_meta($post->ID, \'linkedin\', true); ?></p> // custom-field <p><?php echo get_post_meta($post->ID, \'bio\', true); ?></p> <a href=\"#\" target=\"_blank\" class=\"link-block w-inline-block\"> <div class=\"biotxt\">bio</div> </a> <a href=\"#\" target=\"_blank\" class=\"link-block w-inline-block\"> <div class=\"text-block-20\"></div> </a> </div> </div> <? if( $c == $bpr ) { echo \'<div class=\"clear\"></div>\'; $c = 0; } $c++; endwhile; } else { _e( \'<h2>Oops!</h2>\', \'rys\' ); _e( \'<p>Sorry, seems there are no post at the moment.</p>\', \'rys\' ); } wp_reset_postdata(); |
I want to load this function from template page. All is loading normally except custom field:
1 2 |
<p><?php echo get_post_meta($post->ID, \'linkedin\', true); ?></p> |
If run function code from template page its running normal. Any ideas?
网友观点:
$post->ID
is not correct, as it\'s picking up the ID from the global $post object, which just happens to be the same when you\'re on the template page, but not necessarily when you\'re using it in a function in functions.php. Use get_the_ID()
instead.
###
Within your custom loop, you will want to update those to use: get_the_ID() and not $post->ID.
1 2 |
<p><?php echo get_post_meta(get_the_ID(), \'linkedin\', true); ?></p> |
This will get the ID from the current loop.
Ref: https://developer.wordpress.org/reference/functions/get_the_id/
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » WordPress从function.php加载自定义字段