doujia1988 2016-07-15 19:58
浏览 34
已采纳

如何在Wordpress帖子页面中安排the_content()?

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?

functions.php

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(
        'title',
        'editor',
        'custom-fields',

        'thumbnail',)



    );
register_post_type( 'stories', $args );
}

single-post.html

<?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 https://wordpress.org/plugins/advanced-custom-fields/

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥80 LS dyna mpp并行报错
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入
  • ¥15 NodeBB论坛配置Apache Solr中文搜索引擎的详细教程