I am try to get rid of the category name appearing from the list of post titles. Here's the code I am using:
<?php
$category = get_queried_object();
if ($category) {
$category_name = $category->name; // Get the name of the current category
$category_id = $category->term_id;
$query = new WP_Query(array(
'cat' => $category_id,
'tag_id' => 11, // Show posts with tag ID 11
'posts_per_page' => 10
));
if ($query->have_posts()) {
echo '<ul>';
while ($query->have_posts()) {
$query->the_post();
$post_title = get_the_title();
// Remove the category name from the post title
$escaped_category_name = preg_quote($category_name, '/');
$title_without_category = preg_replace('/\b' . $escaped_category_name . '\b/i', '', $post_title);
echo '<li><a href="' . get_permalink() . '" style="text-decoration: none;">' . $title_without_category . '</a> </li>';
}
echo '</ul>';
} else {
echo '<p class="noposts" style="padding-left: 2em;">No posts found for ' . $category_name . '. If you have any suggestions, please let us know.</p>';
}
wp_reset_postdata();
}
?>
The code above works if there is no apostrophe included in the category name. For example, let's say the post title is Hello World and Me. Hello World is the category. I am getting the correct output which is and me. But when the category name has apostrophe like Hello World's and me. I am not getting the expected output which is the and me post title only.
Can anyone help me with this issue? Thanks!
I am try to get rid of the category name appearing from the list of post titles.