在index.php上以不同的方式显示POST格式(循环使用函数)

时间:2016-05-09 作者:OmiYaHomi

我是WordPress的新手,我希望在主页上以不同的方式显示帖子格式;有点像这样的图像:

homepage

问题是我使用的主题与我见过的其他主题非常不同,the loop is in a function.

在我的index.php, 我有

<?php
            if(have_posts()) :
                while(have_posts()) : the_post();
                    echo cool_post_article_normal_block();
                endwhile;
            endif;
?>
在函数中,它表示:

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;
}
我的理解是我应该在<article></article> 但我不知道怎么做get the post format 有条件地特别是因为已经有了一个粘性条件,而且我对PHP不太了解:(

我对PHP非常陌生,所以我不确定如何做到这一点。我激活了几种帖子格式:quote, video, gallery, audioimage.

我希望循环类似于:

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 个回复
最合适的回答,由SO网友:cjbj 整理而成

最好的方法是获取post格式,然后使用switch语句,如下所示:

$my_format = get_post_format ();
switch ($my_format) {
    case \'quote\': $htmlcontent = ... ; break;
    case \'aside\': $htmlcontent = ... ; break;
    and so on
    }
构建$htmlcontent的函数首先声明一些稍后使用的构建块(从$timeformat到$firsttext)。您可以通过设置更大的块来改进这一点。例如,现在的代码如果有这样的块就已经更好了:

$post-title =      \'<header class="content">
                       <h2 class="post-title"><a href="\'. get_permalink(get_the_ID()) .\'">\'. get_the_title() .\'</a></h2>
                   </header>\';
这段代码现在被复制了,这从来都不是一个好主意。

SO网友:majick

查看您的列表,您实际上只是在更改图像和报价格式摘录的默认结果(目前),您可以使用the_excerpt 滤器

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;
}
您的主题似乎有一个自定义的缩略图过滤器,名为cool_featured_figure_lazy 有两个参数。。。您可以尝试这样的操作来删除除图片帖子格式(和标准帖子)以外的任何内容的特色图片:

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;
}
如果需要,您可以类似地更改标题,您可以使用the_title 筛选。。。例如:。

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