The Messenger of Allah ﷺ said, "The rider follows behind the funeral procession, while the one on foot may walk wherever he wishes around it, and the funeral prayer is performed over the infant."
أخبرنا زياد بن أيوب قال حدثنا عبد الواحد بن واصل قال حدثنا سعيد بن عبيد الله وأخوه المغيرة جميعا عن زياد بن جبير عن أبيه
قال رسول الله ﷺ الراكب خلف الجنازة والماشي حيث شاء منها والطفل يصلى عليه
[Its chain of transmission is hasan] Ibn Majah narrated it in the Book of Funerals, Chapter on What Has Been Said Concerning Attending Funerals, no. 1481, 1507, from the hadith of Saeed bin Ubaydullah bin Jubayr bin Hayyah, with this wording. It is also found in al-Kubra, no. 2069. Tirmidhi graded it sahih, no. 1031, from the hadith of Ziyad bin Jubayr, as did Ibn Hibban and Hakim, and Dhahabi agreed. See the hadith in Nayl al-Maqsud, no. 3180, from the hadith of Ziyad bin Jubayr, if you wish.