使用发布自定义字段中的内容填充页面

时间:2011-06-18 作者:snthr

我现在正在写一个博客,每个帖子都有一首歌的名字。通过自定义字段可以很容易地将歌曲添加到帖子中。

然而,我也需要有一个页面,其中列出了所有歌曲在一个地方。不使用插件可以做到这一点吗?如果是,如何?(如果没有,那么最好的插件是什么?)

谢谢

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

我建议使用短代码,因为这样可以很容易地将歌曲列表嵌入到内容的任何地方,任何页面或帖子中。

UPDATE: 我有点神魂颠倒了,结果是这样!

function song_list_shortcode( $attrs )
{
    $r = ( object )wp_parse_args( $attrs, array(
        \'format\' => \'%post_title - %link\',
        \'link\'   => \'%song_key_name\',
        \'key\'    => \'song_key_name\'
    ) );

    $query = new WP_Query( array( \'meta_query\' => array( array( \'key\' => $r->key ) ), \'nopaging\' => true, \'update_post_term_cache\' => false ) );
    if ( !$query->have_posts() )
        return \'\';

    $meta_keys = array();
    foreach ( array( \'format\', \'link\' ) as $type ) {
        // find meta keys
        if ( !preg_match_all( \'#%([a-z0-9_-]+)#\', $r->$type, $_keys ) )
            continue;

        $_keys = array_flip( $_keys[1] );
        unset( $_keys[\'post_title\'], $_keys[\'link\'] ); // don\'t want these, not meta keys

        $meta_keys = $meta_keys + $_keys; // add new keys on to meta key stack
    }

    if ( !empty( $meta_keys ) )
        $meta_keys = array_keys( $meta_keys );

    $output = \'<ul class="songs">\';
    while ( $query->have_posts() ) {
        $query->the_post();

        $format = $r->format;
        $link = $r->link;

        if ( !empty( $meta_keys ) ) {
            // grab all meta data in one swoop (should be cached from query)
            $meta_data = get_post_custom( $query->post->ID );

            // swap out all meta key names with their actual value!
            foreach ( $meta_keys as $key ) {

                // using get_post_custom(), all meta data values are arrays
                if ( isset( $meta_data[ $key ][0] ) )
                    $value = esc_html( $meta_data[ $key ][0] );
                else
                    $value = \'\'; // meta key not found, so replace with blank

                list( $format, $link ) = str_replace( "%$key", $value, array( $format, $link ) );
            }
        }

        // swap out %post_title with actual post title
        list( $format, $link ) = str_replace( \'%post_title\', get_the_title(), array( $format, $link ) );

        // swap out %link in $format with actual $link
        $output .= \'<li>\' . str_replace( \'%link\', \'<a href="\' . get_permalink() . \'">\' . $link . \'</a>\', $format ) . \'</li>\';
    }

    wp_reset_postdata();

    $output .= \'</ul>\';
    return $output;
}
add_shortcode( \'song-list\', \'song_list_shortcode\' );
你可以这样使用它;

[song-list format="%post_title - %link - %song_date_meta_key"]
// The post title - <a href="/post/">Song Name</a> - Song Date
查看如何使用% 指示一个元键,它将在运行时与值交换。

还要注意的是%post_title%link 是两个特殊参数,分别与文章标题和锚定链接交换。

您还可以以相同的方式格式化链接文本的内容;

[song-list link="Date: %song_date_meta_key"]
// Post Title <a href="/post/">Date: Song Date</a>
最后key 属性控制检索哪些帖子。

[song-list key="song_name"]
// Retrieves all posts with the meta key \'song_name\'
我建议用最常用的参数替换函数开头的硬编码默认值。

结束

相关推荐

自定义发布类型未显示在QUERY_POSTS结果中

此查询在某些地方有效,但在其他地方无效。。。现在它在主页上不起作用。此查询位于包含在索引中的文件中。我的主题的php。常规帖子显示,但没有cutom帖子类型。此查询有什么问题?$args = array( \'post_type\' => array(\'post\', \'video\', \'slideshow\', \'poll\', \'quote\'), \'orderby\' => \'date\', \'order\' => \'DES