我试图在wordpress(主页和单页)标题上方显示图像(类coreimg
)coreimg
尺寸:固定(例如:680 x 240)
我知道我可以做一些css技巧通过相对对齐它们,但我想要一个更复杂的方法。我也尝试了westondeboer的get_first_image()
方法。但是负值是-
- 无法获取特定图像(
coreimg
类),必须是第一张。 - 很难插入图像自己的
alt
或title
。
那么我要做的就是创建一个wp短代码,大概是这样的:
[coreimg src="http://example.org/wp-content/uploads/2010/06/fly.jpg" title="The flying kite"]
或者甚至下面的代码都可以:
<img src="http://example.org/wp-content/uploads/2010/06/fly.jpg" title="The flying kite" alt="The flying kite" class="coreimg"/>
Update:问题只是通过php获取图像。但是我做不到,所以我重新开始,希望代码能有一个好的开始。更新2:single.php
<h1 class="entry-title"><?php the_title();?></h1><?php the_content(); ?>
新代码(应该是这样):
<img src="<?php get_coreimg_url(); ?>" title="<?php get_coreimg_title(); ?>" alt="<?php get_coreimg_title(); ?>" class="headcoreimg"/>
<h1 class="entry-title"><?php the_title();?></h1><?php the_content(); ?>
现在我要get_coreimg_url()
和get_coreimg_title()
的代码为functions.php
您可以修改get_first_image()中的代码,以查找具有该类的标记,而不仅仅是第一张图像。您必须更改正则表达式以查找class=" coreging "或其他内容。
终于得到了答案。(:
谢谢大家的帮助。
single.php
和/或index.php
(默认主题)中删除默认的<h1 class="entry-title"><?php the_title();?></h1>
在functions.php
中增加如下功能:
function coreimg($atts) {
// Just add the below shortcode in the post.
// [cover src='http://localhost/wordpress/wp-content/uploads/2010/06/flyingkite.jpg' title='A flying Kite']
global $post;
extract(shortcode_atts(array('src' => '', 'title' => '' ), $atts));
if($src == '') {
$src = get_bloginfo('template_directory') . '/rotate.php';
}
if ($title == '') {
$title = "{$post->post_title}";
}
$coverup = '<a href="';
$coverup .= get_permalink($post->ID);
$coverup .= '" ' . "alt='{$title}' title='{$title}' class='corecover'><img src='";
$coverup .= "{$src}' class='cocover' alt='{$title}' title='{$title}' /></a>";
$before = '<h1 class="entry-title"><a href="' . get_permalink($post->ID) .'" title="' . "{$post->post_title}'">";
$after = '</a></h1>';
$output = "{$coverup}{$before}{$post->post_title}{$after}";
return $output;
}
add_shortcode('cover', 'coreimg');
您可以从alistapart中获取random.php
文件。
它做什么(特殊)?
如果默认情况下你没有为文章指定任何特定的图像,它会随机旋转标题图像,你可以在文件夹中上传(并在random.php
中指定). 它的简短代码可能看起来像[cover title="The flying kite"]
或[cover]
。完整的url短代码可以输入[cover src="../mykite.png"]
或[cover src="../mykite.png" title="Hey, look a kite!"]