0

I'm new to WordPress and I'm hoping to display post formats differently on the home page; sort of like this image:

homepage

The problem is that the theme I'm using is very different than other themes I've seen, the loop is in a function.

In my index.php, I have

<?php
            if(have_posts()) :
                while(have_posts()) : the_post();
                    echo cool_post_article_normal_block();
                endwhile;
            endif;
?>

In the function it says:

function cool_post_article_normal_block ($usecategory = false)
{
    add_filter( 'excerpt_length', 'cool_article_excerpt_latest_length', 999 );
    add_filter('excerpt_more', 'cool_article_excerpt_latest_more');
    $timeformat = "<time class='post-date' datetime='" . get_the_time("Y-m-d H:i:s") . "'>" . get_the_time("F j, Y ")  . "</time>";
    $categoryarray = get_the_category();
    $categorytext = '';
    if(!empty($categoryarray))
        $categorytext = "<span class='post-category'><a href='" . get_category_link($categoryarray[0]->term_id) . "' rel='category'>" . $categoryarray[0]->name . "</a></span>";
    $authorurl = get_author_posts_url(get_the_author_meta('ID'));
    $authorname = apply_filters('cool_get_author_name', null);
    $authortext = '<span class="post-author">'. __('By', 'cool_textdomain') . ' <a href="' . $authorurl .'" rel="author">' . $authorname .'</a></span>';
    $firsttext = $authortext;
    if($usecategory) {
        $firsttext = $categorytext;
    }

    $post_class = get_post_class(array('review-list', 'clearfix'), get_the_ID());

    if(is_sticky()) {
        $htmlcontent =
            '<article class="'. implode(' ', $post_class) .'">
               <div class="col-md-6">
                   ' . apply_filters('cool_featured_figure_lazy', null, 'half-post-featured') . '
               </div>
               <div class="col-md-6">
                   <div class="">
                       <header class="content">
                           <h2 class="post-title"><a href="'. get_permalink(get_the_ID()) .'">'. get_the_title() .'</a></h2>
                       </header>
                       <div class="post-meta">
                           ' . $firsttext . '
                           ' . $timeformat .  '
                       </div>
                       <div class="post-excerpt">
                           <p>'.  get_the_excerpt() .'</p>
                       </div>
                   </div>
               </div>
           </article>';
    } else {
        $htmlcontent =
            '<article class="'. implode(' ', $post_class) .'">
           <div class="col-md-5">
               ' . apply_filters('cool_featured_figure_lazy', null, 'half-post-featured') . '
           </div>
           <div class="col-md-7">
               <div class="">
                   <header class="content">
                       <h2 class="post-title"><a href="'. get_permalink(get_the_ID()) .'">'. get_the_title() .'</a></h2>
                   </header>
                   <div class="post-meta">
                       ' . $firsttext . '
                       ' . $timeformat .  '
                   </div>
                   <div class="post-excerpt">
                       <p>'.  get_the_excerpt() .'</p>
                   </div>
               </div>
           </div>
       </article>';
    }

    remove_filter( 'excerpt_length', 'cool_article_excerpt_latest_length');
    remove_filter( 'excerpt_more', 'cool_article_excerpt_latest_more');

    return $htmlcontent;
}

What I understand is that I'm supposed to modify between <article></article> but what I don't know how to do is get the post format conditionally; especially since there is already a sticky conditional-- and I don't know PHP well :(

I'm very new to PHP so I'm unsure how to do this. I have several post formats activated: quote, video, gallery, audio and image.

I'd like the loop to go something like:

if (sticky) {
if (quote)
 show title only
} elseif (video, audio, gallery) {
 show title
 show excerpt
} elseif(image){
 show title
 show featured image
} else {
if (quote) {
 show title only
}elseif (video, audio, gallery){
 show title
 show excerpt
} elseif (image) {
 show title
 show featured image
}

2 Answers 2

1

The best way to do this is get the post format and then use the switch statement, like this:

$my_format = get_post_format ();
switch ($my_format) {
    case 'quote': $htmlcontent = ... ; break;
    case 'aside': $htmlcontent = ... ; break;
    and so on
    }

The function that builds up $htmlcontent starts with declaring some building blocks that are used later on (from $timeformat to $firsttext). You can improve on this by setting up larger blocks. The code as it is now, for instance, would already be better if it had blocks like this:

$post-title =      '<header class="content">
                       <h2 class="post-title"><a href="'. get_permalink(get_the_ID()) .'">'. get_the_title() .'</a></h2>
                   </header>';

This code is now duplicated, which is never a good idea.

0

Looking at your list, you are really only changing the default result for excerpts for the image and quote formats (at the moment), you can remove those using the_excerpt filter...

add_filter('the_excerpt','custom_post_format_excerpt',99);
function custom_post_format_excerpt() {
     $noexcerptformats = array('quote','image');
     if (has_post_format($noexcerptformats)) {$excerpt = '';}
     return $excerpt;
}

It looks like your theme has a custom filter for the thumbnail called cool_featured_figure_lazy with two arguments... you might try something like this to remove the featured image for anything but image post formats (and standard posts):

add_filter('cool_featured_figure_lazy','custom_post_format_thumbnail',99,2);
function custom_post_format_thumbnail($thumbnail,$class) {
    $nothumbnailformats = array('quote','video','audio','gallery');
    if (has_post_format($nothumbnailformats)) {$thumbnail = '';}
    return $thumbnail;
}

If you need to you can change the title similarly, you can use the the_title filter for that... eg.

add_filter('the_title','custom_post_format_title',99);
function custom_post_format_title($title) {
     if (is_sticky()) {$title = 'Featured: '.$title;}
     return $title;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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