Forums

how to use the_title() in ALT tags (8 posts)

  1. spstieng
    Member
    Posted 3 years ago #

    I have a code that will display the IMG html code IF an image has been related to the post.

    // (...) post list loop (...)
    if(!empty($values[0]))
    {
    echo '';
    echo '<img src="' . $basePath[0] . $values[0] . '" alt="' . the_title() . '" />';
    echo '
    ';
    }
    // End post list loop

    The only problem I have is that the_title() is shown in plain text and not inside the HTML code.

    I've also tried using the the_title_attribute() with no luck.

    Any ideas anyone?

  2. hotkee
    Member
    Posted 3 years ago #

    Is it within the main wordpress loop or is it custom loop

  3. spstieng
    Member
    Posted 3 years ago #

    That was quick :)
    I think it's within a custom loop?

    <?php while (have_posts()) : the_post(); ?>
    (... , my code here)
    <?php endwhile; ?>

  4. hotkee
    Member
    Posted 3 years ago #

    Paste the full code, the_title() should be displaying properly, cant see anything obviously wrong.

  5. spstieng
    Member
    Posted 3 years ago #

    <div id="leftcol">
        <?php
          // "Featured articles" (Nyheter) module begins
          query_posts('showposts=3&cat=15'); ?>
          <h3> Nyheter  </h3>
          <?php while (have_posts()) : the_post(); ?>
          <div class="feature">
    
      <?php
    	$basePath = get_option('upload_path') . '/';
    	$permaLink = get_permalink();
    	// this grabs the image filename ffrom image gallery
    	$values = get_post_custom_values("featuredarticleimage");
    
    	// this checks to see if an image file exists
    	if(empty($values[0]))
    	{
    		//Grab url to external image
    		$values = get_post_custom_values("externalthumb");
    		$basePath = '';
    	}
    
    	// If and image has been related, it will be displayed
    	if(!empty($values[0]))
    	{
      	echo '<a href="' . $permaLink . '">';
      	echo '<img src="' . $basePath[0] . $values[0] . '" alt="artikkelbilde" />';
      	echo '</a>';
      }
      ?>
    
          <a href="<?php the_permalink() ?>" rel="bookmark" class="title">
            <?php the_title(); ?>
          </a>
          <p>
            <?php the_content_rss('Les mer','', '', 20); ?>
            <a href="<?php the_permalink() ?>">Les mer</a>.
          </p>
        </div>
        <?php endwhile; ?>
      </div>
      <!--END LEFTCOL-->

    See the line echo '<img src="' . $basePath[0] . $values[0] . '" alt="artikkelbilde" />'; ?

    I will replace 'artikkelbilde' with the_title() and the new code looks like this:
    echo '<img src="' . $basePath[0] . $values[0] . '" alt="' . the_title() . '" />';

    You can see the result here.

    Notice the small linked text in front of the headline. This is the ALT text. If you look at the source code, you will see alt="".

  6. hotkee
    Member
    Posted 3 years ago #

    Your code is generating

    <a href="">Manolo Blahnik blir foreviget<img src="http://i285.photobucket.com/albums/ll59/shoeslove/manolo-blahnik.jpg" alt="" /></a>

    I think you need to user $posts = query_posts(...)

  7. spstieng
    Member
    Posted 3 years ago #

    Didn't quite understand you answer hotkee.
    I am using query_posts()
    query_posts('showposts=3&cat=15');

    Well, I've created a workaround. I've added a custom fild fot image title and will use this for ALT text.

  8. hotkee
    Member
    Posted 3 years ago #

    Sorry I was talking rubbish in last reply.

    I managed to get the title coming out by using the following

    <img src = "<?php echo $basePath[0].$values[0]; ?>" alt="<?php the_title(); ?>" />

    This is placed outside of the main <?php ?>

Topic Closed

This topic has been closed to new replies.

About this Topic