WordPress:在页面中打印自定义字段内容

时间:2015-10-24 20:44:58

标签: php wordpress custom-post-type

我正在使用自定义帖子类型。使用类型插件创建帖子类型。自定义帖子类型名称为partners,其中包含标题,精选图​​片和自定义字段说明,这是我能够获取图片和标题的方式

<?php
       $args=array('post_type' => 'partners');
       $query= new WP_Query($args);                               
       while ($query-> have_posts() ) : $query->the_post()?>
       <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12">
       <?php the_title;?>
       <?php the_post_thumbnail( 'full', array( 'class' => 'innerimages') 
 );?>
        </div>
 <?php endwhile;?>

现在如何在标题后打印自定义字段内容?请帮助

1 个答案:

答案 0 :(得分:1)

请注意“类型”插件的文档:

...类型自定义字段使用标准的WordPress后元表,使其与任何主题或插件交叉兼容....

因此,可以使用“get_post_meta”函数获取自定义字段的值:

#include <EtherCard.h> static byte mymac[] = {0x65,0x77,0x33,0x2D,0x30,0x66}; static byte myip[] = {192,168,0,99}; byte Ethernet::buffer[500]; BufferFiller bfill; static word homePage() { bfill = ether.tcpOffset(); bfill.emit_p( PSTR ( "HTTP/1.0 503 test page\r\n" "Content-Type: text/html\r\n" "Retry-After: 600\r\n" "\r\n" "<html>" "<head><title>" "Arduino test page" "</title></head>" "<body>" "<h3>Test</h3>" "<p>Test</p>" "</body>" "</html>" )) ; return bfill.position(); } void setup() { // put your setup code here, to run once: Serial.begin(57600); Serial.println("TEST"); Serial.println(); Serial.print("Status: "); if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) { Serial.println( "Failed to access Ethernet controller"); } else { Serial.println( "Ethernet controller OK!"); ether.staticSetup(myip); ether.dhcpSetup(); } Serial.println(); ether.printIp("My IP: ", ether.myip); ether.printIp("GW IP: ", ether.gwip); ether.printIp("DNS IP: ", ether.dnsip); } void loop() { // put your main code here, to run repeatedly: word pos = ether.packetLoop(ether.packetReceive()); if (pos){ for (int i=pos;Ethernet::buffer[i]; i++) { Serial.print((char)Ethernet::buffer[i]); Serial.println(); ether.httpServerReply(homePage()); } } }

如果您知道数据库中自定义字段的名称,例如:description,则可以使用代码片段将其值放入循环中:

get_post_meta ( int $post_id, string $key = '', bool $single = false )

在您的代码中包含以前的代码:

get_post_meta ( get_the_ID(), 'description' )

就是这样。

最好的问候。