Php 带有链接到帖子的缩略图的摘录
我是wordpress和PHP的新手,刚刚从blogger转换过来。我希望有人能在这里帮助我,因为我一直在网上寻找,但没有真正找到我想要的解决方案 我想知道是否有办法让摘录显示文章中的第一张图片,然后将图片链接到实际的文章?我已经尝试过用特色图片的变化,但要么我在摘录中得到两张图片,要么在帖子中没有图片 如果有人知道一个代码可以做到这一切,我会很高兴:) 这是我的索引文件:Php 带有链接到帖子的缩略图的摘录,php,wordpress,image,function,post,Php,Wordpress,Image,Function,Post,我是wordpress和PHP的新手,刚刚从blogger转换过来。我希望有人能在这里帮助我,因为我一直在网上寻找,但没有真正找到我想要的解决方案 我想知道是否有办法让摘录显示文章中的第一张图片,然后将图片链接到实际的文章?我已经尝试过用特色图片的变化,但要么我在摘录中得到两张图片,要么在帖子中没有图片 如果有人知道一个代码可以做到这一切,我会很高兴:) 这是我的索引文件: <?php /** * Displays the index section of the theme.
<?php
/**
* Displays the index section of the theme.
*
* @package Theme Horse
* @subpackage Clean_Retina
* @since Clean Retina 1.0
*/
?>
<?php get_header(); ?>
<?php
/**
* cleanretina_before_main_container hook
*/
do_action( 'cleanretina_before_main_container' );
?>
<div id="container">
<?php
/**
* cleanretina_main_container hook
*
* HOOKED_FUNCTION_NAME PRIORITY
*
* cleanretina_content 10
*/
do_action( 'cleanretina_main_container' );
?>
</div><!-- #container -->
<?php
/**
* cleanretina_after_main_container hook
*/
do_action( 'cleanretina_after_main_container' );
?>
<?php get_footer(); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<p><?php echo get_new_excerpt(); ?></p>
<?php endwhile;endif;wp_reset_query(); ?>
据我所知,您希望文章中的第一张图片进入摘录。所以我有一个更好的解决方案,为什么不在functions.php文件中创建另一个函数,其中包含与原始摘录连接的图像
添加到functions.php
function get_new_excerpt(){
$str = '';
$img = '<img src="'.catch_that_image().'">';
$linkedImage = '<a href="'.get_permalink().'" title="'.get_the_title().'">'.$img.'</a>';
$str .= '<div class="post-image">'.$linkedImage.'</div>';
$str .= '<div class="post-excerpt">'.get_the_excerpt().'</div>';
return $str;
}
// Catch first Image from http://css-tricks.com/
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)) {
$first_img = "/path/to/default.png";
}
return $first_img;
}
函数get_new_extract(){
$str='';
$img='';
$linkedImage='';
$str.=''.$linkedImage';
$str.=''。获取摘录();
返回$str;
}
//从中捕获第一个图像http://css-tricks.com/
函数catch_that_image(){
全球$员额,$员额;
$first_img='';
ob_start();
ob_end_clean();
$output=preg_match_all('//i',$post->post_content,$matches);
$first_img=$matches[1][0];
如果(空($first_img)){
$first_img=“/path/to/default.png”;
}
返回$first\u img;
}
然后在index.php或任何想要显示此函数的页面中,插入:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="post">
<h1 class="post-header"><?php the_title(); ?></h1>
<div class="post-info">
<?php echo get_new_excerpt(); ?>
</div>
</div>
<?php endwhile;endif;wp_reset_query(); ?>
您可能需要添加一些css样式,如:
<style>
.post {
clear: both;
background-color: #fff;
border: 1px solid #ccc;
overflow: hidden;
}
.post h1.post-header {
border-bottom: 1px solid #ccc;
}
.post .post-info {
clear: both;
overflow: hidden;
}
.post .post-image {
float: left;
width: 100px;
height: 100px;
background-color: #EFEFEF;
}
.post .post-image img {
width: 100%;
height: 100%;
}
.post .post-excerpt {
float: right;
width: calc(100% - 120px);
text-align: left;
}
</style>
.邮政{
明确:两者皆有;
背景色:#fff;
边框:1px实心#ccc;
溢出:隐藏;
}
.post h1.post-header{
边框底部:1px实心#ccc;
}
.post.post信息{
明确:两者皆有;
溢出:隐藏;
}
.post.post图像{
浮动:左;
宽度:100px;
高度:100px;
背景色:#EFEF;
}
.post.post图像img{
宽度:100%;
身高:100%;
}
.post.post摘录{
浮动:对;
宽度:计算(100%-120px);
文本对齐:左对齐;
}
我希望我已经理解你了。我希望它能有所帮助。请添加您已经拥有的代码,以及您尝试过的代码。谢谢,这样可以获得摘录中的第一幅图像。然而,我也希望摘录中的这张图片能链接到全文。可能吗?@partiety是的,对不起我忘了,我现在已经更新了。不,对不起,那不行。我得到的只是摘录,没有图片,然后在主页底部我得到了链接到帖子的图片链接。我使用干净的视网膜作为模板,但我试图改变它。现在我没有激活插件,也没有其他代码。。。。如果这些信息对我有帮助的话…@我刚刚解决了一个小的语法问题,现在我测试了它,它可以工作了。我的wordpress版本是3.8.1,我知道我一定做错了什么。我把代码放在index.php中,但仍然只得到了摘录下面页面下方的完整文章。在index.php文件中是否有我应该放置此文件的特定位置?我还有wordpress 3.8.1。