2013-12-18 135 views
0

我想添加短代码来在WordPress中创建自定义主页。自定义WordPress查询

这里是我的代码:

function custom_query() 
{ 
extract(shortcode_atts(array(
     'name' => '', 
     'title' => '', 
     'limit' => '5', 
     ),$atts)); 

     //The Query 
    query_posts('showposts=3&orderby=date&order=DESC&category_name='.$name); 
     //The Loop 
     if (have_posts()) : while (have_posts()) : the_post(); 

$return .= '<div class="posts-title">'.$title.'</div>'; 
$return .= '<div class="posts">'; 
$return .= '<a href="'.get_permalink($post->ID).'" title="">'.get_the_post_thumbnail($post_id, 'thumb').'</a>'; 
$return .= '</div>'; 

endwhile; 
endif; 
$return .= '</div>'; 
     //Reset Query 
    wp_reset_query(); 
return $return; 
} 
add_shortcode('custom_query', 'custom_query'); 

我试图在两个方面:

[custom query name=sport title=Sport News] 
[custom_query name="sport" title="Sport News"] 

输出仍然会显示最新的WordPress的职位,而不是在$名称中输入一个类,甚至$标题不显示。 我做了这个谷歌帮助和教程,我不是一个专业的编码器,所以如果有人可以帮助我解决这个问题,我会感激。

回答

0

输出仍然会显示最新的WordPress的职位,而不是在$名称中输入一个类,甚至$标题不会显示

这意味着你的$title$name是空的。请再次检查并确保您在该功能内部获得了正确的值。

编辑:

发现了一些错误,在你的代码,

尝试以下

function custom_query_fun($atts) 
{ 
extract(shortcode_atts(array(
     'name' => '', 
     'title' => '', 
     'limit' => '5', 
     ),$atts,'custom_query')); 

     //The Query 
    query_posts('showposts=3&orderby=date&order=DESC&category_name='.$name); 
     //The Loop 
     if (have_posts()) : while (have_posts()) : the_post(); 

$return .= '<div class="posts-title">'.$title.'</div>'; 
$return .= '<div class="posts">'; 
$return .= '<a href="'.get_permalink($post->ID).'" title="">'.get_the_post_thumbnail($post_id, 'thumb').'</a>'; 
$return .= '</div>'; 

endwhile; 
endif; 
$return .= '</div>'; 
     //Reset Query 
    wp_reset_query(); 
return $return; 
} 
add_shortcode('custom_query', 'custom_query_fun'); 
+0

$的ATT失踪。非常感谢。 – user3114390