1.wordpress的get
wordpress可以设置自定义字段,方便扩展功能,wordpress利用巧妙的数据库表设计达到这一目的,posts表存放文章,页面和附件等,与之对应的postmeta表用来存储自定义的字段,采用post_id,key,value这样的设计来存放自定义字段的值。
get_post_meta函数用法:
get_post_meta($post_id, $key, $single);
该函数有3个基本参数:
$post_id —— 所检索数据的文章的ID,使用 $post->ID 来获取文章的ID。
$key —— 要检索的自定义字段名称
$single —— 这是一个布尔值,如果设置为 true ,将直接以字符串的形式返回字段的值;一个自定义字段可以填写多个值,如果设置为 false,将返回一个数组 array 来显示这多个值。
此函数定义在wordpress的post.php中:
2.wordpress在不破坏css的情况下,指定id调用最新文章(文章显示数量
显示循环部分代码
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<div class="col col_2">
<img src="<?php if ( get_post_meta($post->ID, 'thumbnail', true) ) {echo get_post_meta($post->ID, 'thumbnail', true);}elseif ( has_post_thumbnail() ){$thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail'); echo $thumbnail_image_url[0]; }else{ echo catch_first_image(); }?>" alt="<?php the_title(); ?>" class="image_frame" />
</div>
<div class="col col_2 no_margin_righ">
<h2><?php the_title(); ?></h2>
<p><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); ?></p>
<a href="<?php the_permalink(); ?>" class="more">More</a>
</div>
<div class="cleaner"></div>
</div>
<?php endwhile; endif; ?>
主题functions.php添加特色图代码和获取文章第一张图的代码
/*自定义缩略图*/
if(function_exists('add_image_size')){
add_image_size('featured',300,200,true); //尺寸自己修改
add_image_size('thumbnail',195,195,true); //尺寸自己修改,这是后面用到的
}
/*抓取第一张缩略图*/
functioncatch_first_image(){
global$post,$posts;
$first_img='';
ob_start();
ob_end_clean();
$output=preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',$post->post_content,$matches);
$first_img = $matches[1][0];
if(empty($first_img)){//自定义第一张图片
$random = mt_rand(1,6);
$first_img = get_bloginfo('stylesheet_directory')."/images/random/".$random.".jpg"; //如文章无图片,则随机显示6张图片中一张,文件夹目录注意下images/random
}
return $first_img;
}
以上只是参考。
3.wordpress调用文章内图片
在后台看下,文章是不是有设置特色图像?如果有,可用the_post_thumbnail()获取。
如果没有设置,可用get_attached_media()获取当前日志下所有附件,代码如: //the_content(); $attachments = get_attached_media( 'image' ); if ( $attachments ) { foreach ($attachments as $key => $attachment ) { wp_get_attachment_image( $attachment->ID ); } }。
4.wordpress如何自动提取文章第一张图片作为特色图片
方法一: 在循环列表中使用的话,这段代码直接放在循环列表中; <?php $soimages='~]*\ />~' ;="" preg_match_all($soimages,$post-="">post_content,$Images); $PictureAmount= 1;//一张, for($i=0;$i<$pictureamount;$i++){ echo="" ''.$images[0][$i].'';="" }?="">方法二: 在functions.php添加如下代码: <?php 调用缩略图function="" emtx_auto_thumbnail($pid,$thumb='thumbnail' )="" {$blogimg="FALSE;" if="" (has_post_thumbnail())="" {//="" 判断该文章是否已经设置了"特色图像",如果有则直接显示该特色图像的缩略图="" $blogimg="wp_get_attachment_image_src(get_post_thumbnail_id($pID),$thumb);" $blogimg="$blogimg[0];" }="" elseif="" ($postimages="get_children("post_parent=$pID&post_type=attachment&post_mime_type=image&numberposts=0"))" {//如果文章没有设置特色图像,则查找文章内是否有上传图片="" foreach($postimages="" as="" $postimage)="" {="" $blogimg="wp_get_attachment_image_src($postimage-">ID, $thumb); $blogimg = $blogimg[0]; } } elseif (preg_match('/]*src=["|\']([^"|\']+)/i', get_the_content(), $match) != FALSE) { $blogimg = $match[1]; } if($blogimg) {$blogimg = '';} return $blogimg; }?>调用时如下代码: <?php $pid="get_the_ID();" echo="" emtx_auto_thumbnail($pid,$thumb='thumbnail' );="" ?="">注意:所有的代码都在<?php ?=""> 中运行,如果外层已经包含了<?php ?="">,就把此去掉。
方法二的代码更强大写,如果文章设置了缩略图就显示缩略图,如果没有设置就抽取第一张为缩略图。
5.wordpress在哪里添加熊掌号ID声明代码
WordPress添加熊掌号的json_id结构化数据代码如下:
PHP代码,用于获取文章摘要内容,代码添加在当前主题functions.php中
<?php//获取文章/页面摘要
function fanly_excerpt($len=220){
if ( is_single() || is_page() ){
global $post;
if ($post->post_excerpt) {
$excerpt = $post->post_excerpt;
} else {
if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){
$post_content = $result['1'];
} else {
$post_content_r = explode("\n",trim(strip_tags($post->post_content)));
$post_content = $post_content_r['0'];
}
$excerpt = preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$post_content);
}
return str_replace(array("\r\n", "\r", "\n"), "", $excerpt);
}}?>json_id数据,添加在head头部中:
<script type="application/ld+json">{
"@context": "/contexts/cambrian.jsonld",
"@id": "<?php the_permalink(); ?>",
"appid": "这里请填写熊掌号ID",
"title": "<?php the_title(); ?>",
"images": ["<?php echo catch_that_image() ?>"],
"description": "<?php echo fanly_excerpt()?>",
"pubDate": "<?php echo get_the_time('Y-m-d\TH:i:s')?>",
}</script>appid就是熊掌号ID,自己修改一下即可。
以上代码来源于泪雪博客:网页链接 版权归属原作者
6.wordpress如何调用单篇文章里面的所有附件图片
一个文章的所有附件,是通过POSTS数据表中的post_parent挂钩的,以及post_type为attachment来标识的。
理解这句后,查看下面这段代码:
/* 获取指定post下的所有图片附件. */
$attachments = get_children(
array(
'post_parent' =>; 指定日志的ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'suppress_filters' => true
)
);接下来的工作就是foreach读出附件的相关信息即可。
7.wordpress调用文章内图片
在后台看下,文章是不是有设置特色图像?如果有,可用the_post_thumbnail()获取。
如果没有设置,可用get_attached_media()获取当前日志下所有附件,代码如:
//the_content();
$attachments = get_attached_media( 'image' );
if ( $attachments ) {
foreach ($attachments as $key => $attachment ) {
wp_get_attachment_image( $attachment->ID );
}
}
转载请注明出处51数据库 » wordpress$image->id
?php>?php>?php>?php>$pictureamount;$i++){>?php>