这个问题是关于整理代码和更好地管理所述代码的,但我在PHP方面是一个完全的新手,所以希望得到一点帮助。
我有这个代码:
<?php
$thumb_id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID
$args = array(
'order' => 'ASC',
'orderby' => 'rand',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
'exclude' => $thumb_id
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID, 'full', false);
}
}
?>
它的作用对于参考并不重要,上面的代码从 Wordpress 帖子中随机获取图像,在 DIV 中随机生成其中一个图像。我希望在许多模板中使用此功能,但我不想用它塞满我的 PHP 文件,因为我的文件会变得混乱且效率低下。
2个问题。
- 我是否需要更改上面的代码才能将其放入函数.php?
- 如何使用可以在许多不同的模板中重复使用的短一行引用上面的代码(这将在我的函数.php中)?
您可能想尝试将代码片段放入 functions.php
内部的函数中,并向其传递一些参数,从而使用法更加灵活。
未经测试,但这需要一系列选项,并覆盖默认值(例如,在每次使用的基础上更改order
或orderby
等)。作为可选参数,您可以传递post_id
,以防您要查询不是当前帖子的帖子。
它还returns
数组,而不是直接输出它们,这可以被视为使用函数的首选方式。
// functions.php
function get_random_post_image($options=array(), $post_id=NULL) {
if($post_id != NULL) :
$thumb_id = get_post_thumbnail_id($post_id);
else :
$thumb_id = get_post_thumbnail_id(get_the_ID());
endif;
$default_args = array(
'order' => 'ASC',
'orderby' => 'rand',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
'exclude' => $thumb_id
);
// merge custom options
$args = array_merge($default_args, $options);
$attachments = get_posts($args);
if ($attachments) {
$images = array();
foreach ($attachments as $attachment) {
$images[] = wp_get_attachment_image($attachment->ID, 'full', false);
}
return $images;
}
return false; // or, return default image/placeholder
}
// and within your template/posts:
if(function_exists('get_random_post_image')) :
$images = get_random_post_image(array('order'=>'DESC')); // overwrite `ASC`
if($images) :
foreach($images as $img) {
echo '<div class="post-img"> ' . $img . '</div>';
}
else :
echo 'No images!';
endif;
endif;
不完美;但你可以很容易地扩展它。
1.如果您已经将代码放入这些标签中,则需要取出<?php
并?>
示例函数.php:
<?php
blah blah
....
// your code here
$thumb_id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID
$args = array(
'order' => 'ASC',
'orderby' => 'rand',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
'exclude' => $thumb_id
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID, 'full', false);
}
}
// be careful, only one "?>" in the file, no nested "<?php ?>" blocks
?>
2. 使用 include()、include_once()、require() 或 require_once() 并将函数保存在另一个文件中,以便您可以引用该文件。
random_img.php
<?php
$thumb_id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID
$args = array(
'order' => 'ASC',
'orderby' => 'rand',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
'exclude' => $thumb_id
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID, 'full', false);
}
}
?>
函数.php
<?php
blah blah
....
include ("random_img.php")
?>