Php 如何列出文章并按字母顺序包装?
我在我的网站上有一个帖子列表,我想做的是按标题从a到Z的字母顺序将它们包装起来,得到如下术语表:Php 如何列出文章并按字母顺序包装?,php,wordpress,sorting,alphabetical-sort,glossary,Php,Wordpress,Sorting,Alphabetical Sort,Glossary,我在我的网站上有一个帖子列表,我想做的是按标题从a到Z的字母顺序将它们包装起来,得到如下术语表: A. Apple B. Banana C. Carotts D. E. F. G. Grenada 等等,直到字母z 即使没有邮件,我也要把信展示出来 我想把结果包装在这个结构中: <div class="group_letter"> <div class="letter">A</div> <div class="post">Apple&
A.
Apple
B.
Banana
C.
Carotts
D.
E.
F.
G.
Grenada
等等,直到字母z
即使没有邮件,我也要把信展示出来
我想把结果包装在这个结构中:
<div class="group_letter">
<div class="letter">A</div>
<div class="post">Apple</div>
</div>
<div class="group_letter">
<div class="letter">B</div>
<div class="post">Banana</div>
</div>
A.
苹果
B
香蕉
以下是我到目前为止得到的信息:
<?php
$letter=' ';
query_posts( array ( 'post_type' => 'auteurs', 'orderby' => 'title', 'order' => 'ASC' ) );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$title=get_the_title();
$initial=strtoupper(substr($title,0,1));
if($initial!=$letter) {
echo "<div>$initial</div>";
$letter=$initial;
}
echo "<div class='post'>" . $title. "</div>";
?>
<?php endwhile; endif; wp_reset_query(); ?>
抱歉,从手机中发布电视,因此可能无法正确显示
首先,您必须创建一个包含所有字母的数组
$alph = array('A', 'B', 'C',.... 'Z');
<
?php
$title=get_the_title();
foreach($alph as $key) {
// add while loop here
initial=strtoupper(substr($title,0,1));
if($initial!=$key) {
echo '<div class="group_letter">';
echo "<div>$key</div>";
}else{
echo "<div class='post'>" . $title. "</div>";
}
echo "</div>";
// end while loop here
}
?>
$alph=array('A','B','C','Z');
<
?php
$title=获取标题();
foreach($alph作为$key){
//在此处添加while循环
初始值=strtoupper(substr($title,0,1));
如果($initial!=$key){
回声';
回显“$key”;
}否则{
回声“$title.”;
}
回声“;
//在这里循环结束
}
?>
很抱歉,从手机上传电视,因此可能无法正确显示
首先,您必须创建一个包含所有字母的数组
$alph = array('A', 'B', 'C',.... 'Z');
<
?php
$title=get_the_title();
foreach($alph as $key) {
// add while loop here
initial=strtoupper(substr($title,0,1));
if($initial!=$key) {
echo '<div class="group_letter">';
echo "<div>$key</div>";
}else{
echo "<div class='post'>" . $title. "</div>";
}
echo "</div>";
// end while loop here
}
?>
$alph=array('A','B','C','Z');
<
?php
$title=获取标题();
foreach($alph作为$key){
//在此处添加while循环
初始值=strtoupper(substr($title,0,1));
如果($initial!=$key){
回声';
回显“$key”;
}否则{
回声“$title.”;
}
回声“;
//在这里循环结束
}
?>
我会用WP\u查询上的过滤器来解决这个问题。一种检测额外查询变量的方法。
将其添加到functions.php中
add_filter( 'posts_where', 'title_filter', 10, 2 );
function title_filter( $where, &$wp_query )
{
global $wpdb;
if ( $search_term = $wp_query->get( 'search_prod_title' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $search_term ) ) . '%\'';
}
return $where;
}
一旦过滤器就位,就可以使用wp_查询和字母表的数组范围
foreach (range('A', 'Z') as $char) {
echo '<div class="group_letter">';
echo '<div class="letter">'.$char.'</div>';
$args = array(
'post_type' => 'auteurs',
'posts_per_page' => -1,
'search_prod_title' => $char,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$title=get_the_title();
$initial=strtoupper(substr($title,0,1));
if($initial==$char) {
echo "<div class='post'>" . $title. "</div>";
}
endwhile;
wp_reset_postdata();
endif;
echo '</div>';
}
remove_filter( 'posts_where', 'title_filter', 10, 2 );
foreach(范围('A','Z')为$char){
回声';
回显“.$char.”;
$args=数组(
“post_type”=>“auteurs”,
“每页帖子数”=>-1,
“搜索产品标题”=>$char,
“发布状态”=>“发布”,
'orderby'=>'title',
“订单”=>“ASC”
);
$thew_query=newwp_query($args);
如果($the\u query->have\u posts()):
while($the_query->have_posts()):$the_query->the_post();
$title=获取标题();
$initial=strotupper(substr($title,0,1));
如果($initial==$char){
回声“$title.”;
}
结束时;
wp_reset_postdata();
endif;
回声';
}
移除过滤器('posts\u where','title\u filter',10,2);
我还没有测试过这段代码,我希望它能工作。
您可以获得有关Wp_查询的更多信息我将通过Wp_查询上的过滤器来解决这个问题。一种检测额外查询变量的方法。
将其添加到functions.php中
add_filter( 'posts_where', 'title_filter', 10, 2 );
function title_filter( $where, &$wp_query )
{
global $wpdb;
if ( $search_term = $wp_query->get( 'search_prod_title' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $search_term ) ) . '%\'';
}
return $where;
}
一旦过滤器就位,就可以使用wp_查询和字母表的数组范围
foreach (range('A', 'Z') as $char) {
echo '<div class="group_letter">';
echo '<div class="letter">'.$char.'</div>';
$args = array(
'post_type' => 'auteurs',
'posts_per_page' => -1,
'search_prod_title' => $char,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$title=get_the_title();
$initial=strtoupper(substr($title,0,1));
if($initial==$char) {
echo "<div class='post'>" . $title. "</div>";
}
endwhile;
wp_reset_postdata();
endif;
echo '</div>';
}
remove_filter( 'posts_where', 'title_filter', 10, 2 );
foreach(范围('A','Z')为$char){
回声';
回显“.$char.”;
$args=数组(
“post_type”=>“auteurs”,
“每页帖子数”=>-1,
“搜索产品标题”=>$char,
“发布状态”=>“发布”,
'orderby'=>'title',
“订单”=>“ASC”
);
$thew_query=newwp_query($args);
如果($the\u query->have\u posts()):
while($the_query->have_posts()):$the_query->the_post();
$title=获取标题();
$initial=strotupper(substr($title,0,1));
如果($initial==$char){
回声“$title.”;
}
结束时;
wp_reset_postdata();
endif;
回声';
}
移除过滤器('posts\u where','title\u filter',10,2);
我还没有测试过这段代码,我希望它能工作。
您可以获得有关Wp_查询的更多信息谢谢您的回复。所以我必须复制function.php中的第一个代码,然后复制页面上的另一个?我找不到如何回应结果。。你能帮我吗?回音哪一个结果,你需要把第一个代码复制到函数中,第二个复制到你的页面中。使用你的代码,我在所有字母后面得到我所有的帖子。A:我所有的帖子B:我所有的帖子C:我所有的帖子。。。“search\u prod\u title”指的是什么?search\u prod\u title是您传递给查询的自定义参数,请使用已编辑的参数,刚刚对筛选部分进行了更改。现在我只获取字母表,没有任何对post的引用$标题没有被亵渎,这可能是问题吗?谢谢你的回复。所以我必须复制function.php中的第一个代码,然后复制页面上的另一个?我找不到如何回应结果。。你能帮我吗?回音哪一个结果,你需要把第一个代码复制到函数中,第二个复制到你的页面中。使用你的代码,我在所有字母后面得到我所有的帖子。A:我所有的帖子B:我所有的帖子C:我所有的帖子。。。“search\u prod\u title”指的是什么?search\u prod\u title是您传递给查询的自定义参数,请使用已编辑的参数,刚刚对筛选部分进行了更改。现在我只获取字母表,没有任何对post的引用$标题没有被亵渎,这可能是问题吗?@Kushboo谢谢你的回复。我不明白应该在代码中添加哪个while循环。当我使用你的代码时,我会在每篇文章中重复字母表……是的,但是我怎样才能在while循环之外获得$title?@Kushboo谢谢你的回复。我不明白应该在代码中添加哪个while循环。当使用你的代码时,我会在每篇文章中重复字母表…是的,但我如何在while循环之外获得$title?