doujia1988 2016-07-15 19:58
浏览 34


I'm trying to format my single post page template that has custom types. I'm using the_content() which displays the post contents altogether, but how can I call individual content fields so that I can arrange them in the template?


function stories_init() {
$args = array(
  'label' => 'Stories',
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => array('slug' => 'stories'),
    'query_var' => true,
    'menu_icon' => 'dashicons-video-alt',
    'supports' => array(


register_post_type( 'stories', $args );


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>

<?php endwhile;  ?>
  • 写回答

2条回答 默认 最新

  • dongruo0909 2016-07-15 21:11

    You would need to create custom fields to be used in posts if you wanted to separate out parts of your content, like each paragraph individually or something. the_content() will always print out the entire content.

    If you want a plugin to do it, this is the most popular

    Or, when creating a new post, you can go to the top and click Screen Options then check the Custom Fields box.

    Custom fields will appear under your post content box. You can create a new one and give it a value (They act as key/value pairs).

    to call it in your template, use <?php echo get_post_meta($post_id, $key, $single); ?>

    $post_id -> is the ID of the post you want the meta values for. Use $post->ID to get a post's ID within the $post variable scope. Use get_the_ID() to retrieve the ID of the current item in the WordPress Loop.

    $key -> is a string containing the name of the meta value you want.

    $single can either be true or false. If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 关于vmprotect3.8.4虚拟文件一项
  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用