将自定义字段值添加到wp_list_ages

时间:2011-01-04 作者:Red

在使用wp\\U list\\U页面时,我试图为每个页面引入一个自定义字段vales。例如,我想在侧边栏中列出每个公文包项目,但也要通过拉入自定义字段值来显示其工作类型的原因,使其看起来像这样:

公文包项目1类型:Web设计公文包项目2类型:Web开发这是我用来拉入自定义帖子类型的代码,它工作得很好,我只是不知道如何让它也拉入自定义字段值:

function wp_list_post_types( $args ) {
$defaults = array(
    \'numberposts\'  => -1,
    \'offset\'       => 0,
    \'orderby\'      => \'menu_order, post_title\',
    \'post_type\'    => \'our-work\',
    \'depth\'        => 0,
    \'show_date\'    => \'\',
    \'date_format\'  => get_option(\'date_format\'),
    \'child_of\'     => 0,
    \'exclude\'      => \'\',
    \'include\'      => \'\',
    \'title_li\'     => __(\'\'),
    \'echo\'         => 1,
    \'link_before\'  => \'\',
    \'link_after\'   => \'\',
    \'exclude_tree\' => \'\' );

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );

$output = \'\';
$current_page = 0;

// sanitize, mostly to keep spaces out
$r[\'exclude\'] = preg_replace(\'/[^0-9,]/\', \'\', $r[\'exclude\']);

// Allow plugins to filter an array of excluded pages (but don\'t put a nullstring into the array)
$exclude_array = ( $r[\'exclude\'] ) ? explode(\',\', $r[\'exclude\']) : array();
$r[\'exclude\'] = implode( \',\', apply_filters(\'wp_list_post_types_excludes\', $exclude_array) );

// Query pages.
$r[\'hierarchical\'] = 0;
$pages = get_posts($r);

if ( !empty($pages) ) {
    if ( $r[\'title_li\'] )
        $output .= \'<li class="pagenav">\' . $r[\'title_li\'] . \'<ul>\';

    global $wp_query;
    if ( ($r[\'post_type\'] == get_query_var(\'post_type\')) || is_attachment() )
        $current_page = $wp_query->get_queried_object_id();
    $output .= walk_page_tree($pages, $r[\'depth\'], $current_page, $r);

    if ( $r[\'title_li\'] )
        $output .= \'</ul></li>\';
}

$output = apply_filters(\'wp_list_pages\', $output, $r);

if ( $r[\'echo\'] )
    echo $output;
else
    return $output;
}

1 个回复
SO网友:MathSmath

如果您只想在侧边栏中列出一些最近的公文包项目,那么您试图模拟wp\\u list\\u pages函数,这就太过分了。您可以通过一个简单的自定义查询来实现这一点。(除非出于某种原因,您真的想对wp\\U list\\u页面执行此操作,但我想不出您为什么会这样做。)

// Set up the query args
$arr_query_args = array(
    \'numberposts\'  => 5, // Or however many posts you want to show
    \'orderby\'      => \'post_title\',
    \'post_type\'    => \'our-work\'
);

// Run the query
$arr_posts = get_posts( $arr_query_args );

// Global the post obj
global $post;

foreach( $arr_posts as $this_post ) { // For every post retrieved

    // Now you have the (mostly) complete post object. Print out whatever you want from it!

    $permalink = get_permalink($this_post->ID); // Get the post\'s permalink

    echo \'<a href="\' . $permalink . \'"\';

    // If this link is for the current page, add a class
    if(is_object($post) && $post->ID == $this_post->ID) {
        echo \' class="current_page_item"\';
    }

    echo \'>\';
        echo $this_post->post_title . \': \';
    echo \'</a>\';

    // You can use get_post_meta to grab meta values for this post
    echo(get_post_meta($this_post->ID, \'your-custom-field-key\', true));

}
我想如果您想在不同的位置向它传递不同的参数,可以将其包装在函数中并将其用作模板标记。

结束

相关推荐

add_query_arg not working

我添加了一个过滤器,以便在类别中导航时将参数附加到URL上。当浏览类别时,仅当sort 参数已设置。例如,单击“查看投票最多的所有帖子”时,将显示投票率较高的帖子。在这里,您可以通过添加sort=most_voted 或sort=doleast_voted 到URL,使用cat=?.add_filter( \'category_link\',\'append_parameter\', 10, 2 ); function append_parameter( $link, $query )