For WordPress sites, I always end up building custom post types. They usually have categories, but I don't use much of the 'archive' abilities. This site, for example, is for a film director and so it has "project" resources and then have the standard 'category' taxonomy. I find myself copying and pasting this code in blocks to list each category.
It seems like there are 2 ways to go with PHP. You can keep the markup clear and work the PHP into it, or stay mostly in PHP land and echo markup. I think PHP is a fine language - but simply the syntax has lead me to JS frameworks in its place:
{{#each things as |thing|}}
<li>{{thing.name}}</li>
{{/each}}
Regarding custom fields, I am far more accustomed to ACF vs CMB2, but recently worked on a project with CMB2 and I kinda saw some light in creating the template with more PHP... but both ways still seem less than awesome.
Here's the block of code I find myself using the most. I do it so much that I have a version with xxxxx
where project
is - that I use to just find and replace with the resource name.
The markup isn't always exactly the same - so I paste it rather than build a function that just takes 1 parameter of resource
- but I could have conditionals
What say you - brave code reviewers? How can I up my game with this? Simplify? Change my view-point? Just relax? build a function? Best practices?
Should I be using some archive template for repeating post-type categories instead?
<section class='container projects one'>
<div class='inner-w'>
<?php
// =========================================
// Define the rules/arguments
$project_args = array(
'post_type' => 'project',
'showposts' => '-1',
'category_name' => 'documentary',
);
// The Query
$project_query = new WP_Query($project_args);
// The Loop
if ( $project_query->have_posts() ) { ?>
<h2 class='category-title'>Documentary</h2>
<ul class='project-list'>
<?php
while ( $project_query->have_posts() ) {
$project_query->the_post();
$image = get_field('project_poster');
?>
<li class='project'>
<a class='link' href='<?php the_permalink(); ?>'>
<figure class='poster'>
<?php
if ( $image ) {
$poster = $image['sizes']['medium'];
} else {
$poster = 'https://placehold.it/1600x900';
}
?>
<img src='<?php echo $poster; ?>' alt='<?php echo get_the_title(); ?> poster image' />
</figure>
<h1 class='title'><?php the_field("project_title"); ?></h1>
</a>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<?php // what happens if there are no posts? ?>
<? } ?>
<?php wp_reset_postdata(); /* restore original post data */ ?>
</div>
</section>
<?php // repeat many blocks like the 'section' above ?>