我在一個WordPress網站上工作,需要使用PHP代碼片段來操作HTML。具體來說,我需要添加、替換或更改某個類。我當前的代碼能夠定位特定的文章,但是不能檢索它的CSS類,并且我不能改變它。我如何才能做到這一點?
function add_video_symbol3($containsVideo, $postId) {
if ($containsVideo === '1'){
//replace-method does not work
//$thumbnail_html_with_class = str_replace('class="thumbnail-link', 'class="thumbnail-link video_post_play-button-overlay', $thumbnail_html);
return $containsVideo;
}
}
add_filter('post_thumbnail_html', 'add_video_symbol', 10, 2);
// Set up the query to retrieve all posts
$args = array(
'post_type' => 'post', // Replace with your post type if necessary
'posts_per_page' => -1, // Retrieve all posts
);
$query = new WP_Query($args);
// Check if there are any posts
if ($query->have_posts()) {
// Loop through each post
while ($query->have_posts()) {
$query->the_post();
// Get the value of a specific custom field (e.g., 'contains_video')
$containsVideo = get_post_meta(get_the_ID(), 'contains_video', true);
$postId = get_the_ID();
$filter_val = apply_filters('post_thumbnail_html', $containsVideo, $postId);
}
// Restore the global post data
wp_reset_postdata();
} else {
echo 'No posts found';
}