Mechanical waves need a material medium to travel.
Transverse waves can travel only in solids and only along the surface of liquids, but not in the entire liquid.
Longitudinal waves can travel in all i.e. solids, liquids, and gases.
Thus, whether the wave will be transverse or longitudinal definitely depends on the nature of the medium.
Mode of excitation means the manner in which the medium is set into vibrations.
If the vibrations are perpendicular to the length of the medium, then the waves will be transverse.
If they are parallel then it will be longitudinal.
For example:−
Consider a long spring which is horizontal with its one end attached to a wall and the other end is in your hand.
Now if you move your hand back and forth rapidly, then the waves on the spring will be longitudinal.
Instead if you move your hand up and down, then they will be transverse.
Thus, whether the wave will be transverse or longitudinal depends also on the mode of excitation.