在WordPress中从the_content()渲染帖子格式 - 一定有更好的方法


Rendering Post formats from the_content() in WordPress - there must be a better way?

我想在我的第一个公共主题中为帖子格式提供支持(我通常为客户做自定义构建),并希望确切地知道如何获取各种不同的内容,特别是对于索引,而不是关于模板层次结构,而是提取特定数据(即: 来自 the_content() 的视频或音频 URL、图库、引用和作者等)。

常见的或标准的方法是什么 - 我假设过滤内容以使用这些内容? 欢迎任何链接/教程,建议等。

总而言之,我追求两件事

a) 我应该支持帖子格式吗b) 有没有标准的方法?

我现在用于视频(例如)的是(使用自定义元框):

  <?php
// Display Videos
// Utility function - allow us to strpos an array
if ( ! function_exists( 'video_strpos_arr' )) {
    function video_strpos_arr($haystack, $needle) {
    if( !is_array($needle) ) $needle = array($needle);
        foreach( $needle as $what ) {
            if( ($pos = strpos($haystack, $what) ) !==false ) return $pos;
        }
        return false;
    }
}
// Get Ready Display the Video
$embedCheck     = array("<embed", "<video", "<ifram");// only checking against the first 6
$mykey_values   = get_post_custom_values('_format_video_embed');
$media_to_display = '';
// check if the audio metabox is used
if ( isset($mykey_values) && !empty($mykey_values) ) {
    // iterate over values passed
    foreach ( $mykey_values as $key => $value ) {
         if ( !empty($value) ) {
            $firstCar = substr($value, 0, 6); // get the first 6 char.
            // if its a http(s).
            if ( strpos($firstCar, "http:/" ) !== false || strpos($firstCar, "https:" ) !== false ) {
                // send it to wp_oembed to see if the link is oembed enabled.
                (wp_oembed_get($value) !==false ?
                    $media_to_display = '<div class="video" style="width:100%; overflow:hidden;">' .
                    wp_oembed_get($value) . '</div>' :
                    // if not output a link.
                    $media_to_display =  '<a class="button videolink" href="' .
                    $value . '" target="_blank">Video link: ' . the_title() . '</a>'
                );
            }
            // if its the embed code that matches our array defined above.
            else if ( video_strpos_arr($firstCar, $embedCheck ) !== false ) {
                $media_to_display = '<div class="video" style="width:100%; overflow:hidden;">' .$value. '</div>';
            }
        }
    }; // end foreach
} // end conditional
if ( is_singular() ) {
    // output a filtered excerpt displaying the result of the conditionals above.
    echo apply_filters('the_content', $media_to_display );
    the_content();
} else {
    // output a filtered excerpt displaying the result of the conditionals above.
    get_template_part( 'loop/loop', 'indextitle' );
    echo apply_filters('the_excerpt', $media_to_display );
    ?><p><?php the_excerpt(); ?></p>
    <a href="<?php the_permalink(); ?>" class="button">Read More</a> <?php
}

它适用于元框,我可以使用这样的函数来获取 URL 并传递它而不是元框值,然后将其从 the_content() 中过滤掉

function nona_url_grabber() {
    if ( ! preg_match( '/<a's[^>]*?href=[''"](.+?)[''"]/is', get_the_content(), $matches ) )
        return false;
    return esc_url_raw( $matches[1] );
}

但我觉得对于应该得到一些支持的东西来说,这是很多 PT......

就个人而言,我并不真正使用帖子格式,我倾向于使用自定义帖子类型,但像往常一样,WP 提供了几种实现相同结果的方法。无论如何,我认为WP已经在法典中很好地解释了您需要的信息。特别是,我认为如果您向下滚动到函数引用内容块的正下方,从添加主题支持开始,您会找到所需的信息。

简而言之,您可以修改主题模板文件以检查帖子格式并根据该值以不同的方式输出。例如,您可以选中if( has_post_format('photo') ),并且仅在匹配时输出帖子的特色图像,如果不匹配,则输出整个正常循环。您可以执行 switch 语句,或者在模板中包含一堆其他条件。您甚至可以使用单独的模板文件(请参阅此处,例如。 taxonomy-post_format-post-format-<formatname>.php )。我不知道是否有标准或首选方法,我建议您选择最适合您工作流程的方法。

相关文章: