第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在短代碼中顯示自定義字段(變量)

如何在短代碼中顯示自定義字段(變量)

PHP
一只萌萌小番薯 2022-01-02 15:35:09
我想讓一個(gè)簡(jiǎn)碼顯示一個(gè)值,它是 Wordpress 中的自定義字段。在這種情況下,變量“prijs”。我已經(jīng)在網(wǎng)上嘗試了很多解決方案,但到目前為止還沒(méi)有運(yùn)氣。誰(shuí)能幫我 ?為什么這個(gè)腳本不顯示任何東西?如何顯示自定義字段“prijs”?<?phpfunction showdetails_shortcode( $attr, $content = null ) {    return <?php $key="prijs"; echo get_post_meta($post->ID, $key, true); ?>}add_shortcode('showdetails', 'showdetails_shortcode');?>
查看完整描述

1 回答

?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

為什么這個(gè)腳本不顯示任何內(nèi)容?


提供的代碼顯示了幾個(gè)語(yǔ)法錯(cuò)誤,其中最關(guān)鍵的是<?php在 PHP 語(yǔ)句中重新調(diào)用。


如果prijs是放置此短代碼的帖子的良好自定義字段鍵,則它應(yīng)該可以工作。


function showdetails_shortcode( ) {


   $post_id = get_the_ID();


//either output the value or let us know the code is working but the value has not been found

   $output = get_post_meta( $post_id, 'prijs', true) ? get_post_meta( $post_id, 'prijs', true) : 'NO CUSTOM VALUE FOUND' ;


   return $output;


}


add_shortcode('showdetails', 'showdetails_shortcode');

回應(yīng)評(píng)論,這是一個(gè)帶有兩個(gè)字段和表格輸出的版本,請(qǐng)記住,會(huì)有更清晰(更靈活和簡(jiǎn)潔)的方法來(lái)導(dǎo)出變量并為更多字段生成輸出。


function showdetails_shortcode( ) {


   $post_id = get_the_ID();


   //extract the field values

   $field1 = get_post_meta( $post_id, 'prijs', true) ? get_post_meta( $post_id, 'prijs', true) : 'PRIJS NOT FOUND';

   $field2 = get_post_meta( $post_id, 'prijs2', true) ? get_post_meta( $post_id, 'prijs2', true) : 'PRIJS2 NOT FOUND';


   //prepare html table output

   $output = '<table><tbody>'; 

   $output .= '<tr><td>' . $field1 . '</td></tr>';

   $output .= '<tr><td>' . $field2 . '</td></tr>'; 

   $output .= '</tbody></table>';


   //return the html    

   return $output;


}


add_shortcode('showdetails', 'showdetails_shortcode');


查看完整回答
反對(duì) 回復(fù) 2022-01-02
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)