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載入自定義欄位