下面的函数将给定目录中的所有文件夹返回到多个级别.
我只需要一个级别的深度,只是目标目录中的文件夹,没有子文件夹.
该函数还返回文件夹的完整路径,我只想要文件夹名称.我确定我错过了一些简单的事情.
如何修改函数以仅返回给定目录的文件夹名称?(不是每个文件夹的完整路径)
$ myArray = get_dirs('../ wp-content/themes/mytheme/images');
<?php
function get_dirs( $path = '.' ){
return glob(
'{' .
$path . '/*,' . # Current Dir
$path . '/*/*,' . # One Level Down
$path . '/*/*/*' . # Two Levels Down, etc.
'}', GLOB_BRACE + GLOB_ONLYDIR );
}
?>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,感谢Doug对原始功能的帮助!
我正在运行 PHP 版本 5.2.11
当我运行下面的函数时:
function get_dirs($path = '.') {
$dirs = array();
foreach (new DirectoryIterator($path) as $file) { // This is line 462.
if ($file->isDir() && !$file->isDot()) {
$dirs[] = $file->getFilename();
}
}
return $dirs;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Fatal error: Uncaught exception 'RuntimeException' with message 'DirectoryIterator::__construct(/home/test/test.com/wp-content/themes/mytheme/images) [<a href='directoryiterator.--construct'>directoryiterator.--construct</a>]: failed to open dir: No such file or directory' in /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php:462 Stack trace: #0 /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php(462): DirectoryIterator->__construct('/home/test/wie...') #1 /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php(31): get_dirs('/home/test/wie...') #2 /home/test/test.com/testing123/wp-settings.php(717): include('/home/test/wie...') #3 /home/test/test.com/testing123/wp-config.php(76): require_once('/home/test/wie...') #4 /home/test/test.com/testing123/wp-load.php(30): require_once('/home/test/wie...') #5 /home/test/test.com/testing123 in /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php …Run Code Online (Sandbox Code Playgroud) 我有一个函数,其目的是列出我的wordpress主题的images目录中的文件夹.当用户在站点的根目录中安装了wordpress时,它非常有用.但是,如果用户在根目录下的文件夹中安装了wordpress,则会失败...
$ mydir = get_dirs($ _ SERVER ['DOCUMENT_ROOT'].'/ wp-content/themes/mytheme/images');
只要将wordpress直接安装到站点的根目录(例如site.com/index.php),此功能就可以正常工作
但是,如果用户已将其博客安装在虚拟目录中,则此路径方法将失败.示例:site.com/somedirectory/index.php
有人可以用更好的方法来确定$ mydir的路径吗?
如果我可以用http地址提供我的函数,那将是非常简单的,但我的函数需要将images目录的路径作为服务器目录.有人知道使用http方法在给定目录下查找文件夹的方法吗?例如,我可以使用...获取对主题图像目录的引用
$ mydir = get_bloginfo('url').'/ wp-content/themes/mytheme/images';
但是,下面的函数不会占用URL,它必须是我要解析的所选目录的物理文件路径.所以我需要用一个带有URL的函数替换它,或者我需要找到一些方法将$ mydir函数转换为物理路径,或者其他一些方法.
这是我用于解析目录并将每个文件夹分配给数组的函数.
function get_dirs($path = '.')
{
$dirs = array();
try
{
foreach (new DirectoryIterator($path) as $file)
{
if ($file->isDir() && !$file->isDot())
{
$dirs[] = $file->getFilename();
}
}
}
catch(Exception $e)
{
//log exception or process silently
//just for test
echo "There is a problem inside the get_dirs function";
}
return $dirs;
}
Run Code Online (Sandbox Code Playgroud) 需要一个专门用于获取符合条件的帖子数量的函数调用.我相信get_posts()函数对于此操作来说太昂贵了.我只是想确定是否在显示预定数量的帖子时显示"查看更多帖子"链接...
例如,要显示的默认帖子链接数为3.如果帖子总数超过3,我只想显示"查看更多帖子"链接.
这是我的代码......
$cat1=get_cat_ID('test');
$cat2=get_cat_ID('test2');
$myposts = get_posts(array('cat' => "$cat1,-$cat2",'showposts' => 3));
$myposts2 = get_posts(array('cat' => "$cat1,-$cat2"));
$mypostcount = count($myposts2);
foreach($myposts as $idx=>$post)
?>
<li><a>Post Info Goes here</a></li>
<?php
if ($mypostscount > 3){ ?>Show View All Link<?php ?>
Run Code Online (Sandbox Code Playgroud) 下面的语句将加载所有不以下划线字符开头的图像...
if (!is_dir($file) && preg_match("/^[^_].*\.(bmp|jpeg|gif|png|jpg)$/i", $file))
Run Code Online (Sandbox Code Playgroud)
我需要修改它,以便它只加载带有下划线的DO BEGIN图像.
默认的WordPress类别窗口小部件不允许排除命名类别.
我创建了一个插件,它创建了一个自定义类别小部件到"可用小部件"列表,这使我可以控制我想要排除的项目.代码如下......
<?php
/*
Plugin Name: Custom Categories Widget
Plugin URI: http://mysite.com
Description: Removes the Specified Categories from the Default Categories Listing
Author: Me
Version: 1.0
Author URI: http://mysite.com
*/
function widget_my_categories()
{
wp_list_categories('exclude=1');
}
function my_categories_init()
{
register_sidebar_widget(__('Custom Categories Widget'), 'widget_my_categories');
}
add_action("plugins_loaded", "my_categories_init");
?>
Run Code Online (Sandbox Code Playgroud)
但是,我希望生成的代码模仿与默认类别窗口小部件相同的外观(即,"类别"一词在我的窗口小部件中显示为子弹,但在默认类别窗口小部件中显示为h4级别标题元素.我想要与默认类别窗口小部件相同的结构应用于我的自定义窗口小部件.
我还想为用户提供指定类别列表标题的选项(就像他们在默认类别小部件中可以做的那样).
顺便说一句,我正在使用id 1,它是默认的"未分类"类别,并将项目分配给我不希望出现在列表中的该类别.
任何帮助非常感谢!:)
我在下面的test.php文件代码中调用jQuery"GET".
我正在尝试让脚本在生成的test.ini文件上弹出"另存为"对话框,以允许它在本地保存.但是,虽然我可以将结果回显给jQuery,但我似乎无法弹出"另存为"对话框.
更新:感谢下面的解决方案,我刚刚将$ .get更改为window.location.replace.
$('#test').click(
function()
{
//$.get('<?php echo get_bloginfo('template_directory') ?>/test.php');
window.location.replace("<?php echo get_bloginfo('template_directory') ?>/test.php");
}
);
Run Code Online (Sandbox Code Playgroud) 我只是在寻找一个简单的javascript,它可以检查jQuery库是否已经加载,如果没有,则加载它.
如果您有解决方案,请提前感谢.
在下面的代码中,我将向下数组(通过生成的内容)添加到具有子菜单的顶级元素.我正在向带有子菜单的子菜单元素添加右箭头.
但是,我需要一些帮助我的css,因为如果父母有孩子,它会将箭头应用于所有子元素.我只需将它应用于那些有孩子的元素.
<ul id="menu-site-menu">
<li class="hasChild top"><a href="#">About Us</a>
<ul class="sub-menu">
<li><a href="#">Our Charity Partners</a></li>
<li><a href="#">Privacy Policy</a></li>
</ul>
</li>
<li class="hasChild top"><a href="#">Buy Apparel</a>
<ul class="sub-menu">
<li class="hasChild sub"><a href="#">Benevolent Elephant</a>
<ul class="sub-menu">
<li><a href="#">Benevolent Elephant Short Sleeve</a></li>
<li><a href="#">Benevolent Elephant Long Sleeve</a></li>
</ul>
</li>
<li class="hasChild sub"><a href="#">Eagle-Spirit</a>
<ul class="sub-menu">
<li><a href="#">Eagle-Spirit Short Sleeve</a></li>
<li><a href="#">Eagle-Spirit Long Sleeve T-Shirts</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Customer Service</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
css在下面
.hasChild.top a:after {
content: ' ';
height: …Run Code Online (Sandbox Code Playgroud) 我的标记看起来像这样:
<ul class="menu collapsible">
<li class='expand sectionTitle home'>
<a class='clicker' href='#'>Home</a>
<ul class='acitem'>
<li class="section">stuff goes here
</ul>
</li>
<li class='expand sectionTitle section1'>
<a class='clicker' href='#'>Section 1</a>
<ul class='acitem'>
<li class="section">stuff goes here
</ul>
</li>
<li class='expand sectionTitle section2'>
<a class='clicker' href='#'>Section 2</a>
<ul class='acitem'>
<li class="section">stuff goes here
</ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我想添加处理手风琴行为的jQuery脚本(见下文),以便在单击"clicker"元素时,其父div滑动到第二个位置.我总是希望顶部的"Home"div,但我希望在第二个位置扩展哪个div"活跃".
这是控制菜单的手风琴行为的jQuery:
jQuery.fn.initMenu = function() {
return this.each(function(){
var theMenu = $(this).get(0);
$('.acitem', this).hide();
$('li.expand > .acitem', this).show();
$('li.expand > .acitem', this).prev().addClass('active');
$('li a', this).click(
function() {
var …Run Code Online (Sandbox Code Playgroud)