0

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 as off-topic by Krzysiek Dróżdż, Jacob Peattie, rudtek, Dave Romsey, Rick Hellewell Feb 19 at 2:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 Feb 4 at 21:36
  • What is $prefix set to? – czerspalace Feb 4 at 21:54
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 );
  • 1
    Thanks so much. It's not ACF, but that prefix was defined elsewhere on the page, just nowhere near it. Can't believe I didn't figure that out. I knew it would be something easy. Thanks a lot. – Dan Feb 5 at 21:14

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