[英]Display X Amount of Wordpress Posts Based On Title Length
我在开发的主题的顶部有一个导航下拉菜单。 导航中基本上有两列,其中一列显示检索到的帖子标题(这很容易)。
但是,我想显示帖子标题和指向特定帖子的链接,但是由于该空间限制为大约40个字符,并且每个链接都由管道分隔,因此我需要弄清楚如何显示一定数量的帖子标题以适合在我的角色范围内。
基本上,如果一个帖子标题占用40个字符,那么我不想显示任何其他标题,基本上,我需要将所有帖子标题的长度合并起来,并确定可以显示哪些标题以适应字符数限制。
我的意思是示例,以防万一您还无法理解我要做什么。
社区职位标题| 另一个职位标题
我有以下代码来提取帖子,然后计算标题中的字符总数。 我无法输出应用了字符约束的管道分隔的链接。
/* Fetches all post data from the Wordpress DB */
$fetched_posts = array(
'community' => get_posts('numberposts=3&tag=community'),
'communication' => get_posts('numberposts=3&tag=communication'),
'energy' => get_posts('numberposts=3&tag=energy'),
'health' => get_posts('numberposts=3&tag=health'),
'prosperity' => get_posts('numberposts=3&tag=prosperity'),
'simplicity' => get_posts('numberposts=3&tag=simplicity'),
'materials' => get_posts('numberposts=3&tag=materials'),
'mobility' => get_posts('numberposts=3&tag=mobility'),
'aesthetic' => get_posts('numberposts=3&tag=aesthetic')
);
// Convert all array entries into variables
extract($fetched_posts);
/**
* Show menu items will output items from a particular tagged category
* but only as many that will fit in the navigation menu space.
*
* @param mixed $object
* @param mixed $maximum
*/
function show_menu_items($object, $maximum = 40) {
// Number of elements in the array
$total = 0;
// Total number of characters we've counted
$counted = 0;
// Store all of the titles for this particular object
foreach ($object as $object) {
$post_titles[] = $object->post_title;
}
// Store the total number of elements in the array
$total = count($post_titles);
// For every post title found count the characters
foreach ($post_titles as $post_title) {
if (strlen($post_title) )
$counted = $counted + strlen($post_title);
}
echo $counted;
}
好的,我制定了解决方案。 这并不容易,但是最终找到了基于角色限制来限制帖子数量的方法。 毫无疑问,其他人会发现这个答案有用,这是代码:
<?php
/* Fetches all post data from the Wordpress DB */
$fetched_posts = array(
'community' => get_posts('numberposts=3&tag=community'),
'communication' => get_posts('numberposts=3&tag=communication'),
'energy' => get_posts('numberposts=3&tag=energy'),
'health' => get_posts('numberposts=3&tag=health'),
'prosperity' => get_posts('numberposts=3&tag=prosperity'),
'simplicity' => get_posts('numberposts=3&tag=simplicity'),
'materials' => get_posts('numberposts=3&tag=materials'),
'mobility' => get_posts('numberposts=3&tag=mobility'),
'aesthetic' => get_posts('numberposts=3&tag=aesthetic')
);
// Convert all array entries into variables
extract($fetched_posts);
/**
* Show menu items will output items from a particular tagged category
* but only as many that will fit in the navigation menu space.
*
* @param mixed $object
* @param mixed $maximum
*/
function show_menu_items($object, $maximum = 70) {
// Number of elements in the array
$total = 0;
// Total number of characters we've counted
$counted = 0;
// The counter for number of iterations
$counter = 0;
// Store all of the titles for this particular object
foreach ($object as $object) {
$post_titles[] = $object->post_title;
}
// Store the total number of elements in the array
$total = count($post_titles);
// If we actually have page nav items
if ($total != 0) {
// For every post title found count the characters
foreach ($post_titles as $post_title) {
// Count characters and keep counting for every title
$counted = $counted + strlen($post_title);
// Increment the counterizzle
$counter++;
// If the length is less than or equal to our maximum
if ($counted != $maximum) {
// Display the links
echo '<a href="#'.url_title($post_title, 'dash', TRUE).'">'.$post_title.'</a>';
if ($counter != $total) {
echo ' | ';
}
}
}
} else {
echo 'No for this subject...';
}
}
?>
您可以通过strlen()
获得它。 只要继续在标题上循环并添加下一个标题(只要它仍小于40)即可。
问题是边界情况。 例如,当标题为38个字符长时。 它小于40,因此上述逻辑将加| Next Heading
| Next Heading
将使它超过40个字符。 要解决此问题,您需要先查看下一个标题以了解它是否适合,或者在标题上添加某种缩写,例如, Heading | Ne...ng
Heading | Ne...ng
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.