Home › Forums › General Issues › I'm looking to pass data from a field into an existing wp loop
I am working on a local dev environment so I can’t share a link, but I can however copy/paste my snippet of code I’m working on.
My goal is to be able to have the user from the frontpage page enter a category ID in a text field and then have that data pass that into the appropriate places in the template file. There are couple different places that data needs to be passed into, and I can’t seem to figure out how to pass that field data into my existing code.
If someone can shine some light on this, it would be most appreciated as I am not the most experienced with php.
<section class="category-news m-all t-1of2 d-1of2">
<h2> <a href="<?php echo get_category_link('$news'); ?>">News</a> </h2>
<?php //News Posts
$newsPosts = new WP_Query('cat=23&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(); ?>
I didn’t paste all the code, sorry. ignore the above and here it is again.
I have a couple of these “boxes” that are passing data, for different categories, but I would like the user to define what category id # it is. There are 2 places I will need to pass that data. cat=23 and the $news in the get_category_link place.
<?php $news = the_field('news_box'); ?>
<section class="category-news m-all t-1of2 d-1of2">
<h2> <a href="<?php echo get_category_link('$news'); ?>">News</a> </h2>
<?php //News Posts
$newsPosts = new WP_Query('cat=23&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>
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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.