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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号