Support

Account

Home Forums General Issues I'm looking to pass data from a field into an existing wp loop Reply To: I'm looking to pass data from a field into an existing wp loop

  • Hi @james-reed

    If you want to pass the category ID from the frontend, you can use a form and then get it using the $_GET method. This page should give you more idea about it: http://www.w3schools.com/php/php_forms.asp.

    Also, please use get_field() instead of the_field() to set the $news variable.

    So, your code should be like this:

    <?php
    $news = get_field('news_box');
    $category_id = $_GET["category"];
    ?>
    		<section  class="category-news m-all t-1of2 d-1of2">
    	<h2> <a href="<?php echo get_category_link($category_id); ?>">News</a> </h2>
    			<?php //News Posts
    			$newsPosts = new WP_Query('cat='. $category_id .'&posts_per_page=1');
    			if ($newsPosts->have_posts()) : 
    				while($newsPosts->have_posts()) : $newsPosts->the_post(); ?>
    			<p class="byline entry-meta vcard">
    
                        <?php printf( __( 'Posted', 'bonestheme' ).' %1$s',
                           /* the time the post was published */
                           '<time class="updated entry-time" datetime="' . get_the_time('Y-m-d') . '" itemprop="datePublished">' . get_the_time(get_option('date_format')) . '</time>'
                        ); ?>
    
                      </p>
    		<h3><a href="<?php echo get_post_permalink(); ?>" ><?php the_title(); ?></a></h3>
     		<?php the_excerpt(); ?> 
    		<?php endwhile; else : endif; wp_reset_postdata(); ?>
    		</section>

    I hope this helps.