Php 根据标题长度显示X数量的Wordpress文章

Php 根据标题长度显示X数量的Wordpress文章,php,arrays,wordpress,Php,Arrays,Wordpress,在我开发的主题顶部有一个导航下拉列表。基本上,导航中有两列,其中一列显示检索到的文章标题(这很容易) 然而,我想显示文章标题和特定文章的链接,但因为空间限制在40个字符左右,每个链接由管道分隔,我需要找出如何显示一定数量的文章标题,以符合我的字符限制 基本上,如果一篇文章的标题占了40个字符,那么我不想显示任何其他标题,基本上我需要将所有文章标题的长度合并起来,并计算出哪些标题可以显示以符合字符限制 我的意思示例,以防你还不明白我想做什么 社区帖子标题|另一个帖子标题 我有下面的代码,它可以拉帖

在我开发的主题顶部有一个导航下拉列表。基本上,导航中有两列,其中一列显示检索到的文章标题(这很容易)

然而,我想显示文章标题和特定文章的链接,但因为空间限制在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;

 }

您可以通过
strlen()
使用它。只要继续循环你的标题,并附加下一个标题,只要它仍然少于40


问题是边界情况。例如,当标题长度为38个字符时。它小于40,因此上面的逻辑将添加
|下一个标题
,这将使它远远超过40个字符。要解决这个问题,您需要先查看下一个标题是否合适,或者在标题中添加一些缩写,例如,
标题| Ne…ng

好的,我找到了解决方案。这并不容易,但最终解决了如何根据角色限制限制帖子数量的问题。毫无疑问,其他人会发现这个答案很有帮助,下面是代码:

<?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...';
    }


 }

?>

我有点明白你的意思,杰森。你有什么例子代码可以提供给我帮助我吗?我已经有一段时间了。截短并不是一个真正的选项,我只想显示任何数量的标题,只要所有文章标题的字符总数不超过最大值。