• GeraldW

    (@geraldw)


    As the title says. I’m after help with a piece of code.

    I’m somewhat new to WP and I’m looking for a piece of code which will completely remove the title-link on a post but only if that post has a featured image.

    I believe once I have the piece of code I need to put it into a content-home.php file in my child theme?

    Can anyone help me out with this?

    Cheers,
    Gerald

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter GeraldW

    (@geraldw)

    if(has_post_thumbnail()) {
    <?php the_title();
    }

    Is the closest I got piecing other bits together but it didn’t do anything except break my entire site.

    Triptripper

    (@triptripper)

    Try

    <?php if ( has_post_thumbnail()) { ?>
    <?php } else { ?>
    	<?php the_title(); ?>
    <?php } ?>

    Thread Starter GeraldW

    (@geraldw)

    Hi Triptripper,

    I tested that code out in a completely empty content-home.php file in my child theme and it broke my site (nothing showed up and some text about broken syntax). I’m not sure if it is the code or my process of inserting it.

    I’ve always used the 1-click child theme plug-in which makes my child theme style sheets for me and I’ve never had to edit one of the .php files in a child theme.. so it was my first time doing that.

    Using my FTP I downloaded the content-home.php and re-uploaded it into my childtheme’s folder. It then shows up in my child theme editor (all good I think)

    But I’m not sure what else needs to be in that content-home.php? When your piece of code is the only code in there it breaks the site.

    the website in question is http://www.geraldwiblin.com

    and the entire content-home.php code is here:

    <?php
    /**
     * @package Spun
     * @since Spun 1.0
     */
    
    global $post;
    
    if ( '' != get_the_post_thumbnail() ) {
    	$thumb = get_the_post_thumbnail( $post->ID, 'single-post' );
    }
    else {
    	$args = array(
    				'post_type' => 'attachment',
    				'numberposts' => 1,
    				'post_status' => null,
    				'post_mime_type' => 'image',
    				'post_parent' => $post->ID,
    			);
    
    	$first_attachment = get_children( $args );
    
    	if ( $first_attachment ) {
    		foreach ( $first_attachment as $attachment ) {
    			$thumb = wp_get_attachment_image( $attachment->ID, 'single-post', false );
    		}
    	}
    }
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<?php if ( '' != $thumb ) { ?>
    		<div class="single-post-thumbnail">
    			<?php echo $thumb; ?>
    		</div>
    	<?php } ?>
    	<header class="entry-header">
    		<h1 class="entry-title"><?php the_title(); ?></h1>
    	</header><!-- .entry-header -->
    
    	<div class="entry-content">
    		<?php the_content(); ?>
    		<?php wp_link_pages( array( 'before' => '<div class="page-links">', 'after' => '</div>', 'link_before' => '<span class="active-link">', 'link_after' => '</span>' ) ); ?>
    	</div><!-- .entry-content -->
    
    	<footer class="entry-meta">
    		<?php if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) : ?>
    			<span class="comments-link">
    				<a href="#comments-toggle">
    					<span class="tail"></span>
    					<?php echo comments_number( __( '+', 'spun' ), __( '1', 'spun' ), __( '%', 'spun' ) ); ?>
    				</a>
    			</span>
    		<?php endif; ?>
    		<span class="post-date">
    			<?php spun_posted_on(); ?>
    		</span>
    		<?php
    			/* translators: used between list items, there is a space after the comma */
    			$categories_list = get_the_category_list( __( ', ', 'spun' ) );
    			if ( $categories_list && spun_categorized_blog() ) :
    		?>
    		<span class="cat-links">
    			<?php printf( __( '%1$s', 'spun' ), $categories_list ); ?>
    		</span>
    		<?php endif; // End if categories ?>
    
    		<?php
    			/* translators: used between list items, there is a space after the comma */
    			$tags_list = get_the_tag_list( '', __( ', ', 'spun' ) );
    			if ( $tags_list ) :
    		?>
    
    		<span class="tags-links">
    			<?php printf( __( '%1$s', 'spun' ), $tags_list ); ?>
    		</span>
    		<?php endif; // End if $tags_list ?>
    
    		<?php edit_post_link( __( 'Edit', 'spun' ), '<span class="edit-link">', '</span>' ); ?>
    	</footer><!-- .entry-meta -->
    </article><!-- #post-<?php the_ID(); ?> -->

    I’m not sure what to do with your piece of code?

    Simalam

    (@simalam)

    Hello GeraldW,

    I’m not sure if you wanted to display the featured image when there is one instead of the title.

    Showing featured image:

    <?php
    /**
     * @package Spun
     * @since Spun 1.0
     */
    
    global $post;
    
    if ( '' != get_the_post_thumbnail() ) {
    	$thumb = get_the_post_thumbnail( $post->ID, 'single-post' );
    }
    else {
    	$args = array(
    				'post_type' => 'attachment',
    				'numberposts' => 1,
    				'post_status' => null,
    				'post_mime_type' => 'image',
    				'post_parent' => $post->ID,
    			);
    
    	$first_attachment = get_children( $args );
    
    	if ( $first_attachment ) {
    		foreach ( $first_attachment as $attachment ) {
    			$thumb = wp_get_attachment_image( $attachment->ID, 'single-post', false );
    		}
    	}
    }
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<?php if ( '' != $thumb ) { ?>
    		<div class="single-post-thumbnail">
    			<?php echo $thumb; ?>
    		</div>
    	<?php } else { ?>
    	<header class="entry-header">
    		<h1 class="entry-title"><?php the_title(); ?></h1>
    	</header><!-- .entry-header -->
            <?php } ?>
    	<div class="entry-content">
    		<?php the_content(); ?>
    		<?php wp_link_pages( array( 'before' => '<div class="page-links">', 'after' => '</div>', 'link_before' => '<span class="active-link">', 'link_after' => '</span>' ) ); ?>
    	</div><!-- .entry-content -->
    
    	<footer class="entry-meta">
    		<?php if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) : ?>
    			<span class="comments-link">
    				<a href="#comments-toggle">
    					<span class="tail"></span>
    					<?php echo comments_number( __( '+', 'spun' ), __( '1', 'spun' ), __( '%', 'spun' ) ); ?>
    				</a>
    			</span>
    		<?php endif; ?>
    		<span class="post-date">
    			<?php spun_posted_on(); ?>
    		</span>
    		<?php
    			/* translators: used between list items, there is a space after the comma */
    			$categories_list = get_the_category_list( __( ', ', 'spun' ) );
    			if ( $categories_list && spun_categorized_blog() ) :
    		?>
    		<span class="cat-links">
    			<?php printf( __( '%1$s', 'spun' ), $categories_list ); ?>
    		</span>
    		<?php endif; // End if categories ?>
    
    		<?php
    			/* translators: used between list items, there is a space after the comma */
    			$tags_list = get_the_tag_list( '', __( ', ', 'spun' ) );
    			if ( $tags_list ) :
    		?>
    
    		<span class="tags-links">
    			<?php printf( __( '%1$s', 'spun' ), $tags_list ); ?>
    		</span>
    		<?php endif; // End if $tags_list ?>
    
    		<?php edit_post_link( __( 'Edit', 'spun' ), '<span class="edit-link">', '</span>' ); ?>
    	</footer><!-- .entry-meta -->
    </article><!-- #post-<?php the_ID(); ?> -->

    Not showing featured image:

    <?php
    /**
     * @package Spun
     * @since Spun 1.0
     */
    
    global $post;
    
    if ( '' != get_the_post_thumbnail() ) {
    	$thumb = get_the_post_thumbnail( $post->ID, 'single-post' );
    }
    else {
    	$args = array(
    				'post_type' => 'attachment',
    				'numberposts' => 1,
    				'post_status' => null,
    				'post_mime_type' => 'image',
    				'post_parent' => $post->ID,
    			);
    
    	$first_attachment = get_children( $args );
    
    	if ( $first_attachment ) {
    		foreach ( $first_attachment as $attachment ) {
    			$thumb = wp_get_attachment_image( $attachment->ID, 'single-post', false );
    		}
    	}
    }
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<?php if ( !$thumb ) { ?>
    	    <header class="entry-header">
    		<h1 class="entry-title"><?php the_title(); ?></h1>
    	    </header><!-- .entry-header -->
            <?php } ?>
    	<div class="entry-content">
    		<?php the_content(); ?>
    		<?php wp_link_pages( array( 'before' => '<div class="page-links">', 'after' => '</div>', 'link_before' => '<span class="active-link">', 'link_after' => '</span>' ) ); ?>
    	</div><!-- .entry-content -->
    
    	<footer class="entry-meta">
    		<?php if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) : ?>
    			<span class="comments-link">
    				<a href="#comments-toggle">
    					<span class="tail"></span>
    					<?php echo comments_number( __( '+', 'spun' ), __( '1', 'spun' ), __( '%', 'spun' ) ); ?>
    				</a>
    			</span>
    		<?php endif; ?>
    		<span class="post-date">
    			<?php spun_posted_on(); ?>
    		</span>
    		<?php
    			/* translators: used between list items, there is a space after the comma */
    			$categories_list = get_the_category_list( __( ', ', 'spun' ) );
    			if ( $categories_list && spun_categorized_blog() ) :
    		?>
    		<span class="cat-links">
    			<?php printf( __( '%1$s', 'spun' ), $categories_list ); ?>
    		</span>
    		<?php endif; // End if categories ?>
    
    		<?php
    			/* translators: used between list items, there is a space after the comma */
    			$tags_list = get_the_tag_list( '', __( ', ', 'spun' ) );
    			if ( $tags_list ) :
    		?>
    
    		<span class="tags-links">
    			<?php printf( __( '%1$s', 'spun' ), $tags_list ); ?>
    		</span>
    		<?php endif; // End if $tags_list ?>
    
    		<?php edit_post_link( __( 'Edit', 'spun' ), '<span class="edit-link">', '</span>' ); ?>
    	</footer><!-- .entry-meta -->
    </article><!-- #post-<?php the_ID(); ?> -->
    Thread Starter GeraldW

    (@geraldw)

    Hi Simalam,

    What I’m after is:

    When there is no featured image, show title and have it link.

    When there is a featured image, show the featured image but remove the link from it.

    Simalam

    (@simalam)

    This should work for you:

    <?php
    /**
     * @package Spun
     * @since Spun 1.0
     */
    
    global $post;
    
    if ( '' != get_the_post_thumbnail() ) {
    	$thumb = get_the_post_thumbnail( $post->ID, 'single-post' );
    }
    else {
    	$args = array(
    				'post_type' => 'attachment',
    				'numberposts' => 1,
    				'post_status' => null,
    				'post_mime_type' => 'image',
    				'post_parent' => $post->ID,
    			);
    
    	$first_attachment = get_children( $args );
    
    	if ( $first_attachment ) {
    		foreach ( $first_attachment as $attachment ) {
    			$thumb = wp_get_attachment_image( $attachment->ID, 'single-post', false );
    		}
    	}
    }
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<?php if ( '' != $thumb ) { ?>
    		<div class="single-post-thumbnail">
    			<?php echo $thumb; ?>
    		</div>
    	<?php } else { ?>
    	<header class="entry-header">
    		<h1 class="entry-title"><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></h1>
    	</header><!-- .entry-header -->
            <?php } ?>
    	<div class="entry-content">
    		<?php the_content(); ?>
    		<?php wp_link_pages( array( 'before' => '<div class="page-links">', 'after' => '</div>', 'link_before' => '<span class="active-link">', 'link_after' => '</span>' ) ); ?>
    	</div><!-- .entry-content -->
    
    	<footer class="entry-meta">
    		<?php if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) : ?>
    			<span class="comments-link">
    				<a href="#comments-toggle">
    					<span class="tail"></span>
    					<?php echo comments_number( __( '+', 'spun' ), __( '1', 'spun' ), __( '%', 'spun' ) ); ?>
    				</a>
    			</span>
    		<?php endif; ?>
    		<span class="post-date">
    			<?php spun_posted_on(); ?>
    		</span>
    		<?php
    			/* translators: used between list items, there is a space after the comma */
    			$categories_list = get_the_category_list( __( ', ', 'spun' ) );
    			if ( $categories_list && spun_categorized_blog() ) :
    		?>
    		<span class="cat-links">
    			<?php printf( __( '%1$s', 'spun' ), $categories_list ); ?>
    		</span>
    		<?php endif; // End if categories ?>
    
    		<?php
    			/* translators: used between list items, there is a space after the comma */
    			$tags_list = get_the_tag_list( '', __( ', ', 'spun' ) );
    			if ( $tags_list ) :
    		?>
    
    		<span class="tags-links">
    			<?php printf( __( '%1$s', 'spun' ), $tags_list ); ?>
    		</span>
    		<?php endif; // End if $tags_list ?>
    
    		<?php edit_post_link( __( 'Edit', 'spun' ), '<span class="edit-link">', '</span>' ); ?>
    	</footer><!-- .entry-meta -->
    </article><!-- #post-<?php the_ID(); ?> -->
    Thread Starter GeraldW

    (@geraldw)

    Hi Simalam.

    Thanks so much for working on that for me.

    I’ve just replaced the original code with yours and while it did remove links on the featured image posts… it also removed the “circles” and added what I think are comment buttons under the cinematography and photography posts…

    check the site out:

    http://www.geraldwiblin.com

    I’ll leave it up for a few minutes

    Thread Starter GeraldW

    (@geraldw)

    Update: I just removed the ability to comment on any of my posts and the comment button has been removed but beautiful circles have disappeared.

    Did you have a look at the site before?

    Thread Starter GeraldW

    (@geraldw)

    Here are two screen shots showing the difference between the original code and your code. I reverted the site back to its original state.

    View post on imgur.com

    Simalam

    (@simalam)

    If your html structure changed a little bit with the template you posted and what you are using on your site.

    You can change the CSS selectors and class names for the circles slightly to match the updated code. For example for the post images:

    .blog .hentry a .attachment-post-thumbnail
    TO
    .blog .hentry .single-post-thumbnail img

    or you could change the html structure in the template file to match your current setup for divs, spans, and images.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘IF statement Coding Help IFpost has featured image, remove post-title link’ is closed to new replies.