Products
GG网络技术分享 2025-03-18 16:12 10
I found this code on here that pulls all wordpress pages and displays them in a dropdown list. My question is what needs to be changed to have these pages listed alphabetically?
<form action=\"\"><select>
<?php // Query for listing all pages in the select box loop
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array(
\'post_type\' => \'page\',
\'posts_per_page\' => -1
));
foreach ($all_wp_pages as $value){
$post = get_page($value);
$title = $post->post_title;
$id = $post->ID;
// For example
// <option value=\"pageId32\">Page title</option>
echo \'<option value=\"pageId\' . $id. \'\">\' . $title . \'</option>\';
}; ?>
</select>
URL of above code: Displaying all pages in dropdown
Thanks in Advance! :)
Tracy图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我在这里找到了这个代码,它可以提取所有wordpress页面并在下拉列表中显示它们。 我的问题是需要更改哪些内容按字母顺序列出这些页面?</ p>
&lt; form action =“”&gt; &lt; select&gt;
&lt;?php //查询列出选择框循环中的所有页面
$ my_wp_query = new WP_Query();
$ all_wp_pages = $ my_wp_query-&gt; query(array(
\'post_type\'=&gt;\'page\',
\'posts_per_page\'=&gt; -1
));
foreach($ all_wp_pages as $ value){
$ post = get_page($ value);
$ title = $ post-&gt; post_title;
$ id = $ post-&gt; ID;
//例如
//&lt; option value =“pageId32”&gt;页面标题&lt; / option&gt;
echo\'&lt; option value =“pageId \'。$ id。\'“&gt;\' 。 $ title。 \'&LT; /选项&GT;\';
}; ?&gt;
&lt; / select&gt;
</ code> </ pre>
上述代码的网址:在下拉列表中显示所有页面 </ p>
提前致谢! :)
Tracy </ p>
</ div>
网友观点:
You have to add parameters
\'orderby\' => \'title\',
\'order\' => \'ASC\', // or DESC
Please find updated code.
.
<?php // Query for listing all pages in the select box loop$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array(
\'post_type\' => \'page\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'ASC\', // or DESC
));
foreach ($all_wp_pages as $value){
$post = get_page($value);
$title = $post->post_title;
$id = $post->ID;
// For example
// <option value=\\\"pageId32\\\">Page title</option>
echo \'<option value=\\\"pageId\' . $id. \'\\\">\' . $title . \'</option>\';
}; ?>
</select>
###
You have to add sorting in WP_Query
$all_wp_pages = $my_wp_query->query( array(\'post_type\' => \'page\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
));
###
add orderby
and order
to query array:
$all_wp_pages = $my_wp_query->query( array(\'post_type\' => \'page\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'ASC\', // or DESC
));
Demand feedback