Support

Account

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

Solving

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.

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

The topic ‘I'm looking to pass data from a field into an existing wp loop’ is closed to new replies.