【问题标题】:Add class on default placeholder to thumbnail (Wordpress loop)将默认占位符上的类添加到缩略图(Wordpress 循环)
【发布时间】:2023-03-29 16:14:01
【问题描述】:

我正在更新我的 loop.php 中的类,并且除了图像类之外,我已经设法完成了这一切。除此之外,我还想包含一个默认占位符图像,如果没有更新图像,则会显示该图像。有人可以帮忙吗?

这是我目前的 loop.php 代码:

<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class('h-entry'); ?>>

    <!-- post thumbnail -->
    <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="h-entry__image-link">
            <?php the_post_thumbnail(array(120,120));
        </a>
    <?php endif; ?>
    <!-- /post thumbnail -->

    <!-- post title -->
    <h2 class="p-name">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h2>
    <!-- /post title -->

    <!-- post details -->
    <time datetime="<?php the_time('Y-m-j'); ?>" class="dt-published"><?php the_time('jS F Y'); ?></time>
    <!-- /post details -->

    <?php html5wp_summary('html5wp_index'); // Build your custom callback length in functions.php ?>

    <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="arrow-link">Read the full article</a></p>

    <?php edit_post_link(); ?>

</article>
<!-- /article -->

我确实需要使用以下代码修改我的functions.php 中的摘要文本类:

function html5wp_summary($length_callback = '', $more_callback = '')
{
    global $post;
    if (function_exists($length_callback)) {
        add_filter('excerpt_length', $length_callback);
    }
    if (function_exists($more_callback)) {
        add_filter('excerpt_more', $more_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    $output = '<p class="p-summary">' . $output . '</p>';
    echo $output;
}

不确定我是否需要为图像做类似的事情,但我无法让它工作。

在此先感谢,希望有人能提供帮助:)

【问题讨论】:

  • 你的意思是the_post_thumbnail图片吗?
  • 您已经拥有class="h-entry__image-link",要更改吗?还是加进去?
  • 它实际上是img 标签上的类,而不是链接。但是,我想我可以通过在末尾添加另一个数组来做到这一点:the_post_thumbnail(array(120,120),array('class' =&gt; 'u-featured')); - 看起来可以吗?它似乎工作!

标签: php wordpress wordpress-theming


【解决方案1】:

只需在此处添加一个 else 条件

<!-- post thumbnail -->
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="h-entry__image-link">
    <?php if ( has_post_thumbnail()) { // Check if thumbnail exists ?>
            <?php the_post_thumbnail(array(120,120)); ?>
    <?php } ?>
<?php else {?>
               <img src="path to default image" alt="<?php the_title(); ?>"/>
<?php } ?>
 </a>
    <!-- /post thumbnail -->

【讨论】:

  • 如果这将是公认的答案,那么您至少可以重写代码以删除重复的命令
  • 我更新了代码。核实。我希望这对你有用。
  • 嘿,谢谢你的代码!我收到一个“PHP Parse 错误:语法错误,意外的 'else' (T_ELSE)”错误:/ 另外,我想我可以使用这个 &lt;?php the_post_thumbnail(array(120,120),array('class' =&gt; 'u-featured')); 添加一个类,如果这看起来不错?
  • 我错过了一个括号,更新了检查
  • 嗯,我仍然收到错误消息。我认为与&lt;?php else {?&gt; 有关。这就是它所说的无论如何都有问题的行。
猜你喜欢
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 1970-01-01
相关资源
最近更新 更多