这个问题是关于整理代码和更好地管理所述代码的问题,但是对于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个问题。
答案 0 :(得分:1)
您可以尝试将代码段放入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 :(得分:0)
<强> 1。如果您已将代码放入这些代码中,则需要退出<?php
和?>
示例functions.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);
}
}
?>
的functions.php
<?php
blah blah
....
include ("random_img.php")
?>