【问题标题】:Display X Amount of Wordpress Posts Based On Title Length根据标题长度显示 X 数量的 Wordpress 帖子
【发布时间】:2010-11-03 02:04:27
【问题描述】:

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

然而,我想显示帖子标题和特定帖子的链接,但由于空间限制为大约 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 arrays wordpress


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你有它与strlen()。只要继续循环你的标题并附加下一个标题,只要它仍然小于 40。

      问题是边界情况。例如,当标题长度为 38 个字符时。它小于 40,因此上面的逻辑将添加 | Next Heading,这将使其超过 40 个字符。要解决此问题,您需要先查看下一个标题以查看它是否合适,或者为您的标题添加某种缩短,即Heading | Ne...ng

      【讨论】:

      • 我明白你的意思,杰森。你有任何示例代码可以提供给我来帮助我吗?一段时间以来,我一直在努力解决这个问题。截断并不是一个真正的选择,我只想显示任何数量的标题,只要所有帖子标题的总字符数不会超过最大值。
      猜你喜欢
      • 2013-10-30
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      相关资源
      最近更新 更多