0

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.

  1. There block of code for each upcoming conference. These take the first cells.

  2. 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.

  3. 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.

  1. 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();
  • Can you add some code to demonstrate what you are attempting and perhaps the error you're receiving? – Welcher Jan 9 '17 at 2:30
1

After reviewing your updated code it might be better to use template parts to achieve this. The idea is that you have a directory containing various template parts, which can then be called into the document wherever you need them.

This will help clean up the template somewhat.

<?php
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)
    {
        get_template_part( 'template-parts/watch', 'list' );
    }
    else
    {
        get_template_part( 'template-parts/other', 'template' );
    }
}


/**
 * Template part "watch-list.php"
 * Location: wp-themes/[theme]/template-parts/watch-list.php
 */
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();
echo '</tr>';

If you need to pass variables from the main template to the template part you'll need to use the following code instead:

include( locate_template( 'template-parts/watch-list.php' ) );


Old Answer (example function)

It's a little difficult to understand what you need, however, I've attached some code below.

The idea behind the functions.php file is that you can create functions in there, to reference in your template. In the example below you can see there is a conditional (which always equals true). When the condition is met it runs the so_dynamic_content() function referenced in the functions.php file

The function is basic, however, it can accept parameters, i.e. layout and id, which can be used to tailor what content is displayed.

Although this doesn't directly answer your question, it should hopefully set you on the right track. If not, add a comment and I'll update accordingly.

<?php
/**
 * template-file.php
 */

while ( have_posts() ) : the_post();

    $the_id = get_the_ID();

    if ( 'Condition X' == 'Condition X' ) {
        so_dynamic_content( 'left', $the_id );
    } else {
        so_dynamic_content( 'right', $the_id );
    }

endwhile;

/** 
 * functions.php
 */
function so_dynamic_content( $layout, $post_id = '' ) {

    if ( $post_id == '' ) {
        global $post;
        $post_id = $post->ID;
    }

    ob_start();
    ?>

    <div class="dynamic-content <?php echo $layout; ?>">
        <p>Text</p>
        <a href="<?php echo get_permalink( $post_id ); ?>">Anchor</a>
        <!-- HTML stuff -->
    </div>

    <?php
    $content = ob_get_clean();
    echo $content;

}
  • I will consider and try your code and report on the results when I get back on that project in a couple of days! – Nora McDougall-Collins Jan 11 '17 at 2:26
  • I have never used ob_start(). I looked it up and found that it can be problematic when there are other plugins that might also be calling it. wordpress.stackexchange.com/questions/41012/… – Nora McDougall-Collins Jan 14 '17 at 0:35
  • That was strictly used as an example and isn't required. I've updated the answer in response to your updated question. Hopefully it helps. – Kieran McClung Jan 16 '17 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.