WordPress.org

Forums

[resolved] php echo a title above my list of related posts (3 posts)

  1. charlesdunworth
    Member
    Posted 1 year ago #

    hello

    I am using the bones template theme but really this question must be so basic that that is irrelevant.

    bones has a function for related posts which outputs an <li> foreach post in an array. Above this list I would like to echo a <p> or maybe <h3> tag which just says "here are some related posts"

    here is the function

    function bones_related_posts() {
    	echo '<ul id="bones-related-posts">';
    	global $post;
    	$tags = wp_get_post_tags( $post->ID );
    	if($tags) {
    		foreach( $tags as $tag ) {
    			$tag_arr .= $tag->slug . ',';
    		}
    		$args = array(
    			'tag' => $tag_arr,
    			'numberposts' => 5,
    			'post__not_in' => array($post->ID)
    		);
    		$related_posts = get_posts( $args );
    		if($related_posts) {
    			foreach ( $related_posts as $post ) : setup_postdata( $post ); ?>
    				<li class="related_post"><a class="entry-unrelated" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a><?php the_excerpt(); ?></li>
    			<?php endforeach; }
    		else { ?>
    			<?php echo '<li class="no_related_post">' . __( 'No Related Posts Yet!', 'bonestheme' ) . '</li>'; ?>
    		<?php }
    	}
    	wp_reset_query();
    	echo '</ul>';

    Of course I don't want this title to show up if there are no related posts, nor do I want it to display foreach related post. ( this, of course, happened when I tried just whacking <p>title related</p> in the if(related_posts) foreach function.

    Any help for a rank amateur would be much appreciated. I suppose if I could use the $tag_arr to use the tag name in the title that would be even better but I'd settle for any help or a nudge in the right direction

  2. davidsword
    Member
    Posted 1 year ago #

    I would change all the echo's to store in a variable, then count how many related posts have been generated, and if that number is great than 0 echo the title, then follow with echoing the variable.

    As per wordpress forum rules I don't think I can just do it for you, but below should be the nudge you're looking for, it'd be similar to:

    function bones_related_posts() {
       $return = "<ul id="bones-related-posts">";
        ...
       $count = 0;
       if($related_posts) {
           foreach...
                $count++; //count how many related posts are being generated
                $return .= '<li class="related_post">....';
       ...
       wp_reset_query();
       $return .= "</ul>";
    
      if ($count > 0) { //only show title when there's more than 0 posts
         echo "<h3>...</h3>";
      }
      echo $return; // your normal output, <ul> with posts, or notice of none.
      ...

    note that when modifying themes, if the theme author updates the plugin it will overwrite changes. making child themes are a good way of preventing this.

  3. charlesdunworth
    Member
    Posted 1 year ago #

    thanks a lot davidsword, that makes plenty more sense, it's working

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.