Navigation überspringen

[Gelöst] How can I limit this custom function to update ONLY the custom post type?

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

The issue here is that the user had some custom code for his archive but wanted it to be limited to a specific CPT.

Solution:

This can be done by using a simple if statement in PHP.

Check here for details.
https://toolset.com/forums/topic/how-can-i-limit-this-custom-function-to-update-only-the-custom-post-type/#post-1209933

This support ticket is created vor 5 Jahren, 11 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von PaulS4783 vor 5 Jahren, 11 Monaten.

Assistiert von: Shane.

Author
Artikel
#1209206

I've got the following code in my functions.php which pulls some custom fields out of an Events post type and prepends them to the exerpt of the RSS feed.

Problem is that the functions works on ALL feeds whereas I really only want it to apply to the Archive feed for the Events post type.

How can I add a conditional to check for the post type?

// Additional RSS Content
$rss_more_content = '';
$rss_more_position = 'before'; // or "after"

// Function which adds content to RSS entries
function add_rss_content($content) {
	global $rss_more_position, $rss_more_content, $post;
	
	$metaValue1 = get_post_meta($post->ID, 'wpcf-start-date', true);
    if(!empty($metaValue1)):
        $metaDate1 = date("Y m d", $metaValue1);
    endif;
    $metaValue2 = get_post_meta($post->ID, 'wpcf-end-date', true);
    if(!empty($metaValue2)):
        $metaDate2 = date("Y m d", $metaValue2);
    endif;

	if(is_feed()) {
		if ($rss_more_position == 'before') {
			$content = "<p>$rss_more_content <b>$metaDate1</b> -> <b>$metaDate2</b></p>\n$content";
		} else {
			$content .= "<p>$rss_more_content <b>$metaDate1</b> -> <b>$metaDate2</b></p>\n";
		}
	}
	return $content;
}

// Add hooks
add_filter('the_excerpt_rss', 'add_rss_content');
#1209510

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Paul,

This should help


// Additional RSS Content
$rss_more_content = '';
$rss_more_position = 'before'; // or "after"
 
// Function which adds content to RSS entries
function add_rss_content($content) {
    global $rss_more_position, $rss_more_content, $post;
     
    $metaValue1 = get_post_meta($post->ID, 'wpcf-start-date', true);
$post_type = get_post_type($post->ID);
if ($post_type='my-cpt-slug'){
    if(!empty($metaValue1)):
        $metaDate1 = date("Y m d", $metaValue1);
    endif;
    $metaValue2 = get_post_meta($post->ID, 'wpcf-end-date', true);
    if(!empty($metaValue2)):
        $metaDate2 = date("Y m d", $metaValue2);
    endif;
 
    if(is_feed()) {
        if ($rss_more_position == 'before') {
            $content = "<p>$rss_more_content <b>$metaDate1</b> -> <b>$metaDate2</b></p>\n$content";
        } else {
            $content .= "<p>$rss_more_content <b>$metaDate1</b> -> <b>$metaDate2</b></p>\n";
        }
    }
}
    return $content;
}
 
// Add hooks
add_filter('the_excerpt_rss', 'add_rss_content');

Please let me know if this helps.
Thanks,
Shane

#1209933

Close enough.

This is the final working code.

// Additional RSS Content
$rss_more_content = '';
$rss_more_position = 'before'; // or "after"
  
// Function which adds content to RSS entries
function add_rss_content($content) {
  global $rss_more_position, $rss_more_content, $post;
     
  $post_type = get_post_type($post->ID);
  
  if ($post_type=='event'){

  	$metaValue1 = get_post_meta($post->ID, 'wpcf-start-date', true);
        if(!empty($metaValue1)):
           $metaDate1 = date("Y m d", $metaValue1);
        endif;
       $metaValue2 = get_post_meta($post->ID, 'wpcf-end-date', true);
        if(!empty($metaValue2)):
           $metaDate2 = date("Y m d", $metaValue2);
        endif;
  
        if(is_feed()) {
           if ($rss_more_position == 'before') {
               $content = "<p>$rss_more_content <b>$metaDate1</b> -> <b>$metaDate2</b></p>\n$content";
           } else {
               $content .= "<p>$rss_more_content <b>$metaDate1</b> -> <b>$metaDate2</b></p>\n";
           }

       }

    }
    return $content;
}
  
// Add hook
add_filter('the_excerpt_rss', 'add_rss_content');

There was a small syntax error in your code.
Should be:

($post_type=='event')

not

($post_type='event')