- This topic has 5 replies, 3 voices, and was last updated 3 years, 3 months ago by
Fernando.
-
AuthorPosts
-
January 22, 2023 at 4:21 am #2504621
Marcus
Hello, I use the following code for a function that should only be used on article pages / in post not on pages.
add_filter( 'the_content', 'mmx_maybe_add_copyright_to_content', 500); function mmx_maybe_add_copyright_to_content( $content ) { $copyright = array(); /* thumbnail copyright */ $thumbnail_id = get_post_thumbnail_id(); $thumbnail_meta = array(); $thumbnail_meta_lizenz = array(); $thumbnail_meta[] = get_post_meta( $thumbnail_id, 'name', true ) ?? NULL; $thumbnail_meta[] = get_post_meta( $thumbnail_id, 'organisation', true ) ?? NULL; $thumbnail_meta_lizenz[] = get_post_meta( $thumbnail_id, 'lizenz', true ) ?? NULL; $thumbnail_meta_lizenz[] = get_post_meta( $thumbnail_id, 'autor', true ) ?? NULL; if (empty( count( array_filter ( $thumbnail_meta ) ) >= 1) ) $copyright[] = '<div>© ' . implode( ', ', $thumbnail_meta) . '</div>'; elseif (!empty( count( array_filter ( $thumbnail_meta_lizenz ) ) >= 1 ) ) $copyright[] = '<div>© ' . implode( ', ', $thumbnail_meta_lizenz) . '</div>'; elseif (!empty( count( array_filter ( $thumbnail_meta ) ) >= 1) ) $copyright[] = '<div>© ' . implode( ', ', $thumbnail_meta) . '</div>'; elseif (empty( count( array_filter ( $thumbnail_meta_lizenz ) ) >= 1 ) ) $copyright[] = '<div>© ' . implode( ', ', $thumbnail_meta_lizenz) . '</div>'; elseif (empty($thumbnail_meta) && (empty($thumbnail_meta_lizenz))) $copyright[] = ''; /* in content images copyright */ preg_match( '|<img.*?src=".*?/?>|', $content, $matches ); foreach( $matches as $img ){ preg_match( '|(src=")(.*?)"|', $img, $url); $attachment_id = attachment_url_to_postid( $url[2] ); $attachment_meta = array(); $attachment_meta_lizenz = array(); $attachment_meta[] = get_post_meta( $attachment_id, 'name', true ) ?? NULL; $attachment_meta[] = get_post_meta( $attachment_id, 'organisation', true ) ?? NULL; $attachment_meta_lizenz[] = get_post_meta( $attachment_id, 'lizenz', true ) ?? NULL; $attachment_meta_lizenz[] = get_post_meta( $attachment_id, 'autor', true ) ?? NULL; if( count( array_filter ($attachment_meta) ) >= 1 ){ $attachment_copyright = implode( ', ', $attachment_meta ); $copyright[] = '<div>© ' . $attachment_copyright . '</div>'; } elseif( count( array_filter ($attachment_meta_lizenz) ) >= 1 ){ $attachment_copyright = implode( ', ', $attachment_meta_lizenz ); $copyright[] = '<div>© ' . $attachment_copyright . '</div>'; } else { $copyright[] = ''; } } $images_found = count( array_filter ( $copyright ) ); if( $images_found >= 1 ){ $copyright_list = '<div class="copyright">'; $copyright_list .= $images_found === 1 ? 'Artikelbild' : 'Artikelbilder'; # @todo i18n gettext $copyright_list .= implode( '', $copyright ); $copyright_list .= '</div><br />'; $content .= $copyright_list; } return $content; }How do I adjust the code so that the output ‘Artikelfoto ©’ is only displayed on article pages / in posts but not in pages?
January 22, 2023 at 8:44 am #2504958David
StaffCustomer SupportHi there,
see this line:
function mmx_maybe_add_copyright_to_content( $content ) {after it add a condition for your pages eg.
function mmx_maybe_add_copyright_to_content( $content ) { if ( is_page() ) { return $content; }What this will do, at the top of the function it will check if
is_pageis true and if it is it will simply return original content.January 22, 2023 at 9:47 am #2505011Marcus
Hello David,
Thanks for your answer!
I tried it like this:
if (is_single()) { add_filter( 'the_content', 'mmx_maybe_add_copyright_to_content', 500); function mmx_maybe_add_copyright_to_content( $content ) { $copyright = array();This prevents the display on pages, but also in articles / posts
Here a Screenshot with is_single

Here with the code as above
January 22, 2023 at 6:50 pm #2505272Fernando Customer Support
Hi Marcus,
Did the suggestion by David not work?
This:
add_filter( 'the_content', 'mmx_maybe_add_copyright_to_content', 500); function mmx_maybe_add_copyright_to_content( $content ) { if ( is_page() ) { return $content; } $copyright = array(); /* thumbnail copyright */ $thumbnail_id = get_post_thumbnail_id(); $thumbnail_meta = array(); $thumbnail_meta_lizenz = array(); $thumbnail_meta[] = get_post_meta( $thumbnail_id, 'name', true ) ?? NULL; $thumbnail_meta[] = get_post_meta( $thumbnail_id, 'organisation', true ) ?? NULL; $thumbnail_meta_lizenz[] = get_post_meta( $thumbnail_id, 'lizenz', true ) ?? NULL; $thumbnail_meta_lizenz[] = get_post_meta( $thumbnail_id, 'autor', true ) ?? NULL; if (empty( count( array_filter ( $thumbnail_meta ) ) >= 1) ) $copyright[] = '<div>© ' . implode( ', ', $thumbnail_meta) . '</div>'; elseif (!empty( count( array_filter ( $thumbnail_meta_lizenz ) ) >= 1 ) ) $copyright[] = '<div>© ' . implode( ', ', $thumbnail_meta_lizenz) . '</div>'; elseif (!empty( count( array_filter ( $thumbnail_meta ) ) >= 1) ) $copyright[] = '<div>© ' . implode( ', ', $thumbnail_meta) . '</div>'; elseif (empty( count( array_filter ( $thumbnail_meta_lizenz ) ) >= 1 ) ) $copyright[] = '<div>© ' . implode( ', ', $thumbnail_meta_lizenz) . '</div>'; elseif (empty($thumbnail_meta) && (empty($thumbnail_meta_lizenz))) $copyright[] = ''; /* in content images copyright */ preg_match( '|<img.*?src=".*?/?>|', $content, $matches ); foreach( $matches as $img ){ preg_match( '|(src=")(.*?)"|', $img, $url); $attachment_id = attachment_url_to_postid( $url[2] ); $attachment_meta = array(); $attachment_meta_lizenz = array(); $attachment_meta[] = get_post_meta( $attachment_id, 'name', true ) ?? NULL; $attachment_meta[] = get_post_meta( $attachment_id, 'organisation', true ) ?? NULL; $attachment_meta_lizenz[] = get_post_meta( $attachment_id, 'lizenz', true ) ?? NULL; $attachment_meta_lizenz[] = get_post_meta( $attachment_id, 'autor', true ) ?? NULL; if( count( array_filter ($attachment_meta) ) >= 1 ){ $attachment_copyright = implode( ', ', $attachment_meta ); $copyright[] = '<div>© ' . $attachment_copyright . '</div>'; } elseif( count( array_filter ($attachment_meta_lizenz) ) >= 1 ){ $attachment_copyright = implode( ', ', $attachment_meta_lizenz ); $copyright[] = '<div>© ' . $attachment_copyright . '</div>'; } else { $copyright[] = ''; } } $images_found = count( array_filter ( $copyright ) ); if( $images_found >= 1 ){ $copyright_list = '<div class="copyright">'; $copyright_list .= $images_found === 1 ? 'Artikelbild' : 'Artikelbilder'; # @todo i18n gettext $copyright_list .= implode( '', $copyright ); $copyright_list .= '</div><br />'; $content .= $copyright_list; } return $content; }January 23, 2023 at 10:00 am #2506164Marcus
Hello,
somehow there was a white page when trying to try David’s version.
When I copied and pasted the code here in its entirety, it worked as intended.
thanks
January 23, 2023 at 5:06 pm #2506555Fernando Customer Support
You’re welcome, Marcus!
-
AuthorPosts
- You must be logged in to reply to this topic.