Support

Account

Home Forums Front-end Issues Field Values Not Showing in Custom Loop

Solving

Field Values Not Showing in Custom Loop

  • Hello!

    I’m currently building a site using loops that query custom post types. I have successfully used the code below to make the loop in this link:

    http://test.nerdfit.com/podcasts

    I tried using the same code (modifying the key values for the custom fields), but for some reason it isn’t pulling all the fields as requested. I tried using var_dump and instead of the actual values, the page returns “string(#)” for some of the fields.

    The faulty link is the following: http://test.nerdfit.com/videos/

    The loop part of the code is almost at the end of the page template where I’m using <?php echo( get_post_meta( get_the_ID(), ‘key’, true))?> to pull the field information for each post in the loop, but for some reason this isn’t working on the Videos page, even though the Podcasts page is working fine.

    I have already checked that the field names are correct. Is there something I’m doing wrong and if so how can I correct it?

    Thank you in advance for any assistance you can provide.

    I’m including the code for the Videos Page Template below.

    Luis

    <?php
    /**
    * Genesis Sample.
    *
    * This file adds the landing page template to the Genesis Sample Theme.
    *
    * Template Name: Videos Grid
    *
    * @package Genesis Sample
    * @author StudioPress
    * @license GPL-2.0+
    * @link http://www.studiopress.com/
    */
    
    // Add landing page body class to the head
    add_filter( 'body_class', 'genesis_sample_add_body_class' );
    function genesis_sample_add_body_class( $classes ) {
    
    $classes[] = 'landing';
    
    return $classes;
    
    }
    
    // Remove Skip Links
    remove_action ( 'genesis_before_header', 'genesis_skip_links', 5 );
    
    // Dequeue Skip Links Script
    add_action( 'wp_enqueue_scripts', 'genesis_sample_dequeue_skip_links' );
    function genesis_sample_dequeue_skip_links() {
    wp_dequeue_script( 'skip-links' );
    }
    
    // Force full width content layout
    add_filter( 'genesis_site_layout', '__genesis_return_full_width_content' );
    
    // Remove site header elements
    // remove_action( 'genesis_header', 'genesis_header_markup_open', 5 );
    // remove_action( 'genesis_header', 'genesis_do_header' );
    // remove_action( 'genesis_header', 'genesis_header_markup_close', 15 );
    
    // Remove navigation
    // remove_theme_support( 'genesis-menus' );
    
    // Remove breadcrumbs
    remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
    
    // Remove footer widgets
    remove_action( 'genesis_before_footer', 'genesis_footer_widget_areas' );
    
    // Remove site footer elements
    remove_action( 'genesis_footer', 'genesis_footer_markup_open', 5 );
    remove_action( 'genesis_footer', 'genesis_do_footer' );
    remove_action( 'genesis_footer', 'genesis_footer_markup_close', 15 );
    
    // Remove page title
    remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
    
    // Remove site inner wrap
    add_filter( 'genesis_structural_wrap-site-inner', '__return_empty_string' );
    
    // Remove edit link
    add_filter ( 'genesis_edit_post_link' , '__return_false' );
    
    // Load landing page styles
    add_action( 'wp_enqueue_scripts', 'landing_do_custom_styles' );
    function landing_do_custom_styles() {
    wp_enqueue_style( 'landing', CHILD_URL . '/landing.css', array(), PARENT_THEME_VERSION );
    }
    
    //======================================================================
    // BEGIN ACF CONTENT
    //======================================================================
    
    // Display content of flexible content layouts
    add_action( 'genesis_entry_content', 'landing_do_acf_content' );
    function landing_do_acf_content() {
    if( have_rows('videos_page_content') ) { ?>
    
    <section class="landing-content">
    
    <?php while ( have_rows('videos_page_content') ) : the_row();
    
    // Hero with text layout
    if( get_row_layout() == 'hero_with_text' ) { ?>
    
    <div class="landing-hero" style="background: url(<?php echo get_sub_field('hero_image'); ?>); background-size: cover;">	
    
    <div class="hero-content">
    <div class="wrap">
    <h1 class="hero-title"><?php echo get_sub_field('headline'); ?></h1>
    <?php echo get_sub_field('text'); ?>
    
    </div>
    </div>
    </div>
    
    <?php } 
    // Headline with WYSIWYG
    else if( get_row_layout() == 'headline_with_wysiwyg' ) { ?>
    
    <div class="heading-text" style="background-color: <?php echo get_sub_field('background_color'); ?>">
    <div class="wrap">
    <h2 class="plain-title"><?php echo get_sub_field('headline'); ?></h2>
    <?php echo get_sub_field('content'); ?>
    </div>
    </div>
    
    <?php }
    
    endwhile; ?>
    
    <div class="wrap">
    
    <?php $args = array( 'post_type' =>'video', 'posts_per_page' => 9 );
    $query = new WP_Query( $args ); ?>
    
    <?php if ( $query->have_posts()) : while ( $query->have_posts() ) : $query->the_post(); ?>
    
    <div class="video-item">
    <div class="video-image">	
    <a href ="<?php echo( get_post_meta( get_the_ID(), 'video_live_url', true))?>" ><?php the_post_thumbnail(); ?></a>
    </div>
    <h2><a href ="<?php echo( get_post_meta( get_the_ID(), 'video_live_url', true))?>" ><?php the_title(); ?></a></h2>
    <h3><?php echo( get_post_meta( get_the_ID(), 'video_date', true))?></h3>
    </div>
    
    <?php endwhile; ?><?else: ?>
    
    <?endif; ?>
    
    </div>
    
    </section> 
    
    <?php }
    
    }
    
    //======================================================================
    // END ACF CONTENT
    //======================================================================
    
    genesis();
  • You are running your code inside of a function. Are your sure that get_the_ID() is returning the correct value in this context?

    
    <?php if ( $query->have_posts()) : while ( $query->have_posts() ) : $query->the_post(); ?>
    
    <div class="video-item">
    <div class="video-image">	
    <?php 
      // see what get_the_ID() is returning
      // make sure it's the same as the image
      echo get_the_ID();
    ?>
    <a href ="<?php echo( get_post_meta( get_the_ID(), 'video_live_url', true))?>" ><?php the_post_thumbnail(); ?></a>
    

    also, you may need to add this line inside of your function, but I’m not sure, it’s just a guess.

    
    global $post
    
  • Thanks so much for your help! In the end I was able to do it by calling the default date and permalinks instead of adding a separate custom field for those.

    The result can be seen at http://nerdfit.com/videos

    The only problem I have now is that I can’t get pagination going with this custom post type archive. Is there any way I can show the “older entries” and “newer entries” links at the bottom of the loop?

    This is my code so far:

    <div class="wrap">
    
    	<?php 
    
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;		
    
    	$args = array( 
    		'post_type' => 'video',
    		'paged' => $paged,
    		'posts_per_page' => 9);
    
    	$query = new WP_Query( $args ); ?>
    
    	<?php if ( $query->have_posts()) : while ( $query->have_posts() ) : $query->the_post(); ?>
    
    			<div class="video-item">
    				<div class="video-image">	
    						<a href="<?php echo get_permalink(); ?>" ><?php the_post_thumbnail(); ?></a>
    
    				</div>
    			<h2><a href ="<?php echo get_permalink(); ?>" ><?php the_title(); ?></a></h2>
    			<h3><?php echo get_the_date(); ?></h3>
    
    			</div>
    		
    	<?php endwhile; ?>
    
    <?php
    
    // get_next_posts_link() usage with max_num_pages
    echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages );
    echo get_previous_posts_link( 'Newer Entries' );
    ?>
    
    <?php 
    // clean up after the query and pagination
    wp_reset_postdata(); 
    ?>	
    
    <?php else: ?>
    
    	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    
    	<?php endif; ?>
    
    </div>
    
    </section>
  • Hi! I tried adding the navigation links using pagenavi and found something interesting: even though I have enough posts to create two archive pages, WordPress is only pulling the first. Theoretically I could have more than the 9 posts per page in the query, but WP would still not show the pagination for the pages after the first.

    How could I fix this?

  • Most page navigation function only work with the main query and not with sub queries.

    You’ve either go to look at the query results and build this navigation yourself or you need to hack the global $wp_query https://wordpress.stackexchange.com/questions/77661/the-next-posts-link-works-only-with-original-wp-query

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.