简体   繁体   中英

WordPress : printing custom fields content in pages

I'm working with custom post types. post type is created using types plugin. Custom post type name is partners that has title , featured image and a custom field description this is how i am able to fetch image and title

<?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;?>

now how do i print that custom field content after title ?please help

Please, pay attention to the documentation of "Types" plugin:

...Types custom fields use the standard WordPress post-meta table, making it cross-compatible with any theme or plugin....

So, it is possible to get the values of custom fields using the "get_post_meta" function:

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

If you know the name of custom field in the database, for example: description, it is possible to get its value into the loop, with the snippet of code:

get_post_meta ( get_the_ID(), 'description' )

Including the previous code in yours:

<?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') );?> <?php print get_post_meta ( get_the_ID(), 'description' ); ?> </div> <?php endwhile;?>

and that's all.

Best regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM