Get post title by Alphabet

时间:2014-01-12 作者:2611

我尝试调用此函数以按字母表获取帖子列表。但由于某种原因,我不断地出错。对非对象调用成员函数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\');
}

1 个回复
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\');

结束

相关推荐

Related posts popup

我想通过向访问者展示与他们正在阅读的帖子相关的其他帖子来留住我博客上的访问者。但他们似乎根本看不到这个小部件。因此,我有一个想法,在用户滚动帖子一定程度后,在页面底部(或任意一侧)的弹出窗口(DIV)中显示此列表。有人知道最好的方法吗?