Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php wordpress:创建一个条件语句以加载特征图像(如果它存在)或默认图像_Php_Wordpress_Conditional - Fatal编程技术网

Php wordpress:创建一个条件语句以加载特征图像(如果它存在)或默认图像

Php wordpress:创建一个条件语句以加载特征图像(如果它存在)或默认图像,php,wordpress,conditional,Php,Wordpress,Conditional,我在这里搜索了与我类似的问题,但没有找到一条能帮助我的线索。。虽然我确实在这里找到了帮助,使我达到了现在的水平;) 我创建了一个jquery幻灯片,它从某个帖子类别中的特色图片中提取图像。我在这里找到了下面的代码,目前为止,它满足了我的需要,但我需要添加一个条件语句: if(has_post_thumbnail()) { $showImg = wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'xiao-sh

我在这里搜索了与我类似的问题,但没有找到一条能帮助我的线索。。虽然我确实在这里找到了帮助,使我达到了现在的水平;)

我创建了一个jquery幻灯片,它从某个帖子类别中的特色图片中提取图像。我在这里找到了下面的代码,目前为止,它满足了我的需要,但我需要添加一个条件语句:

if(has_post_thumbnail()) {
$showImg = wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'xiao-show-img' );
} ?>

<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>">
    <img src="<?php echo $showImg[0]; ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>" />
</a>
if(具有\u post\u缩略图()){
$showImg=wp\u get\u attachment\u image\u src(get\u post\u缩略图\u id($post->id),'show img');
} ?>
这样行,但是。。如果文章中没有特色图片,我想使用下面的方法将默认图片加载到幻灯片中:

<?php
if(has_post_thumbnail()) { ?>
    <a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>">
        <img src="<?php the_post_thumbnail() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>" />
    </a> 
 <?php
    } else { 
 ?>
    <a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>">
        <img src="<?php get_bloginfo("template_url"); ?>/images/xiao-default-image.png" />;
            </a>
    <?php }
 ?>


我意识到下面的示例不起作用。

更简单的方法-只需更改该图像的样式以包含
背景图像:url('images/xiao=default image.png')宽度:
高度:
。然后,您不需要使用
if/else
语句,还可以将
$showImg
更改为包含
src
,并为图像提供一个自定义类:

if(has_post_thumbnail()) {
   $showImg = 'src="' . wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'xiao-show-img' ) . '"';
} ?>

<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>">
    <img class="usedefault" <?php echo $showImg[0]; ?> title="<?php the_title() ?>" rel="<?php the_excerpt() ?>" />
</a>

我认为您代码中的罪魁祸首是
src=”“

由于_post_thumbnail()将回显整个图像标记所需的html,因此请尝试以下操作

<?php if(has_post_thumbnail()): ?>
    <a href="<?php the_permalink() ?>" title="<?php the_title() ?>">
        <?php the_post_thumbnail() ?>
    </a> 
<?php else: ?>
    <a href="<?php the_permalink() ?>" title="<?php the_title() ?>">
        <img src="<?php get_bloginfo("template_url"); ?>/images/xiao-default-image.png" />;
    </a>
<?php endif ?>


为什么您的示例不起作用?你到底在问什么?嗨,亚当,谢谢你这么快就登记了。我不知道为什么下面的例子不起作用。我希望它会,只是需要一个正确的方向来推动它的工作,或者如果我认为这样做是完全错误的。我刚刚看到我遗漏了一个结束锚标记,但这不是罪魁祸首。对我来说,这似乎很好,当您尝试运行此代码时,您实际遇到了什么错误(如果有),呈现了哪个图像?页面加载,但图像不加载。幻灯片代码等没有读取任何大小的图像。。虽然如果我使用上面的代码,ti可以正常工作,但是如果没有附加到文章的特色图像,我没有加载默认图像的选项。谢谢你看这个。。。真的不知道为什么下面的方法不起作用。。我希望它会有明显的问题…马特,谢谢你抽出时间。我完全理解你在做什么,但是默认的背景看起来确实超越了post_缩略图。。我只在css中设置的高度/宽度处得到一个空div。。。不过我现在可能有点昏昏欲睡。。然而,我想让条件语句工作起来,以完成我开始的工作。。再次感谢或者慢慢来亚当,非常感谢,这很有道理它完全有效,但现在有一个小问题。。。我最终得到错误代码的部分原因是,我的jQuery代码通过从加载的图像中获取元素并根据加载切换到下一个图像等来运行。。。这附近有什么地方吗?我能以某种方式将值加载到变量中吗?Adam,别介意我克服了恐慌,修改了jQuery代码(至少可以说,我也不完全自信)。。所有的工作都像我现在希望的那样!谢谢你的迅速帮助!!干杯David对于任何想使用此功能的人,我应该补充一点,我必须在“get_bloginfo()”之前将“echo”放回,以便将图像打印到屏幕上。。。我想我在排除故障时它不见了。
<?php if(has_post_thumbnail()): ?>
    <a href="<?php the_permalink() ?>" title="<?php the_title() ?>">
        <?php the_post_thumbnail() ?>
    </a> 
<?php else: ?>
    <a href="<?php the_permalink() ?>" title="<?php the_title() ?>">
        <img src="<?php get_bloginfo("template_url"); ?>/images/xiao-default-image.png" />;
    </a>
<?php endif ?>