Site logo

Archived topic

Related posts title

5 replies · Started by Liran on April 22, 2021

Viewing posts 1–6 of 6

Hey,

I have this hook for my related posts:

<div class="wpsp-related-posts  grid-container">
	<h2>You may also like</h2>
<?php
if ( is_single() ) {
    $cats =  get_the_category();
    $cat = $cats[0];
} else {
    $cat = get_category( get_query_var( 'cat' ) );
}

$cat_slug = $cat->slug;
$list = get_page_by_title( 'related posts', 'OBJECT', 'wp_show_posts' );
wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
?>
</div>

What's the best way to change the h2 title to 'More on *category name*'?

Thanks!

Hi there,

What’s the best way to change the h2 title to ‘More on *category name*‘?

Do you have any specific conditions for this change? Say for example, you only want to apply this change on single post page or only archive page?

Let us know.

Only on single posts.

Try this:

<?php
// Get the category
if ( is_single() ) {
    $cats =  get_the_category();
    $cat = $cats[0];
} else {
    $cat = get_category( get_query_var( 'cat' ) );
}
// Get the category values
$cat_name = $cat->name;
$cat_slug = $cat->slug;
$list = get_page_by_title( 'related posts', 'OBJECT', 'wp_show_posts' );
// Output the HMTL
?>
<div class="wpsp-related-posts  grid-container">
    <h2>More On <?php echo $cat_name; ?></h2>
    <?php wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' ); ?>
</div>

Wow, David. Thank you!

Glad to be of help.

This archived topic is closed to new replies.