简体   繁体   English

如何将图片网址设置为精选图片?

[英]How Set Image URL As Featured Image?

I have article with title "POST-TITLE" and I create image title same with post title, "POST-TITLE.jpg". 我有标题为“ POST-TITLE”的文章,并创建与标题为“ POST-TITLE.jpg”的图片标题相同的图片。

All image I put in my hosting, like this: 我放入托管中的所有图像,如下所示:

http://www.example.com/img/CATEGORY/POST-TITLE.jpg

In mytheme post-single.php, I have this code: 在mytheme post-single.php中,我有以下代码:

<img src="http://www.example.com/img/<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ''; } ?>/<?php the_title(); ?>.jpg" onError="this.src='http://cerpenkoran.net/img/dot.jpg';" title="Thumb for <?php the_title(); ?>" >

My answer: 我的答案:

What code I add the image url code above, so it as Featured Image? 我要在上面添加图片网址代码的什么代码,将其作为“特色图片”?

OR how to combine this fix URL: 或如何结合此修复URL:

http://www.example.com/img/<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ''; } ?>/<?php the_title(); ?>.jpg

with this Featuread Image code: 使用此Featuread图片代码:

<?php if (has_post_thumbnail( $post->ID )) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" />
<?php endif; ?>

Are you asking how to add a Featured Image to your WordPress website? 您是否在问如何在WordPress网站上添加特色图片? If so, then follow the below instructions: 如果是这样,请按照以下说明进行操作:

  1. Firstly, you are going to need to add the relevant Theme Support inside your functions.php file. 首先,您需要在functions.php文件中添加相关的主题支持。 To achieve this, go into your functions.php file and enter the following code: 为此,进入您的functions.php文件并输入以下代码:

    <?php function my_theme_name_support(){ add_theme_support('post-thumbnails'); } add_action('after_setup_theme', 'my_theme_name_support'); ?>

  2. Make sure that the above code is placed after the opening <?php tag (at the top of the functions.php file) but before the closing ?> tag (at the bottom of the functions.php file.) 确保将上面的代码放置在打开的<?php标记之后(在functions.php文件的顶部),但在闭合的?>标记之前(位于functions.php文件的底部)。

  3. You will see that there are two my_theme_name_support entries. 您将看到有两个my_theme_name_support条目。 You can change the wording of these to something more appropriate. 您可以将这些措辞更改为更合适的内容。 Just make sure they match. 只要确保它们匹配即可。

  4. Now, head into your WordPress Dashboard and go to create a New Page or New Post. 现在,进入您的WordPress仪表板,然后创建一个New Page或New Post。 You should now be able to see an entry, entitled 'Featured Image'. 现在,您应该可以看到名为“特色图像”的条目。 As you scroll the page, you should see it appear on the right hand side. 滚动页面时,您应该看到它出现在右侧。 Simply select the link and add the required image. 只需选择链接并添加所需的图像。

  5. This, alone, does not output the Featured Image. 仅此一项就不会输出特色图像。 To output your Featured Image, you now need to enter the following code into the relevant file(s) on your server: 要输出特色图像,现在需要在服务器上的相关文件中输入以下代码:

    <?php the_post_thumbnail('thumbnail'); ?>

By following the above steps, you should now be able to dynamically call your Featured Images from your WordPress Dashboard and output them to whichever pages you want. 通过执行上述步骤,您现在应该可以从WordPress仪表板动态调用“特色图片”并将其输出到所需的任何页面。

For more information, on Featured Images, check out this WordPress Codex Article >>> 有关更多信息,请参见“特色图片”上的WordPress Codex文章>>>

Answer Extension 答案扩展

If you want the same image to appear for every Blog Post, so that you do not have to manually add a Featured Image to every Blog Post, you could: 如果您希望每个博客文章显示相同的图像,从而不必为每个博客文章手动添加特色图像,则可以:

  1. Upload the image to WordPress, via their Media Uploader. 通过他们的媒体上传器将图片上传到WordPress。
  2. Then head to the single.php Blog Post Template File and enter the following code: 然后转到single.php博客文章模板文件,并输入以下代码:

    <img src="POST-TITLE.jpg" alt="post-title" height="42" width="42">

Be sure to make the following modifications, to the above code: 确保对以上代码进行以下修改:

src: This is the link to the actual image. src:这是指向实际图像的链接。 Therefore, change 'POST-TITLE.jpg' to the name of your desired image. 因此,将“ POST-TITLE.jpg”更改为所需图像的名称。 Be sure to include the correct image format (.jpg, .png etc). 确保包括正确的图像格式(.jpg,.png等)。

alt: This is a brief description of the image. alt:这是图像的简短描述。 No coding required here. 此处无需编码。 Plain text will do. 纯文本即可。

height and width: Insert the correct dimensions. 高度和宽度:插入正确的尺寸。 The dimensions are in pixels. 尺寸以像素为单位。

Please Note ... 请注意 ...

single.php is the default Blog Post Template File. single.php是默认的博客文章模板文件。 There is a chance that there are additional Blog Post Template files too. 也有可能还有其他博客文章模板文件。 If this is the case, you will need to add the code to these other Template Files too. 在这种情况下,您也需要将代码添加到其他模板文件中。

You may find this WordPress Article , helpful, when working with Template Files. 使用模板文件时,您可能会发现此WordPress文章很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM