• clane_workforce

    (@clane_workforce)


    Hello,

    I am making simple statements in HTML to reference WP operators. My HTML tags (<p>, <em>) do not display wrapping around the WP operator. Example of my code below. Any help is appreciated:)

    else {
    echo the_post_thumbnail();
    echo '<h4><a href="'. get_permalink() .'" title="'. the_title_attribute( array( 'echo' => 0 ) ) .'">'. get_the_title() .'</a></h4>';
    echo '<p><em>' .the_category( ' ' ). '</em></p>';
    echo '<p>' .excerpt('20'). '</p>';
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • For starters, make sure to not echo functions that already return.

    You got it right by setting echo=0 on the_title_attribute, which echoes by default, and with your use of the get_the_title function.

    Instead of the_category you should use get_the_category instead, if you must use it in an echo statement..

    There’s also no need to echo the_post_thumbnail as it already returns all the post thumbnail markup by default.

    I’m assuming the excerpt() function is defined elsewhere? If so, you should probably be prefixing that. Or do you mean get_the_excerpt?

    You’re also welcome to simply close the PHP tag and proceed with something like this:

    else }
    the_post_thumbnail(); ?>
    <h4><a href="<?php the_permalink(); ?>"><!-- and so on -->
    Thread Starter clane_workforce

    (@clane_workforce)

    Hello,

    Thank you for the help Leland Fiegel! I thought I should provide my code in context. I also want to sort the post results by post type. I am using Search & Filter Pro with a custom template for my results to filter Media Attachments and Posts. I have added a category to my Media Attachments for the filtering.

    I want the Featured Image from the post to be linked to the post, but it is not linked and it seems to be using my “if” statement and grabbing the image without a link.

    I’m sure my code could be better written—this is a really raw implementation. Your help is greatly appreciated:)

    <?php
    if ( $query->have_posts() )
    {
    ?>
    <?php
    	while ($query->have_posts())
    	{
    		$query->the_post();
    
    ?>
    
    <div id="sf-results">
    <?php
    $post = get_post($media_item_id);
    if ($post && $post->post_type == 'attachment') {
    echo wp_get_attachment_image( $attachment->ID, 'full' );
    echo '<p><em>';
    echo the_category(" ");
    echo '</em></p>';
    }
    else {
    echo '<a href="'. get_permalink() .'" title="'. the_title_attribute( array( 'echo' => 0 ) ) .'">' . the_post_thumbnail() . '</a>';
    echo '<h4><a href="'. get_permalink() .'" title="'. the_title_attribute( array( 'echo' => 0 ) ) .'">'. get_the_title() .'</a></h4>';
    echo '<p><em>';
    echo the_category(" ");
    echo '</em></p>';
    echo '<p>';
    echo excerpt('20');
    echo'</p>';
    }
    ?>
    </div>
    <?php
    	}
    	?>
    <div class="pagination">
      <?php
    			/* example code for using the wp_pagenavi plugin */
    			if (function_exists('wp_pagenavi'))
    			{
    
    				wp_pagenavi( array( 'query' => $query ) );
    
    			}
    		?>
    </div>
    <?php
    }
    else
    {
    	echo "No Results Found";
    }
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘PHP in HTML’ is closed to new replies.