2
\$\begingroup\$

A question of organization and best practices with Wordpress templates:

  • Where should I put my variables? Directly in the template? In a separate file via includes()?
  • Should I place all variables at the top of the template, or is it OK to mix them in with the HTML?
  • Should HTML be included directly in the variable, or is something like this OK?
<?php if($next_show) { ?>

    <a href="<?php echo $next_show_link; ?>" class="next-show c3"><?php echo $next_show; ?></a>
<?php } ?>

I'm a PHP novice, so if there's any guidance or pointers regarding anything that can be done more efficiently, any feedback is much appreciated!

<?php 
    get_header(); 
    require_once(THEMEASSETS . '/functions/frontend/alfa-nav.php');

    $uri = my_url();
    $uri = removeqsvar($uri, 'orden');

    $genero = get_query_var('genero');

    global $query_string;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $args = array();

    wp_reset_query();
    rewind_posts(); 

    if($genero) {
        $taxtitle = explode('+',$genero);
        $taxtitle = implode(' + ', $taxtitle);

        $genreterm = genre_format($uri);

        query_posts($query_string.'&orderby=title&order=ASC');
    } else {
        $taxtitle = get_query_var('term');
        $args = array(
            'post_type' => 'artistas',
            'posts_per_page' => -1,
        );

        query_posts($args);
    }

    $queried_terms = array();
    $showcount = 0;

    if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
        $postid = $post->ID; 

        if( has_term( '', 'alfa', $postid) ) {
            $terms = get_the_terms( $postid, 'alfa' );
            foreach($terms as $term) {
                $myterm = $term->slug;
                if(in_array($myterm, $queried_terms)) {continue;}
                $queried_terms[] = $myterm;
            }
        }

        $next_show = get_post_meta($postid, '_artistEventTitle', true);
        if($next_show) {$showcount++;}
    endwhile; endif;
    rewind_posts();
    wp_reset_query();
?>

    <section role="main" class="wrapper">
        <div class="inner filter-wrap full-wrap">
            <div class="sidebar pr qt btw">
<?php 
the_widget('Taxonomy_Drill_Down_Widget', array(
    'mode' => 'lists',
    'taxonomies' => array( 'genero', 'alfa') // list of taxonomy names
));
?>
            </div>

            <div class="content single-content">
                <?php include('includes/breadcrumbs.php'); ?>

                <header class="archive-header">
                    <h1 class="kilo archive-title"><strong class="section-title">Artistas:&nbsp;</strong><span class="tax-title"><?php echo ucwords($taxtitle); ?><b class="count">&nbsp;(<?php echo $total = $wp_query->found_posts; ?>)</b></span></h1>               
                </header>

                <?php bam_artist_alfa($queried_terms, $alfas); ?>

                <nav class="nav-hor w100 page-nav">
                    <div class="page-wrap">
<?php
global $wp_query;

$big = 999999999; // need an unlikely integer

echo $pagelinks = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'type' => 'list',
'prev_text' => '',
'next_text' => ''
) );

$showclass = ($showcount == 0 ? 'empty ' : '');

if(isset($_GET['orden']) && $showcount > 0) {
    $nombreclass = ' bg1 c2';
    $fechaclass = ' bg2 c1 current';
    $nombreuri = ' href="'.removeqsvar(my_url(), 'orden').'"';
} else {
    $nombreclass = ' bg2 c1 current';
    $fechaclass = ' bg1 c2';
    $nombreuri = '';
}

if($showcount == 0 || isset($_GET['orden'])) {
    $fechaurl = '';
} elseif(!isset($_GET['alfa']) && !isset($_GET['genero'])) {
    $fechaurl = ' href="'.$uri.'?orden=fecha"';
} else {
    $fechaurl = ' href="'.$uri.'&orden=fecha"';
}
?>
                    </div>

                    <div class="artist-sort caps bo">
                            <span class="me btn-round btn">Ordenar por:</span>
                            <a class="btn btn-round<?php echo $nombreclass; ?>"<?php echo $nombreuri; ?>>Nombre</a>

                            <a class="<?php echo $showclass; ?>btn btn-round<?php echo $fechaclass; ?>"<?php echo $fechaurl; ?>>
                                Proxima Fecha
                            </a>
                    </div>
                </nav>

                <div class="ajax-fade artist-wrap grid-wrap group">
                    <div class="group">
<?php
query_posts($query_string.'&orderby=title&order=ASC&paged='.$paged);

if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); 
    $image = wp_get_attachment_image_src( get_post_thumbnail_id(  $post->ID ), "artist-thumb" );    
    $title = get_the_title();
    $link = get_permalink();
    $next_show = get_post_meta($post->ID, '_artistEventTitle', true);
    $next_show_link = get_post_meta($post->ID, '_artistEventLink', true);
?>

                        <div class="artist-grid qt fl<?php echo ($next_show ? ' has-date' : ''); ?>">
                            <a href="<?php the_permalink(); ?>" class="round-t img">
                            <?php if($image) { ?>
                                <img src="<?php echo $image[0]; ?>" class="round-t" />
                            <?php } ?>
                            </a>

                            <div class="bg2 c1 artist-title round-b bo">
                                <h1 class="alpha"><a href="<?php echo $link; ?>"><?php echo $title; ?></a></h1>

                            <?php if($next_show) { ?>

                                <a href="<?php echo $next_show_link; ?>" class="next-show c3"><?php echo $next_show; ?></a>
                            <?php } ?>

                            </div>
                        </div>
<?php endwhile; endif; ?>


                    </div>
                    <nav class="nav-hor page-nav">
                        <div class="page-wrap">
                            <?php echo $pagelinks; ?>
                        </div>
                    </nav>
                </div>
            </div> <!-- main -->
        </div> <!-- inner -->
    </section> <!-- wrapper -->

<?php get_footer(); ?>
\$\endgroup\$
2
\$\begingroup\$

Where should I put my variables? Directly in the template? In a separate file via includes()?

Directly in the template, it makes it easier to find them.

Should I place all variables at the top of the template, or is it OK to mix them in with the HTML?

Right before where they are used. Again it makes it easier to find them. If you have some variable used several time throughout the template file, then it might make sense to put on top though.

Should HTML be included directly in the variable, or is something like this OK?

The way you've done it is the right way. Always avoid including HTML in variables if you can.

In my opinion, there's too much code at the top and within your template file. See if you can refactor some of this code into utility functions. You can them put this code in functions.php.

\$\endgroup\$

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.