0

I am currently working on a news website and using the Wordpress archive feature to display all my posts in a feed (when you click on the post titles you are taken to the source website).I need to add another piece of metadata to these posts (the source of the news article).enter image description here.

This is what it currently looks like, I need the source inline with the date and author.

I have used the advanced custom fields plugin to create a custom meta box for each post in which I can input the source of the article. This seems to be working fine, but I can not figure out how to display this on the archive page. enter image description here

This is the code I added to the loop-archive.php because that's where I want the custom field to display. But nothing happened. I got this code from many different websites which make it seem easy to display your custom metadata. (did this all on a child theme, the theme I'm using is Newspaper)

How do I fix this?

archive.php:

<?php get_header();

    $td_archive_title = '';
    if (is_day()) {
        $td_archive_title .= __('Daily Archives:', 'newspaper' ) . ' ' . get_the_date();
    } elseif (is_month()) {
        $td_archive_title .= __('Monthly Archives:', 'newspaper') . ' ' . get_the_date('F Y');
    } elseif (is_year()) {
        $td_archive_title .= __('Yearly Archives:', 'newspaper') . ' ' . get_the_date('Y');
    } else {
        $td_archive_title .= __('Archives', 'newspaper');
    }

?>

    <div class="td-main-content-wrap td-container-wrap">
        <div class="td-container">
            <div class="td-crumb-container">
                <?php echo tagdiv_page_generator::get_breadcrumbs(array(
                    'template' => 'archive',
                )); ?>
            </div>

            <div class="td-pb-row">
                <div class="td-pb-span8 td-main-content">
                    <div class="td-ss-main-content">
                        <div class="td-page-header">
                            <h1 class="entry-title td-page-title">
                                <span><?php echo esc_html( $td_archive_title ) ?></span>
                            </h1>

                        </div>

                        <?php
                            get_template_part('loop-archive');
                        ?>
                    </div>
                </div>

                <div class="td-pb-span4 td-main-sidebar">
                    <div class="td-ss-main-sidebar">
                        <?php dynamic_sidebar( 'td-default' ) ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

<?php get_footer(); ?>

archive loop:

<?php

$post_count = 1;
$column_count = 1;

$span = 'span6';
$column_break = 2;
$is_404 = false;
if( is_404() ) {
    $is_404 = true;
    $column_break = 3;
    $span = 'span4';

    $args = array(
        'post_type'=> 'post',
        'showposts' => 6,
        'ignore_sticky_posts' => true
    );
    query_posts($args);
}

if (have_posts()) {

    while ( have_posts() ) : the_post();
        if( $column_count == 1 ) { ?>
            <div class="td-block-row">
        <?php } ?>

            <div class="td-block-<?php echo esc_attr( $span ) ?>">
                <div <?php post_class('td_module_1 td_module_wrap clearfix') ?> >
                    <div class="td-module-image">
                        <div class="td-module-thumb">
                            <?php
                                if ( current_user_can('edit_published_posts') ) {
                                    edit_post_link('Edit', '', '', '', 'td-admin-edit');
                                }
                            ?>

                            <a href="<?php the_permalink() ?>" rel="bookmark" class="td-image-wrap" title="<?php the_title_attribute() ?>">
                                <?php
                                    $post_thumbnail_url = '';

                                    if( get_the_post_thumbnail_url(null, 'medium_large') != false ) {
                                        $post_thumbnail_url = get_the_post_thumbnail_url(null, 'medium_large');
                                    } else {
                                        $post_thumbnail_url = get_template_directory_uri() . '/images/no-thumb/medium_large.png';
                                    }
                                ?>

                                <img class="entry-thumb" src="<?php echo esc_url($post_thumbnail_url) ?>" alt="<?php the_title() ?>" title="<?php echo esc_attr(strip_tags(the_title())) ?>" />
                            </a>
                        </div>

                        <?php
                            $categories = get_the_category();
                            if( !empty( $categories ) ) {
                                $cat_link = get_category_link($categories[0]->cat_ID);
                                $cat_name = $categories[0]->name; ?>

                                <a class="td-post-category" href="<?php echo esc_url($cat_link) ?>"><?php echo esc_html($cat_name) ?></a>
                        <?php } ?>
                    </div>
<?php 
 
                        $source = get_post_meta($post->ID, 'source', true);
 
                        if ($source) { ?>
 
                        <p>Source: <? echo $source; ?></p>
 
                        <?php 
 
                        } else { 
                        // do nothing; 
                        }
 
                        ?>
                    <h3 class="entry-title td-module-title">
                        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute() ?>">
                            <?php the_title() ?>
                        </a>
                    </h3>

                    <div class="td-module-meta-info">
                        <div class="td-post-author-name">
                            <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta( 'ID' ))) ?>"><?php the_author() ?></a>
                            <span> - </span>
                        </div>
                    
                        <span class="td-post-date">
                            <time class="entry-date updated td-module-date" datetime="<?php echo esc_html(date(DATE_W3C, get_the_time('U'))) ?>" ><?php the_time(get_option('date_format')) ?></time>
                        </span>

                        <div class="td-module-comments">
                            <a href="<?php comments_link() ?>">
                                <?php comments_number('0','1','%') ?>
                            </a>
                        </div>
                    </div>
                </div>
            </div>

    <?php
        if( $column_count == $column_break || $post_count == $wp_query->post_count ) { ?>
            </div> <?php
            $column_count = 1;
        } else {
            $column_count++;
        }

        $post_count++;

    endwhile;

    if( !$is_404 ) {
        tagdiv_page_generator::get_pagination();
    }

} else { ?>
    <div class="no-results td-pb-padding-side">
        <h2><?php esc_html_e('No posts to display', 'newspaper') ?></h2>
    </div>
<?php }
3
  • Did you check the $post->ID is getting correct in loop-archive.php? Please print_r $post->ID and check your post ID is perfect or not. I think you need to use the get_the_ID() function rather than the $post->ID.
    – ZealousWeb
    Commented Apr 23, 2021 at 6:35
  • can you post the full code of your loop-archive.php and archive.php?
    – MMK
    Commented Apr 23, 2021 at 10:36
  • @MMK I added the rest of the code Commented Apr 23, 2021 at 17:22

1 Answer 1

0

try this (assuming your custom acf field is of type text),

$source = get_field('source', get_the_id());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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