修剪Wordpress帖子标题

时间:2015-09-15 20:24:39

标签: php wordpress

以下PHP代码在用户前端显示WordPress媒体文件

<?php 
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
echo '<tr><td><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
echo $attachment->_wp_attached_file;
echo '</a>      
</td>
}
}?>

上面的代码显示了前端的完整文件名。如果有人上传sssssssssssssssssssssssssssssssssssssssssssssss.txt,123457xxxxxxxxxxxxxxx.gif 或xls,pdf,docx以及所有其他具有不同长文件名的扩展程序,则会在我的主题上造成混乱。任何人都可以请帮助指导我如何修剪文件名,并在前端显示sssssss ... ssss.txt xxxxxx ... xxxx.pdf或gif之类的东西?

我尝试跟踪函数中的特定文件的函数.php仍然没有变化

$wp_attached_file = 'sssssssssssssssssssssssssssssssssssssssssssss.txt';
echo preg_replace('/(.{3}).*(\..{2,4})/', '$1...$2', $wp_attached_file);

$attachment = $wp_attached_file;
echo preg_replace('/(.{3}).*(\..{2,4})/', '$1...$2', $wp_attached_file);

现在正在使用,我将替换以下行

echo ($attachment->_wp_attached_file);

echo preg_replace('/(.{15}).*(.{5})(\..{2,4})/', '$1...$2$3', $attachment->_wp_attached_file);

1 个答案:

答案 0 :(得分:0)

您可以使用像

这样的正则表达式
TempData["SearchString"] = SearchString;

这会捕获前三个字符string searchString = TempData["SearchString"] as string; 然后忽略一切,/(.{3}).*(\..{2,4})/
在最后一个句点和接下来的2-4个字符之前(对于(.{3}).*ai可以将其过滤到mpeg,如果它始终是3),{{1 }}。 jpg位是2-4个字符串长的任何字符。

PHP用法:

3

输出:

\..{2,4}

Regex101演示:https://regex101.com/r/aR1vJ2/2