我要做的是创建一个wp_query,以便结果只是标题的首字母。
这就是我展示的方式。超链接字母&数字..
<div class="align_Center">
<?php $args = array(
"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","ALL" ); ?>
<?php
for($x = 0; $x < 37; $x++) { ?>
<span style="color: #777;"> | </span>
<a href="../page-title-<?php echo $args[$x]; ?>" class="movie_lists"><?php echo $args[$x]; ?></a>
<?php } ?>
<span style="color: #777;"> |</span>
</div>
这是我的测试页面..
<div class="col-md-6">
<!-- ############################################## -->
<!-- ############### PAGE TITLE T ################# -->
<!-- ############################################## -->
<p class="f_header">PAGE TITLE T</p>
<?php
global $wpdb;
$first_char = 'T';
$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_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
?>
<table width="100%"><!-- #### TABLE #### -->
<?php
while ($query->have_posts()) {
$query->the_post();
?>
<tr style="border-bottom: thin solid #111;">
<td class="movie_genre">
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</td>
</tr>
<?php }; ?>
</table><!-- #### / TABLE #### -->
<?php }; wp_reset_postdata(); }; ?>
测试页面目前在根主题目录中保存为page-title-t,目前它没有提供任何数据,所以我有点困惑。
答案 0 :(得分:0)
提供的代码是正确的,包含以下一般注释:
您说明了您正在列出网页,但您的查询指出'post_type' => 'post'
。 post_type
参数同时接受字符串或数组,因此您可以选择'post_type' => 'page'
或'post_type' => array('post','page')
确保您的数据库归类是不区分大小写,(应以_ci结尾,例如utf8_general_ci
)否则您应该处理小的大写字母查询(t
和T
)
caller_get_posts
中的$args
应移至{em> WordPress 版本大于 3.1.0 <的ignore_sticky_posts
/ LI>