Change Titel to Description of an image

Hi,

I have a content-attachment.php page in my child Theme for every picture in my blog. So every picture has an own site like: https://one-million-places.com/reiseberichte/europa/griechenland/windmuehlen-mykonos/attachment/mykonos-town-31

My question: how can use the Description of this picture as my Title?

So in this case the Titel of the picture is “Mykonos Town 31” and the description of this picture is “Die märchenhaften Gassen von Mykonos”

I like to have the description as the h1 titel on top of the page.

Here are the first rows of my content-attachment.php file:

Blockquote

<?php // VIEWS/GLOBAL/ATTACHMENT.PHP // ----------------------------------------------------------------------------- // Display of attachment content. // ============================================================================= ?> <?php do_action( 'x_before_the_content_begin' ); ?>
<?php do_action( 'x_after_the_content_begin' ); ?>
<?php x_get_view( 'ethos', '_content', 'post-header' ); ?>
<?php echo "Schön, dass ihr unseren Reiseblog One Million Places besucht.

How can I do that?

Michael

Hi There,

Please create the _content-post-header.php file under this directory: x-child/framework/views/ethos with the following code:

<?php
global $post;
// =============================================================================
// VIEWS/ETHOS/_CONTENT-POST-HEADER.PHP
// -----------------------------------------------------------------------------
// Standard <header> output for various posts.
// =============================================================================

?>

<header class="entry-header">
  <?php if ( is_single() ) : ?>
    <?php x_entry_navigation(); ?>
    <h1 class="entry-title">
    	<?php 
	    	if(is_singular( 'attachment' )){
	    		echo $post->post_content;
	    	} else {
	    		the_title(); 
	    	}
    	?>
    </h1>
  <?php else : ?>
    <h2 class="entry-title">
      <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ); ?>"><?php x_the_alternate_title(); ?></a>
    </h2>
  <?php endif; ?>
  <?php x_ethos_entry_meta(); ?>
</header>

Hope it helps :slight_smile:

Hi,

thank you for your answer.

When I add this code under x-child/framework/views/ethos the Titel at the image page is empty.
Are you shure that x_the_alternate_title() is the field “Description” of an image?

Michael

Hi Michael,

Maybe I miss-understood you.

Could you please provide us with a screenshot how the tiitle and description should look like?

Thank you.

No Problem. At the moment the Title at my content-attachment.php comes from the green field.
I like to use the Titel on top of the page from the Red field.

Hi There,

While that is outside the scope of support, I could point you in the right direction with the understanding that it would ultimately be your responsibility to take it from here.

First, please setup a child theme, then add this to your child theme’s functions.php file

/**
Replace Image attachment title with caption
*/
function caption_title() { 
 if ( is_attachment() ) : ?>
<header class="attachment-entry-header">
  <?php x_entry_navigation(); ?>
  <h2 class="entry-title h1"><?php the_excerpt(); ?></h2>
  <?php x_ethos_entry_meta(); ?>
</header>
<?php endif; 
}
add_action('x_before_view_ethos__content-post-header','caption_title', 30);

Then add this to Theme Options > CSS

.single-attachment .entry-header {
   display: none;
}

.single-attachment .attachment-entry-header .entry-title p {
    font-size: 200%;
    margin-bottom: 0;
}

Hope it helps,
Cheers!

Hi,

thank you for your help. This works perfect. Please have a look:

Just one last question:

With the display: none of .single-attachment .entry-header you can see that headline on the page, but it is still on my source code. So I have 2 headers there. Please see the screenshot.

I don´t know if this is very good vor SEO.

So is there a possibility to delete the entry-header on that page in my x-child templates?

And is there a php code to display the Alt-Tags under the image, like <?php the_alt_tag(); ?>

Michael

Hey Michael,

The entry header is in the content template so you can copy the content files in your child theme and then remove the block of code.

Hope that helps.

When I remove the block of code from the _content-post-header.php the Titel will also be deleted from all my posts.
What about the code in line 12? Is it possible to say:

if is_single DO 1
if is_attachment DO 2
ELSE DO 3

And is there a php code to display the Alt-Tags of an image at the site, like <?php the_alt_tag(); ?>
In my screenshot above I mean the field “Alternativer Text”

I want to show the follow Text under the image:
This image has the following tags: XX, XX, XX, XX

Hi Michael,

Ok, let’s do that instead. Please remove the previous PHP code and CSS code that I provided.

Then login to your server via FTP and navigate to this directory \wp-content\themes\x\framework\views\ethos

Look for and copy the file _content-post-header.php

Navigate to \wp-content\themes\x-child\framework\views\ethos directory (create it if it does not exist)

Paste the file _content-post-header.php in there, open/edit that file and look for this line:

<h1 class="entry-title"><?php the_title(); ?></h1>

Update that line to this:

<h1 class="entry-title"><?php is_attachment() ? the_excerpt() : the_title(); ?></h1>

Save the file and refresh your page, clear caching plugin (if any) and clear your browser’s cache.

Then use this custom CSS to styling the caption title.

.single-attachment .entry-header .entry-title p {
    font-size: 200%;
    margin-bottom: 0;
}

We like to continue on helping on this, regretfully, this is going outside the scope of our support that we can offer, you may wish to consult a developer to assist you on further customization with this.

Thank you for understanding,

Thank you for your update. But this makes no difference. The part from my post #7 is still twice on the attachemnt site.

I understand that this is maybe outside the scobe. But just to understand: what is your new code doing and to help others who try the same. Does your code mean:
IF site IS attachemnt OR excerpt THEN show the_title

If so then it´s not the logic I need like, or?

if is_single DO 1
if is_attachment DO 2
ELSE DO 3

Hi There,

Please remove the previous code on your child theme’s functions.php file. Because that was the code that giving you 2 headers.

This new solution is a different approach, as this will only show 1 header.

<h1 class="entry-title"><?php is_attachment() ? the_excerpt() : the_title(); ?></h1>

That is a simplefied if condition which means.

If it is attachment show the caption, else show the title.

Make sure you remove the previous CSS that I provided as well, as that will hide the header.

.single-attachment .entry-header {
   display: none;
}

Hope this shed some lights,
Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.