最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Displaying custom fields for custom post type generate with Custom Post Type UI Plugin

programmeradmin3浏览0评论

First, I am using default theme twentyseventeen. I've used plugin Custom Post Type UI to create a new post type called film_kernal

For that post type I have created some custom fields, one of which is called Quality Score

I followed a tutorial that had me display this as an archive which is working. The custom posts I have created a displayed on an archive page in a list however they do not display my custom field either on this archive page, or when I actually click on the post and visit an individual page.

I would like to display a custom field on my archive page, I have created a new child theme layout called archive-film_kernal.php however I am unsure how to modify the code to add in the field 'Quality Score'.

The template code is a copy of the original archive.php and is below. From reading I think I need to use get_post_meta() but I'm not sure how to use this properly, or how to select only one, or several of my custom fields rather than all.

Thanks in advance.

<?php /*Template Name: Custom Archive Layout Test */?>

<?php


get_header(); ?>

<div class="wrap">

    <?php if ( have_posts() ) : ?>
        <header class="page-header">
            <?php
                the_archive_title( '<h1 class="page-title">', '</h1>' );
                the_archive_description( '<div class="taxonomy-description">', '</div>' );
            ?>
        </header><!-- .page-header -->
    <?php endif; ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

        <?php
        if ( have_posts() ) :
            ?>
            <?php
            /* Start the Loop */
            while ( have_posts() ) :
                the_post();



                /*
                 * Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */

                ]
                get_template_part( 'template-parts/post/content', get_post_format() );

            endwhile;

            the_posts_pagination(
                array(
                    'prev_text'          => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
                    'next_text'          => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
                    'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
                )
            );

        else :

            get_template_part( 'template-parts/post/content', 'none' );

        endif;
        ?>

        </main><!-- #main -->
    </div><!-- #primary -->
    <?php get_sidebar(); ?>
</div><!-- .wrap -->

<?php
get_footer();

First, I am using default theme twentyseventeen. I've used plugin Custom Post Type UI to create a new post type called film_kernal

For that post type I have created some custom fields, one of which is called Quality Score

I followed a tutorial that had me display this as an archive which is working. The custom posts I have created a displayed on an archive page in a list however they do not display my custom field either on this archive page, or when I actually click on the post and visit an individual page.

I would like to display a custom field on my archive page, I have created a new child theme layout called archive-film_kernal.php however I am unsure how to modify the code to add in the field 'Quality Score'.

The template code is a copy of the original archive.php and is below. From reading I think I need to use get_post_meta() but I'm not sure how to use this properly, or how to select only one, or several of my custom fields rather than all.

Thanks in advance.

<?php /*Template Name: Custom Archive Layout Test */?>

<?php


get_header(); ?>

<div class="wrap">

    <?php if ( have_posts() ) : ?>
        <header class="page-header">
            <?php
                the_archive_title( '<h1 class="page-title">', '</h1>' );
                the_archive_description( '<div class="taxonomy-description">', '</div>' );
            ?>
        </header><!-- .page-header -->
    <?php endif; ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

        <?php
        if ( have_posts() ) :
            ?>
            <?php
            /* Start the Loop */
            while ( have_posts() ) :
                the_post();



                /*
                 * Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */

                ]
                get_template_part( 'template-parts/post/content', get_post_format() );

            endwhile;

            the_posts_pagination(
                array(
                    'prev_text'          => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
                    'next_text'          => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
                    'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
                )
            );

        else :

            get_template_part( 'template-parts/post/content', 'none' );

        endif;
        ?>

        </main><!-- #main -->
    </div><!-- #primary -->
    <?php get_sidebar(); ?>
</div><!-- .wrap -->

<?php
get_footer();
Share Improve this question asked Jan 7, 2020 at 17:50 JamesJames 1191 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It depends on how you added the custom fields. If you used a custom fields plugin, you may be able to call a plugin-specific function. For example, with ACF, you can call get_field('fieldname') to retrieve one specific custom field.

If you're not using ACF (or even if you are), as long as your fields are stored as postmeta, you can do something like

<?php
if(get_post_meta($post->ID, 'quality_score', true)) {
    echo get_post_meta($post->ID, 'quality_score', true);
}
?>

or format more heavily, i.e.

<?php
if(get_post_meta($post->ID, 'quality_score', true)) {
    $quality_score = get_post_meta($post->ID, 'quality_score', true);
    echo '<div class="quality">' . $quality_score . '</div>';
}
?>
发布评论

评论列表(0)

  1. 暂无评论