WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Choose featured image based on title (3 posts)

  1. nengard
    Member
    Posted 2 years ago #

    I have a function in my theme that looks for the featured image or shows the default image. What I'd like to do is add an "else if" in there to say that if this post has a title that starts with 'Bookmarks for' I'd like to use a different image than the default. These posts are posted automatically and won't have a featured image so that's why I need to add it to the code. Any tips on altering this to do what I want?

    if ( has_post_thumbnail() ) {
    	 the_post_thumbnail( 'vulc_post', array('class' => 'postimg') );
    } else {
    ?>
    <img class="postimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.png"  />
    <?php
    };
  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    try for example:

    if ( has_post_thumbnail() ) {
    	 the_post_thumbnail( 'vulc_post', array('class' => 'postimg') );
    } elseif( strpos( ' '.get_the_title(), 'Bookmarks' ) >=1 ) {
    ?>
    <img class="postimg" src="<?php bloginfo('template_directory'); ?>/images/bookmark.png"  />
    <?php } else {
    ?>
    <img class="postimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.png"  />
    <?php
    };

    http://php.net/manual/en/function.strpos.php
    http://codex.wordpress.org/Function_Reference/get_the_title

  3. nengard
    Member
    Posted 2 years ago #

    Than you so very much! That did the trick :) and thanks for the manual pages as well. I'll be bookmarking those.

    Nicole

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.