2
votes

I am modifying a WordPress plugin / widget. It is a category-post plugin where we can display posts from a specific category. I am trying to make the widget to show only one post (easy) and then I add a "Read More" button at the bottom. But, instead of showing the rest of the post (they are short posts, like quotes for example) I want "Read More" to take the user to the respective category page.

So far the best I can do is:

<p class="read-more-custom-widget"><?php echo '<a href="'.get_the_category().'" title="Read More">Read More &raquo;</a>';?></p>

but this displays Array instead of the category. Can anyone please help me?

Thanks in advance.

EDIT: The posts will only have one category. Sorry this is so simple (and I bet it is) but I have googled around and can't find the answer.

1
get_the_category returns an array if there are multiple categories. Have you tried putting it in an array, then using something like $cat[0] as the href? - HappyTimeGopher
Thanks HappyTimeGopher, I am trying to do that right now. Honestly I am a beginner in PHP. - Mario88

1 Answers

0
votes

To expand on my comment, try this:

$category = get_the_category(); 
if($category[0]){
    echo '<p class="read-more-custom-widget">';
    echo '<a href="'.get_category_link($category[0]->term_id ).'"title="Read More">Read More &raquo;</a>';
    echo '</p>';
}

Adapted from the WP docs.