美文网首页
wordpress文章的各种调用

wordpress文章的各种调用

作者: 八点两刻 | 来源:发表于2019-11-29 11:08 被阅读0次

调用文章标题:<?php the_title(); ?>

调用文章内容:<?php the_content(); ?>

调用文章摘要:<?php the_excerpt(); ?>

调用作者姓名:<?php the_author(); ?>

调用文章发布时间:<?php the_time(); ?>

调用作者的Gravatar头像:<?php echo get_avatar( get_the_author_email(), 36 ); ?>

调用特色图片:<?php the_post_thumbnail();?>

调用固定链接:<?php the_permalink(); ?>
the_post_thumbnail(); // 默认显示缩略图
the_post_thumbnail(‘thumbnail’); // 显示缩略图
the_post_thumbnail(‘medium’); // 显示中等尺寸
the_post_thumbnail(‘large’); // 显示大尺寸
the_post_thumbnail( array(200,200) ); // 自定义尺寸
对特色图像大小进行设置
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
if(function_exists('set_post_thumbnail_size')){
set_post_thumbnail_size(231,160,true);
}
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'big', 432, 234, true );
add_image_size( 'car', 179, 109, true );
}

调用文章的第一幅图
在functions.php中加入代码:

<?php
function catch_that_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)){ //Defines a default image

    $first_img = "/images/default.jpg";

  }

  return $first_img;
}

?>
在要调用图片的地方加入代码:
<img src="<?php echo catch_that_image() ?>" />

相关文章

网友评论

      本文标题:wordpress文章的各种调用

      本文链接:https://www.haomeiwen.com/subject/ixbwwctx.html