Displaying Custom Fields from Custom Post Types

admin2025-06-03  3

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I've been looking everywhere and still don't understand how to display the information from some of the Custom Post Types on my site (took over the site for a new client).

The previous developer had set up some Custom Fields such as thumb_title and resource_url below:

         'fields'     => array(
            // Thumbnail Title
            array(
                'name'    => esc_html__( 'Thumbnail Title', 'textdomain' ),
                'id'      => "{$prefix}thumb_title",
                'type'    => 'text',
                'desc'    => 'i.e. Project Management',
            ),
             // Resource URL
            array(
                'name'    => esc_html__( 'Link to Resource', 'textdomain' ),
                'id'      => "{$prefix}resource_url",
                'type'    => 'text',
                'desc'    => 'Enter the resource URL, i.e. <b>/resource/project-management</b>',
            ),
        ),

I can display the usual the_title and the_content just fine, it's these other fields that I just can't display and it's driving me insane.

Here is what I have for the page:

<?php
    $args = array( 'post_type' => 'resources_cta', 'posts_per_page' => 10 );
    $the_query = new WP_Query( $args );
    $thumb_title = get_post_meta( get_the_ID(), 'thumb_title', true );
?>

<?php if ( $the_query->have_posts() ) : ?>

    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php 
        // Read somewhere that this needed to be in the loop so also tried it here
        //$meta = get_post_meta( $post->ID, 'thumb_title', true ); 
    ?>

    <div class="carousel-item <?php if( $the_query->current_post == 0 ):?>active<?php endif; ?>">

        <div class="resourcesLeft">
            <h1><?php the_title(); ?></h1>
            <div class="entry-content">
                <?php the_content(); ?>
            </div>
        </div>
        <div class="resourcesRight">
            <div class="little-book">the little book of</div>
            <div class="thumb-title"><?php echo $meta; ?></div> <?php //print_r($key); die(); ?>
            <img src="<?php bloginfo('template_directory'); ?>/library/images/logo-full_white.svg" alt="" class="logo">
            <a href="<?php the_field('resource_url'); ?>" class="resource-cta-link">FIND OUT MORE</a>
        </div>

    </div>

<?php endwhile; endif; ?>

I need to sort this out in the next couple of days, so any help would be very grateful.

Thanks in advance.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I've been looking everywhere and still don't understand how to display the information from some of the Custom Post Types on my site (took over the site for a new client).

The previous developer had set up some Custom Fields such as thumb_title and resource_url below:

         'fields'     => array(
            // Thumbnail Title
            array(
                'name'    => esc_html__( 'Thumbnail Title', 'textdomain' ),
                'id'      => "{$prefix}thumb_title",
                'type'    => 'text',
                'desc'    => 'i.e. Project Management',
            ),
             // Resource URL
            array(
                'name'    => esc_html__( 'Link to Resource', 'textdomain' ),
                'id'      => "{$prefix}resource_url",
                'type'    => 'text',
                'desc'    => 'Enter the resource URL, i.e. <b>/resource/project-management</b>',
            ),
        ),

I can display the usual the_title and the_content just fine, it's these other fields that I just can't display and it's driving me insane.

Here is what I have for the page:

<?php
    $args = array( 'post_type' => 'resources_cta', 'posts_per_page' => 10 );
    $the_query = new WP_Query( $args );
    $thumb_title = get_post_meta( get_the_ID(), 'thumb_title', true );
?>

<?php if ( $the_query->have_posts() ) : ?>

    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php 
        // Read somewhere that this needed to be in the loop so also tried it here
        //$meta = get_post_meta( $post->ID, 'thumb_title', true ); 
    ?>

    <div class="carousel-item <?php if( $the_query->current_post == 0 ):?>active<?php endif; ?>">

        <div class="resourcesLeft">
            <h1><?php the_title(); ?></h1>
            <div class="entry-content">
                <?php the_content(); ?>
            </div>
        </div>
        <div class="resourcesRight">
            <div class="little-book">the little book of</div>
            <div class="thumb-title"><?php echo $meta; ?></div> <?php //print_r($key); die(); ?>
            <img src="<?php bloginfo('template_directory'); ?>/library/images/logo-full_white.svg" alt="" class="logo">
            <a href="<?php the_field('resource_url'); ?>" class="resource-cta-link">FIND OUT MORE</a>
        </div>

    </div>

<?php endwhile; endif; ?>

I need to sort this out in the next couple of days, so any help would be very grateful.

Thanks in advance.

Share Improve this question asked Feb 4, 2019 at 21:13 DanDan 272 bronze badges 2
  • 1 It's a little hard to tell from your code - is Advanced Custom Fields responsible for creating the fields, or is it pure custom code? the_field() is specific to ACF, not core fields, so if you're not using ACF then you need to use something like echo get_post_meta() instead. – WebElaine Commented Feb 4, 2019 at 21:36
  • What is $prefix set to? – czerspalace Commented Feb 4, 2019 at 21:54
Add a comment  | 

1 Answer 1

Reset to default 0

It looks like you want to get the value from ACF? If so, you can use get_field:

$thumb_title = get_field( 'thumb_title', get_the_ID();

But wait: that won't work because of this:

'id'      => "{$prefix}thumb_title",

The {$prefix} part is defined somewhere in that file, and it's creating a full field name. You'll need to include that in your calls to get_field:

// In your file adding fields somewhere:
$prefux = 'myprefix_';

// In your template:
$thumb_title = get_field( 'myprefix_thumb_title', get_the_ID() );

It might be helpful to show us the line where $prefix gets defined in your first block. Also, if these are just regular WordPress meta fields, you would just need to do something like this instead:

$thumb_title = get_post_meta( get_the_ID(), 'myprefix_thumb_title', true );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748943073a315028.html

最新回复(0)