Some special wordpress functions

Some special wordpress functions

 

# 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>';
  }
}

 

 

 

 

 

 

Leave a Reply