Site logo

[Support request] How to use if (is_single()) for Posts

Home Forums Support [Support request] How to use if (is_single()) for Posts

Home Forums Support How to use if (is_single()) for Posts

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #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>&copy; ' . implode( ', ', $thumbnail_meta) . '</div>'; 
    
    elseif (!empty( count( array_filter ( $thumbnail_meta_lizenz ) ) >= 1 ) )
     $copyright[] = '<div>&copy; ' . implode( ', ', $thumbnail_meta_lizenz) . '</div>';
    
    elseif (!empty( count( array_filter ( $thumbnail_meta ) ) >= 1) )
     $copyright[] = '<div>&copy; ' . implode( ', ', $thumbnail_meta) . '</div>'; 
    
    elseif (empty( count( array_filter ( $thumbnail_meta_lizenz ) ) >= 1 ) )
     $copyright[] = '<div>&copy; ' . 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>&copy; ' . $attachment_copyright . '</div>';				
    			} elseif( count( array_filter ($attachment_meta_lizenz) ) >= 1 ){
    				$attachment_copyright = implode( ', ', $attachment_meta_lizenz );
    				$copyright[] = '<div>&copy; ' . $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?

    #2504958
    David
    Staff
    Customer Support

    Hi 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_page is true and if it is it will simply return original content.

    #2505011
    Marcus

    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

    Screenshot with is_single

    Here with the code as above

    Screenshot without is_single

    #2505272
    Fernando
    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>&copy; ' . implode( ', ', $thumbnail_meta) . '</div>'; 
    
    elseif (!empty( count( array_filter ( $thumbnail_meta_lizenz ) ) >= 1 ) )
     $copyright[] = '<div>&copy; ' . implode( ', ', $thumbnail_meta_lizenz) . '</div>';
    
    elseif (!empty( count( array_filter ( $thumbnail_meta ) ) >= 1) )
     $copyright[] = '<div>&copy; ' . implode( ', ', $thumbnail_meta) . '</div>'; 
    
    elseif (empty( count( array_filter ( $thumbnail_meta_lizenz ) ) >= 1 ) )
     $copyright[] = '<div>&copy; ' . 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>&copy; ' . $attachment_copyright . '</div>';				
    			} elseif( count( array_filter ($attachment_meta_lizenz) ) >= 1 ){
    				$attachment_copyright = implode( ', ', $attachment_meta_lizenz );
    				$copyright[] = '<div>&copy; ' . $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;
    }
    #2506164
    Marcus

    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

    #2506555
    Fernando
    Customer Support

    You’re welcome, Marcus!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.