-
AuthorPosts
-
August 17, 2020 at 6:45 pm #1238593
Hi
On the single post page I would like to change the Blog Latest posts title to the category that the post is in (for example Videos in the example), I have found this code that changes it to the post title but not the category, any advice is gladly received!
Many thanks!
JonAugust 19, 2020 at 4:31 pm #1239218Hi
Still after a little help if anyone has the time to take a look at this please?
Many thanks
Jon
August 23, 2020 at 10:24 pm #1240209Hi,
Sorry for the late reply and thanks for the link to your site and the documentation that you found.
I tested the function to found in the documentation and researched the WP Codex, but I was not able to replace the title bar title with the category name and link.
But I was able to achieve this with jQuery, the following script will only work on single posts that have the breadcrumbs showing since this is where the script gets the category link.
Try adding this code to the end of your functions.php file in Appearance > Editor:function custom_script(){ ?> <script> (function($){ $(document).ready(function(){ $("#top.single-post").each(function(){ var caturl = $(this).find(".breadcrumb-trail span:nth-child(4) > span > a").attr("href"); var catname = $(this).find(".breadcrumb-trail span:nth-child(4) > span > a > span").text(); $(this).find(".main-title.entry-title a").html(catname).attr("href", caturl); }); }); })(jQuery); </script> <?php } add_action('wp_footer', 'custom_script');
Then clear your browser cache and any cache plugin, and check.
Best regards,
MikeAugust 25, 2020 at 11:23 am #1240640Hi Mike
Thank you for this, it is extremely helpful and is working! Superstar!
There is one tweak, where there i more than one cat ticked in the admin it picks the first cat rather than the primary cat, is there a way to show the primary cat title if it is ticked?
August 28, 2020 at 12:55 pm #1241561Hi,
Sorry for the late reply, glad to hear this helped.
When I check the breadcrumbs trail on single post items, with multiple categories, only one category is shown in the breadcrumbs.
For example please see the link in the Private Content area, it belongs to the categories “Article of the Week” & “Videos”, but in the breadcrumbs only “Article of the Week” is shown.
So my thought to give to more control over the category that is used for the post title is to scrap the above script and add the primary category as a class to the body of the post and then use that to determine the post title category link.
The drawback to this is that you will need to plan your primarily categories out so only one primary is used for each post and you will have to manually edit the script if you add more primary categories in the future.
Another possible workaround is if you are using Yoast, you can configure the breadcrumb options in Yoast panel then use this function here to set the primary category in the breadcrumbs, and then the script above would work correctly.
So if you use Yoast then please try this, if not then I will try the other approach.Best regards,
Mike -
AuthorPosts
- You must be logged in to reply to this topic.