# Display data according to page
if (is_page('about')) { // or is_page(42) for ID
echo '<p>This is the About page.</p>';
}
# Display posts with pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => $paged,
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) {
while ($custom_query->have_posts()) {
$custom_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
echo '<p>' . get_the_excerpt() . '</p>';
}
// Pagination links
echo paginate_links(array(
'total' => $custom_query->max_num_pages,
));
wp_reset_postdata();
} else {
echo 'No posts found.';
}
# Get Product Category Name
$product_id = 123; // Replace with actual product ID
$terms = get_the_terms($product_id, 'product_cat');
if (!empty($terms) && !is_wp_error($terms)) {
foreach ($terms as $term) {
echo $term->name . '<br>';
}
}
# Get second last index value of an array $array = ['apple', 'banana', 'cherry', 'date']; // Get second last element $second_last = $array[ count($array) - 2 ]; echo $second_last; // Output: cherry # Example $actual_link = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $trimurl = trim(parse_url($actual_link, PHP_URL_PATH), '/'); $array = explode( "/", $trimurl); echo $array[count($array) - 2]; // get second last index value