wp query - Display Custom Post Type Fields

I'm making a website and want to display Events. Therefore I made a Custom Post Type called 'event'.I

I'm making a website and want to display Events. Therefore I made a Custom Post Type called 'event'.

I'm querying events to show them in a page. The Events I made also have some custom fields like : 'Location', 'Day', 'Month', 'Year' and a category.

This is my code now :

    <?php
        $args = array(
          'post_type' => 'event', 
        );

        $events = new WP_Query( $args );

        if( $events->have_posts() ) {
          while( $events->have_posts() ) {
            $events->the_post();

            ?>
              <div class='event'>
                  <div class="event-date">

                  </div>
                  <div class="event-content">
                        <div class="event-title"><?php the_title() ?></div>
                        <div class="event-info">
                            <span>Location: </span>
                            <span>Category: </span>
                        </div>
                  </div>
              </div>
            <?php
          }
        } else {
          echo 'No events!';
        }
      ?>

Which only gives me the title. How can I display the custom fields and the category ?

I am fairly new to WordPress development, used to programming in .NET.

Thx for any help!

I'm making a website and want to display Events. Therefore I made a Custom Post Type called 'event'.

I'm querying events to show them in a page. The Events I made also have some custom fields like : 'Location', 'Day', 'Month', 'Year' and a category.

This is my code now :

    <?php
        $args = array(
          'post_type' => 'event', 
        );

        $events = new WP_Query( $args );

        if( $events->have_posts() ) {
          while( $events->have_posts() ) {
            $events->the_post();

            ?>
              <div class='event'>
                  <div class="event-date">

                  </div>
                  <div class="event-content">
                        <div class="event-title"><?php the_title() ?></div>
                        <div class="event-info">
                            <span>Location: </span>
                            <span>Category: </span>
                        </div>
                  </div>
              </div>
            <?php
          }
        } else {
          echo 'No events!';
        }
      ?>

Which only gives me the title. How can I display the custom fields and the category ?

I am fairly new to WordPress development, used to programming in .NET.

Thx for any help!

Share Improve this question asked Dec 22, 2016 at 9:55 Nanou PonetteNanou Ponette 1551 gold badge1 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 8

Custom fields are saved in the post_meta table. In your query you got the post title and post ID, so now you have to get post meta.

Use:

<?php
get_post_meta( get_the_ID(), '_location', true );
?>

same for the rest of your custom fields only '_location' will change according to field you are getting. Read more about post meta here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745001545a4605524.html

相关推荐

  • wp query - Display Custom Post Type Fields

    I'm making a website and want to display Events. Therefore I made a Custom Post Type called 'event'.I

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信