0

我想在我的第一个公共主题中提供对帖子格式的支持(我通常为客户进行自定义构建),并且希望明确地知道如何获取各种不同的内容,特别是对于索引,而不是关于模板层次结构,而是从 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....

4

1 回答 1

-1

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

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

于 2013-11-13T20:16:09.150 回答