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();
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>';
}
?>
1
  • This has worked perfectly. The only thing I'd like to add for other complete Wordpress beginners is that this snipper of code should go within 'have_posts()' loop, after 'the_post();'. At least in the example I gave in the question above. Also not sure how to format code in comments, sorry. – James Jan 9 '20 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.