【问题标题】:How to arrange the_content() in Wordpress post page?如何在 Wordpress 帖子页面中安排 the_content()?
【发布时间】:2016-07-15 19:58:00
【问题描述】:

我正在尝试格式化具有自定义类型的单个帖子页面模板。我正在使用完全显示帖子内容的 the_content(),但是如何调用各个内容字段以便我可以将它们排列在模板中?

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

【问题讨论】:

  • 您想与什么交流如何调用各个内容字段以便我可以在模板中排列它们? 顺便说一句;您可以创建一个名为:. “single-stories.php”并在该文件中进行所有自定义...
  • 我想访问不同 div 中的内容。例如,我希望标题出现在与编辑器中的文本不同的列中。所以我试图只返回自定义字段。

标签: php wordpress custom-wordpress-pages


【解决方案1】:

如果您想分离部分内容,例如单独的每个段落或其他内容,则需要创建要在帖子中使用的自定义字段。 the_content() 将始终打印出全部内容。

如果你想要一个插件来做,这是最受欢迎的 https://wordpress.org/plugins/advanced-custom-fields/

或者,在创建新帖子时,您可以到顶部点击Screen Options,然后检查Custom Fields box

自定义字段将显示在您的帖子内容框下方。您可以创建一个新的并给它一个值(它们充当键/值对)。

要在模板中调用它,请使用&lt;?php echo get_post_meta($post_id, $key, $single); ?&gt;

$post_id -> 是您想要元值的帖子的 ID。使用$post-&gt;ID 获取$post 变量范围内的帖子ID。使用 get_the_ID() 检索 WordPress 循环中当前项目的 ID。

$key -> 是一个包含你想要的元值名称的字符串。

$single 可以是truefalse。如果设置为 true,则函数将返回单个结果,作为字符串。如果为 false 或未设置,则该函数返回自定义字段的数组。

【讨论】:

  • 谢谢!这就是我一直在寻找的。有没有办法删除自定义字段?
  • 如果您只是想删除特定帖子,您可以在 wordpress 管理员中创建帖子的屏幕上执行此操作。如果您想删除所有帖子中的自定义字段,最好进入 phpmyadmin 并执行 SQL 查询以一次性将其从所有内容中删除。也许有一种更简单的方法可以做到这一点,但我不太确定。
【解决方案2】:

由于您的自定义帖子的名称是stories,因此您最好在主题目录中创建一个名称为:single-stories.php 的专用文件。在该文件中,您可以执行以下操作:

<?php 
    // FILE-NAME: single-stories.php

    if ( have_posts() ) : 
        while ( have_posts() ) : the_post(); $id= get_the_ID(); // IN CASE YOU NEED IT ?>
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>
            <span class="entry-author"><?php echo get_the_author(); ?></span>
            <span class="entry-date"><?php echo get_the_date(); ?></span>
            <!-- YOU CAN DO AS YOU WISH IN THIS FILE -->
            <!-- AND THE RENDERING HERE WILL BE UNIQUE TO THE CUSTOM STORIES POSTS -->
            <!-- HOWEVER, BE INFORMED THAT THIS IS FOR DETAILED (SINGLE-POST) VIEW ONLY -->
        <?php endwhile;  ?>
    <?php endif;  ?>

【讨论】:

  • 感谢您的回答。我想我的问题措辞很糟糕,但 Nate 能够破译我的意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
  • 2012-02-01
  • 1970-01-01
相关资源
最近更新 更多