根据一篇关于挖掘WP的教程,这是我想出的短代码(http://digwp.com/2010/01/custom-query-shortcode/) 以显示特定专辑中的歌曲列表。
[loop the_query="post_type=song&albumtype=enc&order=ASC&showposts=-1"]
上面是一个示例,使用自定义分类法来实现我想要的。这将返回一个列表项,其中包含歌曲帖子类型项的标题和永久链接。
然而,我还想从几个与歌曲帖子类型相关的自定义字段中返回数据,例如“Duration”。我试图以在模板中插入的方式插入此内容,但它没有返回任何内容。
我应该在哪里向下面的代码中添加什么,以便在链接旁边显示自定义字段的值?
function custom_query_shortcode($atts) {
// EXAMPLE USAGE:
// [loop the_query="showposts=100&post_type=page&post_parent=453"]
// Defaults
extract(shortcode_atts(array(
"the_query" => \'\'
), $atts));
// de-funkify query
$the_query = preg_replace(\'~�*([0-9a-f]+);~ei\', \'chr(hexdec("\\\\1"))\', $the_query);
$the_query = preg_replace(\'~�*([0-9]+);~e\', \'chr(\\\\1)\', $the_query);
// query is made
query_posts($the_query);
// Reset and setup variables
$output = \'\';
$temp_title = \'\';
$temp_link = \'\';
// the loop
if (have_posts()) : while (have_posts()) : the_post();
$temp_title = get_the_title($post->ID);
$temp_link = get_permalink($post->ID);
// output all findings - CUSTOMIZE TO YOUR LIKING
$output .= "<li><a href=\'$temp_link\'>$temp_title</a> I WANT MY CUSTOM FIELD VALUE HERE</li>";
endwhile; else:
$output .= "nothing found.";
endif;
wp_reset_query();
return $output;
}
add_shortcode("loop", "custom_query_shortcode");
以上代码来自我的函数。php文件,这是显而易见的。
最合适的回答,由SO网友:Iva 整理而成
一位朋友指出了如何解决这个问题。如果其他人遇到与我相同的问题,这就是正确的代码。
// Custom shortcode, to query tracklistings.
function custom_query_shortcode($atts) {
// Defaults
extract(shortcode_atts(array(
"the_query" => \'\'
), $atts));
// de-funkify query
$the_query = preg_replace(\'~�*([0-9a-f]+);~ei\', \'chr(hexdec("\\\\1"))\', $the_query);
$the_query = preg_replace(\'~�*([0-9]+);~e\', \'chr(\\\\1)\', $the_query);
// query is made
$query_posts = get_posts($the_query);
// Reset and setup variables
$output = \'\';
// the loop
if (!empty($query_posts)) {
foreach($query_posts as &$post_object) {
$output .= \'<li><a href="\'. get_permalink($post_object->ID) .\'">\'. $post_object->post_title .\'</a> <span>\'. get_post_meta($post_object->ID, \'Duration\', true) .\'</span></li>\';
}
}
return $output;
}
add_shortcode("loop", "custom_query_shortcode");
SO网友:Bainternet
您只需查询自定义字段并使用快捷码返回它们:
// EXAMPLE USAGE:
// [loop the_query="showposts=100&post_type=page&post_parent=453"]
function custom_query_shortcode($atts) {
// Defaults
extract(shortcode_atts(array(
"the_query" => \'\'
), $atts));
// de-funkify query
$the_query = preg_replace(\'~�*([0-9a-f]+);~ei\', \'chr(hexdec("\\\\1"))\', $the_query);
$the_query = preg_replace(\'~�*([0-9]+);~e\', \'chr(\\\\1)\', $the_query);
// query is made
query_posts($the_query);
// Reset and setup variables
$output = \'\';
$temp_title = \'\';
$temp_link = \'\';
// the loop
if (have_posts()){
while (have_posts()){
the_post();
$temp_title = get_the_title($post->ID);
$temp_link = get_permalink($post->ID);
//get you custom fields
$duration = get_post_meta( $post->ID, \'duration\', true );
$other_custom_field = get_post_meta( $post->ID, \'other_custom_field_key\', true );
//add the custom field to the output var
$output .= "<li><a href=\'$temp_link\'>$temp_title</a> Duration: $duration, Other: $other_custom_field</li>";
}
}else{
$output .= "nothing found.";
}
wp_reset_query();
return $output;
}
add_shortcode("loop", "custom_query_shortcode");