Emergence of cooperation in evolutionary prisoner's dilemma game strongly depends on the topology of underlying interaction network. We explore this dependence using community networks with different levels of structural heterogeneity, which are generated by a tunable upper-bound on the total number of links that any vertex can have. We study the effect of community structure on cooperation by analyzing a finite population analogue of the evolutionary replicator dynamics. We find that structural heterogeneity mediates the effect of community structure on cooperation. In the community networks with low level of structural heterogeneity, community structure has negative effect on cooperation. However, the positive effect of community structure on cooperation appears and enhances with increasing structural heterogeneity. Our work may be helpful for understanding the complexity of cooperative behaviors in social networks.