我尝试调用此函数以按字母表获取帖子列表。但由于某种原因,我不断地出错。对非对象调用成员函数get\\u col()。
我做错了什么?
{function get_post_by_alphabet($the_char){
$first_char = $the_char;
$postids=$wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));
if ($postids) {
$args=array(
\'post__in\' => $postids,
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'caller_get_posts\'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo \'List of Posts Titles beginning with the letter \'. $first_char;
while ($my_query->have_posts()) : $my_query->the_post();
echo \'<li><a href="\'.the_permalink().\'" rel="bookmark" title="Permanent Link to \'.the_title_attribute().\'"><\'.the_title().\'</a></li>\';
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
}
get_post_by_alphabet(\'H\');
}
SO网友:helgatheviking
你有两个问题。首先需要global $wpdb;
. 那么您正在使用the_title()
和the_permalink()
两者都会自动回显一个值。所以你需要把它们换成get_the_title()
和get_permalink()
.
function get_post_by_alphabet($the_char){
global $wpdb;
$first_char = $the_char;
$postids=$wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));
if ($postids) {
$args=array(
\'post__in\' => $postids,
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'caller_get_posts\'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo \'List of Posts Titles beginning with the letter \'. $first_char;
while ($my_query->have_posts()) : $my_query->the_post();
echo \'<li><a href="\'.get_permalink().\'" rel="bookmark" title="Permanent Link to \'.the_title_attribute().\'"><\'.get_the_title().\'</a></li>\';
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
}
get_post_by_alphabet(\'H\');