I have a somewhat complicated WordPress template that keeps getting more complicated as the client wants it to work this way under one condition and another way under another condition, and so on and so forth.
The home page has two columns of cells with 4 types of content.
There block of code for each upcoming conference. These take the first cells.
There is a block of code for conference services. This will take the last cell, if there are an uneven number of conferences. That means that this content will always be on the right side of the page, if it shows.
There is a block of content that is a Watch List of conferences where full information is not yet available. This block will show if it's published, that is there are conferences in the list.
It will show up on the right column, right after the conferences if there are an odd number of conferences and on the left side, right after the conferences, if there are an even number of conferences.
- There is a block of content for a webinar series that always shows after the Watch List, if it shows, after the conferences, if the Watch List doesn't show and before the Conferences Services, if there is an odd cell.
So, the Watch List and Webinar Series cells can show on either the right or left columns, depending on the number of upcoming conferences and whether the Watch List will show. . I would like to create a function in functions.php for these two block of code because the template file is becoming rather cumbersome to maintain and add to.
The rub is that there is a query that pulls the content fields from one particular post. I would like the query to be outside the function, but the foreach is throwing an error, so I'm guessing that the function is actually processed before the code in the template file. Is there a way to call a function in functions.php where the php is process inline with the rest of the template?
Code added for clarification. This is one of three shifting blocks of code that could appear on the page. I have included more code than what I want to put in functions.php for context. The code I want to move out starts with echo ''; and ends with the cell closing.
*** Note this code will soon change to remove the table structure, but that is not part of this question.
/* WATCH LIST AVAILABLE?:
To decide whether to add a Watch List cell before the Webinar Series cell, check whether the Watch List post is published
*/
query_posts( 'p=1684&posts_per_page=1');
if ( get_post_status ( '1684' ) == 'publish' )
{
/* CHECK WHETHER WATCH LIST AND WEBINAR WILL BE IN SAME ROW */
if(($NumberConferences % 2) == 0)
{
echo '<tr>';
while( have_posts() ) : the_post();
echo '<td>';
echo '<div class="RightBorderLayer">';
echo '<div class="HomeLeftCell">';
$HomeWatchListImage = types_render_field("watch-list-home-page-image", array("raw"=>"true"));
if($HomeWatchListImage != '')
echo '<a href="'.get_permalink().'"><img src="'.$HomeWatchListImage.'"></a>';
$WatchListTitles = get_post_meta($post->ID, 'wpcf-watch-list-title', false);
$i = 1;
foreach($WatchListTitles as $WatchListTitle)
{
echo '<li style="text-align: left;"><strong>Workshop '.$i.'</strong>: '.$WatchListTitle.'</li>';
$i++;
} // End foreach
echo '<div class="HomePageCellNote">If you are interested in any of these events and are not regularly receiving WFCA notices, signup for our email list in the bottom left of this page. All regular WFCA subscribers will receive notice of these events as available.</div>';
echo '<div style="clear: both;"></div>';
echo '</div><!-- End Home Left Cell div -->';
echo '</div><!-- End Right Border Layer -->';
echo '</td>';
endwhile;
wp_reset_postdata();